-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.cpp
52 lines (44 loc) · 1.26 KB
/
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
#include<iostream>
using namespace std;
class Employee
{
protected: // hidden but not for sub class
string Name;
string Company;
int Age;
public:
Employee(string name, string company, int age) // parameterised constructor
{
Name = name;
Company = company;
Age = age;
}
void AskForPromotion()
{
if(Age>=30)
cout << Name << " got promoted!" << endl;
else
cout << Name << ", sorry. No promotion for you!" << endl;
}
};
class Developer:public Employee // sub class of Employee (super class)
{
public:
string FavProgrammingLanguage;
Developer(string name, string company, int age, string favProgrammingLanguage) // as parent class has constructor we need to have the same in child class also
:Employee(name,company,age)
{
FavProgrammingLanguage = favProgrammingLanguage;
}
void FixBug()
{
cout << Name << " is fixing bug using " << FavProgrammingLanguage << endl; // can't use Name if access modifier: private, we can use protected instead
}
};
int main()
{
Developer d = Developer("Urvi", "Google", 21, "C++");
d.FixBug();
d.AskForPromotion();
return 0;
}