Skip to content

Commit 7589599

Browse files
committed
Passing arguments to Base Class Constructors
1 parent bdedfe7 commit 7589599

File tree

1 file changed

+61
-0
lines changed
  • Section15_Inheritance/Passing_Arguments_to_Base_Class_Constructors

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//PASSING ARGUMENTS TO BASE CLASS CONSTRUCTORS
2+
3+
#include <iostream>
4+
5+
using namespace std;
6+
7+
class Base
8+
{
9+
//attributes
10+
private:
11+
int my_value;
12+
//methods
13+
public:
14+
Base():my_value{0}
15+
{
16+
cout<<"Base No-args constructor "<<endl;
17+
}
18+
19+
Base(int x): my_value{x}
20+
{
21+
cout<<"int Base constructor "<<endl;
22+
}
23+
24+
~Base()
25+
{
26+
cout<<"Base destructor"<<endl;
27+
}
28+
};
29+
30+
class Derived : public Base
31+
{
32+
//attributes
33+
private:
34+
int my_double_value;
35+
//methods
36+
public:
37+
Derived()
38+
:Base{},my_double_value{0}
39+
{
40+
cout<<"Derived no-args constructor "<<endl;
41+
}
42+
43+
Derived(int x)
44+
: Base{x},my_double_value{2*x}
45+
{
46+
cout<<"Int Derived constructor "<<endl;
47+
}
48+
49+
~Derived()
50+
{
51+
cout<<"Derived destructor"<<endl;
52+
}
53+
};
54+
55+
int main()
56+
{
57+
//Derived d;
58+
Derived d{100};
59+
60+
return 0;
61+
}

0 commit comments

Comments
 (0)