-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinheritancefunctionoverriding.cpp
100 lines (80 loc) · 1.43 KB
/
inheritancefunctionoverriding.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
85
86
87
88
89
90
91
92
93
94
95
96
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
/*
class B{
int num;
public:
void getdata();
void display();
};
void B::getdata(){
cout<<"accept number: ";
cin>>num;
}
void B::display(){
cout<<"\nnumber is: "<<num;
}
class D:public B{ //single inheritance
int num1;
public:
void getdata();
void display();
};
void D::getdata(){
// B::getdata(); another way for function overriding
cout<<"accept number: ";
cin>>num1;
}
void D::display(){
cout<<"\nnumber is: "<<num1;
}
/*D: private- int num1;
public- getdata(), display(), getdata(), display()
int main() {
D d;
d.B::getdata(); //function overriding
d.getdata();
d.display();
d.B::display();
return 0;
}
*/
class Base{
protected:
int num1, num2, answer;
public:
void getdata();
void display();
void result();
};
void Base::getdata(){
cout<<"enter first number: ";
cin>>num1;
cout<<"enter second number: ";
cin>>num2;
}
void Base::display(){
cout<<"\noutput is: "<<answer;
}
void Base::result(){
answer=num1+num2;
}
class Derived:public Base{
public:
void result();
};
void Derived::result(){
answer=num1*num2;
}
int main(){
/* Base b;
b.getdata();
b.result();
b.display(); */
Derived d;
d.getdata();
d.result();
d.display();
return 0;
}