-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_inheritance.cpp
52 lines (47 loc) · 1.27 KB
/
19_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 {
public:
int id ;
float salary;
Employee(){} // Constructer of base class impilicity called by declaration of derived class
Employee(int inpId){
id = inpId;
salary = 12;
}
void data(){
cout << "Employee ID :"<<id
<<"\nEmployee Salary:"<<salary<<endl<<endl;
}
};
class Programmer : public Employee{
int langcode;
public:
Programmer(int inpId){
id=inpId;
salary = 20;
langcode=4;
}
void data(){
cout << "Programmer ID :"<<id
<<"\nProgrammer Salary:"<<salary<<endl;
cout<<"Language Code of programmer is : "<<langcode<<endl;
}
};
int main ()
{
Employee Mohit(55),Hardik(44);
Mohit.data();
Hardik.data();
Programmer hardik(44);
// cout<<"Langcode "<<hardik.langcode; private member
hardik.data();
return 0;
}
/*
Inheritance : reusability in cpp
- a class that inherits another class is known as derived or subclass
- the class from which it inherits is called base or superclass or parent
Principle -
D R Y - Do not Repeat Yourself
*/