0% found this document useful (0 votes)
16 views10 pages

OOP Lecture 9

The visibility mode specifies how features of the base class are inherited by the derived class, including public, private, and protected modes. The public mode retains accessibility of all base class members. The private mode makes all base class members private in the derived class. The protected mode makes all base class members protected in the derived class.

Uploaded by

ayesha sabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

OOP Lecture 9

The visibility mode specifies how features of the base class are inherited by the derived class, including public, private, and protected modes. The public mode retains accessibility of all base class members. The private mode makes all base class members private in the derived class. The protected mode makes all base class members protected in the derived class.

Uploaded by

ayesha sabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OBJECT-ORIENTED lecture 9

PARADIGM Instructor : Syeda Hira Fatima


VISIBILITY MODES
The visibility mode specifies how the features of the base class will be inherited by the derived
class. There are three types of visibility modes for all types of Inheritance in C++:
Public Visibility Mode:
In the public visibility mode, it retains the accessibility of all the members of the base class. The
members specified as public, protected, and private in the base class remain public, protected,
and private respectively in the derived class as well. So, the public members are accessible by
the derived class and all other classes. The protected members are accessible only inside the
derived class and its members. However, the private members are not accessible to the derived
class.
class base_class
class derived_class : public base_class
{ {
private:
private: int derived_private;
// int base_private;
//class member protected:
int base_private; int derived_protected;
// int base_protected;
protected: public:
int derived_public;
//class member // int base_public;
};
int base_protected; int main()
public: {
// Accessing members of base_class using object of
//class member the //derived_class:
derived_class obj;
int base_public; obj.base_private; // Not accessible
obj.base_protected; // Not accessible
}; obj.base_public; // Accessible
}
#include<iostream> class derived :public base
using namespace std; {
class base public:
{ void show()
int a; {
public: //cout << "Value of private b
int b; is:" << a; //Error
protected: cout << "Value of protected
int c; variable b is:" << b<<endl;
public: cout << "Value of public
void set_values(int A, int B, int C) variable is: " << c<<endl;
{ }
a = A; };
b = B;
c = C;
cout << a << B << c << "IN base class set int main()
values" << endl; {
} derived d;
void print() base ba;
{ d.set_values(2, 4, 6);
cout << "Value of private variable a is d.show();
displayed using function of base class:" << //ba.set_values(1, 2, 3);
a<<endl; ba.print();
}}; return 0;
}
Private Visibility Mode:
In the private visibility mode, all the members of the base class become private in the
derived class. This restricts the access of these members outside the derived class. They can
only be accessed by the member functions of the derived class. And in this case, the derived
class does not inherit the private members.
class base_class_1
class derived_class : private base_class
{
{ // class definition}; private:
class derived_class: private base_class_1 int derived_private;
// int base_private;
{ // class definition};
// int base_protected;
The following code displays the working of private visibility mode with all three // int base_public
access specifiers of the base class:
protected:
class base_class int derived_protected;
{ private: public:
int derived_public;
//class member
};
int base_private; int main()
protected: {
// Accessing members of base_class
//class member
using object of the derived_class:
int base_protected; derived_class obj;
public: obj.base_private; // Not accessible
obj.base_protected; // Not accessible
//class member
obj.base_public; // Not Accessible
int base_public;}; }
#include<iostream>
void show()
using namespace std; {
//cout << "Value of private b is:" << a; //Error
class base{ int a; cout << "Value of protected variable b is:" << b<<endl;
public: int b; cout << "Value of public variable is: " << c<<endl;
}
protected: int c; };
public: void print()
{cout << "Value of private variable a is displayed int main()
using function of base class:" << a<<endl;} {
}; derived d;
base ba;
class derived :private base d.set_values(4, 6);
{public: void set_values(int B, int C) d.show();
//ba.set_values(1, 2, 3);
{//a = A;b = B;c = C; ba.print();
return 0;
cout << B << c << "IN base class set values" <<
endl; }

}
Protected Visibility Mode:
In the protected visibility mode, all the members of the base class become protected
members of the derived class. These members are now only accessible by the derived
class and its member functions. These members can also be inherited and will be
accessible to the inherited subclasses. However, objects of the derived classes cannot
access these members outside the class.
class base_class int main()
{private: //class member {
// Accessing members of base_class
int base_private; using object of the derived_class:
protected: //class member derived_class obj;
int base_protected; obj.base_private; // Not accessible
public: //class member obj.base_protected; // Not accessible
obj.base_public; // Not Accessible
int base_public;}; }
class derived_class : protected base_class
{private:
int derived_private;
// int base_private;
protected:
int derived_protected;
// int base_protected;
// int base_public
public:
int derived_public;
};
class base
{ int a; int main()
{
public: int b; derived d;
protected: int c; base ba;
d.set_values(4, 6);
Public: void set_values(int B, int C) d.show();
//ba.set_values(1, 2, 3);
{//a = A;b = B;c = C;
ba.print();
cout << B << c << "IN base class set values" << endl;} cout << "Please enter a value for PC:";
cin >> d.deblic;
void print(){cout << "Value of private variable a is displayed using function //cin >> d.pc;
of base class:" << a<<endl;}};
return 0;
class derived :private base }
{public: int deblic;
protected: int pc;
public:void show()
{//cout << "Value of private b is:" << a; //Error
cout << "Value of protected variable b is:" << b<<endl;
cout << "Value of public variable is: " << c<<endl; }};

You might also like