-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36inher_program.cpp
More file actions
70 lines (64 loc) · 1.92 KB
/
Copy path36inher_program.cpp
File metadata and controls
70 lines (64 loc) · 1.92 KB
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
70
#include <iostream>
using namespace std;
class employee
{
public:
int id;
float salary;
employee(int idn)
{
id = idn;
salary = 100.23;
}
employee() {} // a default base constructor is necessary to allow derived constructors fetch values from here
~employee(){}
};
/*
derived class syntax
class {{derived class name}} : {{visibility mode}} {{base class name}}
{
members/ methods etc
};
visibility mode : It could either be public or private
If it is private, the public members of the base class will be the private members of this derived class
If it is public, the public members of the base class will be the public members of this derived class
By default, a derived class visibility is private if not mentioned
private member of the base class are unheritable in any case
*/
class programmer : employee
{
int experience;
public:
int languageCode = 9;
programmer(int idp) //this constructor calls the default base class constructor
{
id = idp;
}
~programmer(){cout<<"End of file"<<endl;}
void showData()
{
cout<<id<<endl;
}
};
class manager : public employee
{
public:
manager(int ids)
{
id = ids;
}
};
int main()
{
employee vasu(1), dev(2);
cout << "Vasu - ID " << vasu.id << " Salary : " << vasu.salary << endl;
cout << "Dev - ID " << dev.id << " Salary : " << vasu.salary << endl;
programmer singh(3);
cout << singh.languageCode << endl;
//cout << "Singh - ID " << singh.id << endl; this doesn't work as they are private for programmer derived class
cout<<"Singh ID : ";
singh.showData(); //by using class function, we can display or access base class members in derived class objects
manager khan(4);
cout<<"Khan - ID "<<khan.id<<" Salary : "<<khan.salary<<endl; //this will not access the constructor of base class
return 0;
}