-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinheritance.cpp
76 lines (59 loc) · 1.41 KB
/
inheritance.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<string>
using namespace std;
class Person {
public:
string name;
int age;
Person(string name, int age) {
this->name = name;
this->age = age;
}
Person() {
cout << "Parent constructor...\n";
}
};
class Classmate {
public:
string name;
int rollno;
};
class Teacher {
public:
string subject;
double salary;
};
class TA : public Classmate, public Teacher {
// You can initialize members here if necessary
};
class Student : public Person {
// name, age, rollno
public:
double rollno;
Student(string name, double age, int rollno) : Person(name, age) { // Corrected
this->rollno = rollno;
}
Student() { // Un-commented default constructor
cout << "Child constructor...\n";
}
void getInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Roll No: " << rollno << endl;
}
};
int main() {
Student s1;
s1.name = "Mashrafi Uddin";
s1.age = 21;
s1.rollno = 2;
Student s2("Sakiba Belal", 20, 1234); // Corrected constructor call
s1.getInfo();
cout << "-------------" << endl;
TA t1;
t1.name = "Mili"; // Inherits name from Classmate
t1.subject = "Math"; // Inherits subject from Teacher
cout << "TA Name: " << t1.name << endl;
cout << "TA Subject: " << t1.subject << endl;
return 0;
}