Skip to content

Commit 2aa9e82

Browse files
Single Level Inheritance-->
1 parent e89d5a5 commit 2aa9e82

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Single_Level_Inheritance.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// inheritance
5+
class Person
6+
{
7+
public:
8+
string name;
9+
int age;
10+
11+
// parameterized constructor
12+
Person(string name, int age)
13+
{
14+
cout << "I am now in parent" << endl;
15+
this->name = name;
16+
this->age = age;
17+
}
18+
19+
// non - parameterized
20+
Person()
21+
{
22+
cout << "I am now in parent" << endl;
23+
}
24+
25+
// Destructor
26+
~Person()
27+
{
28+
cout << "I am now in parent destructor" << endl;
29+
}
30+
};
31+
32+
// inheritance
33+
class Student : public Person
34+
{
35+
// name, age, rollno
36+
public:
37+
int rollno;
38+
Student(string name, int age, int rollno) : Person(name, age)
39+
{
40+
cout << "I am now in child" << endl;
41+
this->rollno = rollno;
42+
}
43+
44+
~Student()
45+
{
46+
cout << "I am now in child destructor" << endl;
47+
}
48+
49+
void getInfo()
50+
{
51+
cout << "name : " << name << endl;
52+
cout << "age : " << age << endl;
53+
cout << "rollno : " << rollno << endl;
54+
}
55+
};
56+
57+
int main()
58+
{
59+
Student s1("Sayan Dutta", 22, 00001);
60+
s1.getInfo();
61+
}

0 commit comments

Comments
 (0)