Wednesday 22 July 2020

Inheritance Of Classes in C++

// Derived Class syntax
/*
class {{derived-class-name}} : {{visibility-mode}} {{base-class-name}}
{
    class members/methods/etc...
}
Note:
1. Default visibility mode is private
2. Public Visibility Mode: Public members of the base class becomes Public members of the derived class
3. Private Visibility Mode: Public members of the base class becomes Private members of the derived class
4. Private members are never inherited
*/
Code:
#include<iostream>
using namespace std;

class employee{
	
	public:
		employee(){} //default constructor for employee class 
		float salary;
		int id;
		employee(int pid){
			id=pid;
			salary=40;
		}
};

//class that is inherited from base class Employee
// public is visibilty mode of dirve class by default it is private
class programmer: public employee{
	public:
		int language;
		programmer(int pid){
			id=pid;
			language=20;
		}
};

int main(){
	employee margaish(40);
	cout<<margaish.salary<<endl;
	cout<<margaish.id<<endl;
	programmer ARM(20);
	cout<<ARM.id<<endl;
	cout<<ARM.salary<<endl;
	return 0;
}

No comments:

Post a Comment