Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 96a49c1

Browse files
Introduction to inheritance in C++
1 parent 68099f3 commit 96a49c1

File tree

1 file changed

+53
-0
lines changed
  • Learn_CPP_Programming_Deep_Dive/Section 13 Inheritance/Introduction_to_inheritance

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
6+
class Base
7+
{
8+
public:
9+
int age;
10+
void show()
11+
{
12+
cout<<age<<endl;
13+
}
14+
15+
/* *******************************************************************************************************************************************************
16+
The age member of the Base class is public, so it can be accessed directly from an object of the Base class. However, when
17+
it comes to the Derived class, which inherits from Base, the age member is not directly accessible because the inheritance is private by default in C++.
18+
********************************************************************************************************************************************************* */
19+
20+
21+
};
22+
23+
/* **************************************************************************************************************************************************
24+
25+
In C++, if you do not specify an access specifier (`public`, `protected`, or `private`) when declaring a derived class, the inheritance is `private` by default.
26+
This means that public and protected members of the base class become private members of the derived class.
27+
*************************************************************************************************************************************************** */
28+
29+
class Derived: public Base
30+
{
31+
public:
32+
int weight;
33+
void display()
34+
{
35+
cout<<age<<" "<<weight<<endl;
36+
}
37+
};
38+
39+
int main()
40+
{
41+
Base b;
42+
b.age = 12;
43+
b.show();
44+
45+
Derived d;
46+
d.age = 45;
47+
d.weight = 96;
48+
d.show();
49+
d.display();
50+
51+
return 0;
52+
53+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /