-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_single_inheritance.cpp
65 lines (51 loc) · 1.16 KB
/
02_single_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
// // Write a C++ program to add two numbers using single inheritance. Accept and add these two numbers in base class and display the sum of these two numbers in derived class.
// // Header files
#include <iostream>
// // use namespace
using namespace std;
// // define class Base
class Base
{
private:
// // instance member variables
int num1, num2;
protected:
int sum;
public:
// // instance member function to set numbers
void setNumbers(int num1, int num2)
{
this->num1 = num1;
this->num2 = num2;
}
// // instance member function to add numbers
void addNumbers()
{
sum = num1 + num2;
}
};
// // define class Derived by inheriting class Base
class Derived : public Base
{
public:
void displaySum()
{
cout << "\nSum of Numbers => " << sum;
}
};
// // Main Function Start
int main()
{
// // create an instance of Derived class
Derived d1;
// // set numbers
d1.setNumbers(5, 15);
// // add numbers
d1.addNumbers();
// // display sum of numbers
d1.displaySum();
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End