-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38_b_Single_Inheritance.cpp
More file actions
71 lines (48 loc) · 1.53 KB
/
38_b_Single_Inheritance.cpp
File metadata and controls
71 lines (48 loc) · 1.53 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
class Base{
int data1; // By default this variable is private and is not inheritable.
public:
int data2;
void setData();
int getData1();
int getData2();
};
// ****************************************************
// Deriving a Class from Base Class...!
class Derived : private Base{
int data3;
public:
void process();
void display();
};
// ****************************************************
void Base :: setData(void){
data1 = 10;
data2 = 20;
}
int Base :: getData1(){
return data1;
}
int Base :: getData2(){
return data2;
}
void Derived :: process(){
setData();
data3 = data2 * getData1(); // We can't write data1 directly as it is the private member of Base Class, and so we can't inherit it. That's why we are using function to get data1.
}
void Derived :: display(){
cout << "Value of data1 is : " << getData1() << endl; // We are taking the value of Data1 from the getData1 method, because the data1 is the private member of Base class.
cout << "Value of data2 is : " << data2 << endl;
cout << "Value of data3 is : " << data3 << endl;
}
// ****************************************************
int main()
{
Derived der; // der is the object of Derived Class.
// der.setData(); --> Can't do this because now it is private
// so we can't call setData() function from here. So we have to call it within the derived class only.
der.process();
der.display();
return 0;
}