-
Notifications
You must be signed in to change notification settings - Fork 1
/
Inheritance_Base_Derived_class_2.cpp
65 lines (58 loc) · 1.18 KB
/
Inheritance_Base_Derived_class_2.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
// author: jaydattpatel
#include<iostream>
using namespace std;
// Base class
class PAIR
{
private:
int a,b;
public:
PAIR(int,int);
void show(){
cout<<"Private a: "<<a<<"\tb: "<<b<<endl;
}
};
// Base class constructor
PAIR::PAIR(int x,int y)
{
a = x;
b = y;
cout<<"\nData copied into private variables\n";
};
// Derived class
class sumPair : public PAIR
{
public:
int sum;
sumPair(int,int);
};
sumPair::sumPair(int p,int q):PAIR(p, q) // here constructor called and copied into private variable
{
sum =p+q;
}
/* Class PAIR has already been
* declared and defined with the
* following constructor:
*
* PAIR(int,int)
*
* that stores its two arguments in
* two private member variables of PAIR.
*
* Class sumPair has also already been
* defined as follows:
*
*
* Implement the constructor
* sumPair(int,int) such that it
* loads the two member variables of
* the base PAIR class with its
* arguments, and initializes the
* member variable sum with their sum.
*/
int main() {
sumPair sp(15,16);
cout << "sp(15,16).sum =" << sp.sum << endl;
sp.show();
return 0;
}