-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors.cpp
More file actions
55 lines (46 loc) · 1.16 KB
/
constructors.cpp
File metadata and controls
55 lines (46 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
float marks;
public:
// ✅ Default constructor (no arguments)
Student() {
name = "Unknown";
age = 0;
marks = 0.0;
}
// ✅ Parameterized constructor (with arguments)
Student(string n, int a, float m) {
name = n;
age = a;
marks = m;
}
// ✅ Constructor overloading (different number/type of parameters)
Student(string n) {
name = n;
age = 14;
marks = 75.0;
}
// Function to display student details
void display() {
cout << "\n--- Student Info ---" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Marks: " << marks << endl;
}
};
int main() {
// 🔹 Using default constructor
Student s1;
s1.display();
// 🔹 Using parameterized constructor
Student s2("Aarav", 14, 88.5);
s2.display();
// 🔹 Using overloaded constructor (one string parameter)
Student s3("Riya");
s3.display();
return 0;
}