-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance2.cpp
64 lines (53 loc) · 989 Bytes
/
inheritance2.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
#include <bits/stdc++.h>
using namespace std;
/*
class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}
*/
// Single Inheritance
// Base Class
class Parent
{
public:
int a;
int b;
void get_data()
{
cout<<"Enter the value of a and b\n";
cin>>a>>b;
}
void show_data()
{
cout<<"The value of a is : "<<a<<endl;
cout<<"The value of b is : "<<b<<endl;
}
};
// Derived Class
// In Inheritance
/*
If you are Inheriting publicly
-> public member are public
-> protected member private
-> private member can never be Inherited
If you are Inheriting protected way
-> public member are public
-> protected member private
-> private member can never be Inherited
*/
class Child : public Parent{
public:
void sum(){
cout<<"The sum of a and b is : "<<a+b<<endl;
}
};
int main()
{
Child c1;
c1.get_data();
c1.show_data();
c1.sum();
cout<<c1.a<<endl;
return 0;
}