-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance.cpp
84 lines (68 loc) · 1.53 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
77
78
79
80
81
82
83
84
// #include <iostream>
// #include <string>
// using namespace std;
// class Person {
// public:
// string name;
// int age;
// Person(){
// cout<<"parent class constructor called first"<<endl;
// }
// ~Person(){
// cout<<"parent class destructor called later"<<endl;
// }
// };
// class Student : public Person {
// public:
// int rollno;
// Student(){
// cout<<"child class constructor called second"<<endl;
// }
// ~Student(){
// cout<<"child class destructor called first"<<endl;
// }
// void getInfo(){
// cout<<"name : "<<name<<endl;
// cout<<"age : "<<age<<endl;
// cout<<"rollno : "<<rollno<<endl;
// }
// };
// int main(){
// Student s1;
// s1.name = "rahul";
// s1.age = 20;
// s1.rollno = 123;
// s1.getInfo();
// return 0;
// }
// when parametrized constructor made then
#include <iostream>
#include <string>
using namespace std;
//example of single inheritance
class Person {
public:
string name;
int age;
Person(string name,int age){
this->name = name;
this->age = age;
}
};
class Student : public Person {
public:
int rollno;
Student(string name,int age,int rollno) : Person(name,age){
this->rollno = rollno;
}
void getInfo(){
cout<<"name : "<<name<<endl;
cout<<"age : "<<age<<endl;
cout<<"rollno : "<<rollno<<endl;
}
};
int main(){
Student s1("rahul",20,123);
s1.getInfo();
return 0;
}