Access specifier

We can inherit the two classes by using three access specifiers. access specifiers is also known as visibility mode. when a class is derived from an existing class, all the members of base class is automatically inherited in the derived class. but we can restrict access to data members of base class in derived class by using the access specifiers. the three access specifiers is:

more:

1.Public access specifier

2.Private access specifier

3.Protected access specifier

                                               

Program:

/*PROGRAM TO IMPLEMENT MULTIPLE INHERITANCE BY USING PRIVATE AND PROTECTED VISIBILITY MODE*/

#include<conio.h>

#include<iostream.h>

class base1

{

char name[15];

int age;

public:

base1()

{

cout<<“Enter name: “;

cin>>name;

cout<<“Enter age: “;

cin>>age;

}

void show1()

{

cout<<“Name is: “<<name<<endl;

cout<<“age is: “<<age<<endl;

}

~base1()

{

cout<<“Destructor of first base class is

executed”<<endl;

}

};

class base2

{

float salary;

char des[5];

public:

base2()

{

cout<<“Enter salary: “;

cin>>salary;

cout<<“Enter designation: “;

cin>>des;

}

void show2()

{

cout<<“Salary is: “<<salary<<endl;

cout<<“Designation is: “<<des<<endl;

}

~base2()

{

cout<<“Destructor of second base class is

executed”<<endl;

}

};

class derived:private base1,protected base2

{

public:

derived():base1(),base2()

{

}

void show3()

{

show1();

show2();

}

~derived()

{

cout<<“Destructor of derived class is executed”<<endl;

}

};

void main()

{

clrscr();

{

derived d;

d.show3();

}

getch();

}

Leave a Reply

Your email address will not be published. Required fields are marked *