-
Notifications
You must be signed in to change notification settings - Fork 0
/
33_Inheritance-Syntax-and-Visibility-Mode.cpp
69 lines (61 loc) · 1.51 KB
/
33_Inheritance-Syntax-and-Visibility-Mode.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
#include <iostream>
using namespace std;
// Base class
class Employee
{
private:
public:
int id;
float salary;
Employee(int inpId)
{
id = inpId;
salary = 3432.00;
}
Employee() {}
};
// Derived Class Syntax
/*
class {{derived-class-name}} : {{visibility-mode}} {{base-class-name}}
{
class members/methods/etc...
}
NOTE:
1. Default vidsiblility mode is private.
2. Private visibility Mode : Public members of the base class becomes private members of the derived class.
3. Public visibility Mode : Public members of the base class becomes public members of the derived class.
4. Private members are never inherited.
*/
// Creating a Programmer class derived from Employee Base Class
class Programmer : Employee
{ // When we make a derived class , automatically default constructor of base class is called so we have to make a default constructor also
public:
string languageCode;
Programmer(int inpId)
{
id = inpId;
languageCode = "C, C++";
}
void getData()
{
cout << id << endl;
}
};
int main()
{
Employee roshan(46), shobhan(55);
cout << roshan.salary << endl;
cout << shobhan.salary << endl;
Programmer skillF(6);
cout << skillF.languageCode << endl;
skillF.getData();
// cout<<skillF.id; // This will not work if inherited privately , only work for - " class Programmer : public Employee "
return 0;
}
/*
OUTPUT
3432
3432
C, C++
6
*/