BIT153 · 2082 · 10 marks
Write a program to create a class Student with data members id, name, and marks. Define constructors to initialize data members and demonstrate constructor overloading. Implement member functions to read students details, display them and calculate average marks.
Write a program to create a class Student with data members id, name, and marks. Define constructors to initialize data members and demonstrate constructor overloading. Implement member functions to read students details, display them and calculate average marks.[10]
Quick answer
Constructor overloading means defining multiple constructors in the same class with different parameter lists. The compiler selects the appropriate constructor based on the arguments passed during object creation.
Constructor Overloading with Student Class
Concept
Complete C++ Program
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int id;
string name;
float marks[5]; // stores marks of 5 subjects
int numSubjects;
public:
// -----------------------------------------------
// Constructor 1: Default Constructor
// Initializes data members with default values
// -----------------------------------------------
Student() {
id = 0;
name = "Unknown";
numSubjects = 5;
for (int i = 0; i < numSubjects; i++) {
marks[i] = 0.0;
}
cout << "Default constructor called." << endl;
}
// -----------------------------------------------
// Constructor 2: Parameterized Constructor
// Initializes id and name; marks set to 0
// -----------------------------------------------
Student(int i, string n) {
id = i;
name = n;
numSubjects = 5;
for (int j = 0; j < numSubjects; j++) {
marks[j] = 0.0;
}
cout << "Parameterized constructor (id, name) called." << endl;
}
// -----------------------------------------------
// Constructor 3: Fully Parameterized Constructor
// Initializes all data members including marks
// -----------------------------------------------
Student(int i, string n, float m[], int num) {
id = i;
name = n;
numSubjects = num;
for (int j = 0; j < numSubjects; j++) {
marks[j] = m[j];
}
cout << "Fully parameterized constructor called." << endl;
}
// -----------------------------------------------
// Member Function: Read student details
// -----------------------------------------------
void readDetails() {
cout << "\nEnter Student ID : ";
cin >> id;
cout << "Enter Student Name : ";
cin.ignore();
getline(cin, name);
cout << "Enter marks for " << numSubjects << " subjects:" << endl;
for (int i = 0; i < numSubjects; i++) {
cout << " Subject " << (i + 1) << " : ";
cin >> marks[i];
}
}
// -----------------------------------------------
// Member Function: Display student details
// -----------------------------------------------
void displayDetails() {
cout << "\n--- Student Details ---" << endl;
cout << "ID : " << id << endl;
cout << "Name : " << name << endl;
cout << "Marks : ";
for (int i = 0; i < numSubjects; i++) {
cout << marks[i];
if (i < numSubjects - 1) cout << ", ";
}
cout << endl;
cout << "Average: " << calculateAverage() << endl;
cout << "-----------------------" << endl;
}
// -----------------------------------------------
// Member Function: Calculate average marks
// -----------------------------------------------
float calculateAverage() {
float sum = 0.0;
for (int i = 0; i < numSubjects; i++) {
sum += marks[i];
}
return sum / numSubjects;
}
};
// -----------------------------------------------
// Main Function: Demonstrates constructor overloading
// -----------------------------------------------
int main() {
cout << "========================================" << endl;
cout << " Constructor Overloading Demo " << endl;
cout << "========================================" << endl;
// Using Default Constructor
cout << "\n[Object 1: Default Constructor]" << endl;
Student s1;
s1.readDetails();
s1.displayDetails();
// Using Parameterized Constructor (id and name only)
cout << "\n[Object 2: Parameterized Constructor (id, name)]" << endl;
Student s2(102, "Ram Sharma");
s2.readDetails();
s2.displayDetails();
// Using Fully Parameterized Constructor
cout << "\n[Object 3: Fully Parameterized Constructor]" << endl;
float m[] = {78.5, 85.0, 90.0, 72.5, 88.0};
Student s3(103, "Sita Thapa", m, 5);
s3.displayDetails(); // no need to read; already initialized
return 0;
}
Sample Output
========================================
Constructor Overloading Demo
========================================
[Object 1: Default Constructor]
Default constructor called.
Enter Student ID : 101
Enter Student Name : Hari Bahadur
Enter marks for 5 subjects:
Subject 1 : 75
Subject 2 : 80
Subject 3 : 65
Subject 4 : 90
Subject 5 : 70
--- Student Details ---
ID : 101
Name : Hari Bahadur
Marks : 75, 80, 65, 90, 70
Average: 76
-----------------------
[Object 2: Parameterized Constructor (id, name)]
Parameterized constructor (id, name) called.
...
[Object 3: Fully Parameterized Constructor]
Fully parameterized constructor called.
--- Student Details ---
ID : 103
Name : Sita Thapa
Marks : 78.5, 85, 90, 72.5, 88
Average: 82.8
-----------------------
Summary Table
| Constructor | Parameters | Purpose |
|---|---|---|
Student() | None | Default values assigned |
Student(int, string) | id, name | Partial initialization |
Student(int, string, float[], int) | id, name, marks array and count | Complete initialization at the time of object creation |
Key Points Demonstrated
| Feature | Where it appears in the program |
|---|---|
| Constructor overloading | Three constructors share the name Student but differ in their parameter lists |
| Compile time polymorphism | The compiler picks the constructor by matching the argument list, so the choice is made at compile time, not at run time |
| Data hiding | id, name and marks are private and are reached only through the member functions |
| Member functions | readDetails() takes input, displayDetails() prints the record and calculateAverage() returns the average |
| Automatic invocation | No constructor is ever called by name; each runs the moment its object is created |
The three constructors are legal together because their signatures differ, Student(), Student(int, string) and Student(int, string, float[], int), and an overload set is resolved on the number and the type of the arguments only. Note that a return type may not be used to distinguish them, since a constructor has no return type at all.
The average is computed as:
$$\text{Average} = \frac{\sum_{i=1}^{n} marks_i}{n}$$
so for the third object the average is (78.5 + 85 + 90 + 72.5 + 88) / 5, which is 82.8 as printed in the sample output.
Conclusion
The program defines a Student class with private data members and three overloaded constructors, then creates one object through each of them to show that the compiler selects the constructor from the argument list. Together with the member functions for reading, displaying and averaging the marks, it demonstrates encapsulation and compile time polymorphism in a single, exam-ready example.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.