Visibility Modes and Virtual Function
Visibility Modes and Virtual Function
We can determine how the derived class will inherit the base class and which base class
members of the derived class will be able to access by using visibility modes. Since they
are used to specify the accessibility and visibility level of class members, they are known
as visibility mode or access specifier.
When the derived class objects can utilize a member function in the base class, access
specifiers are mostly employed in inheritance. The following is the standard syntax for
utilizing an access specifier when a child class is derived from the base class:
class Base {
// base class content
};
class Derived: access_specifier Base {
// child's class content
};
In the code above, the access_specifier tells us which base class members will be
obtained and made accessible by the derived class. There is also a base class
called Base and a derived class called Derived.
Multiple public, protected, or private parts are permissible in C++ classes. The code
below demonstrates how visibility modes can be used in a class:
class base {
public:
// public members place
protected:
// protected members place
private:
// private members place
};
Differences between Private, Public, and Protected in C++:
Only the functions inside the The participants of the The base class and its derived classes
base class can access class class that have been made allow access to class members that have
members that have been public are accessible from been designated as protected.
designated as private. anywhere.
The keyword private is used The keyword public is Members that are protected are
to declare private members, used to declare members, identified by using the word protected
followed by the colon (:) followed by the colon (:) and the colon (:) character.
symbol. symbol.
Private members are not In a class, public In a class, protected members can be
passed down across classes. members may be inherited.
inherited.
It offers its data members the It offers no security at all Compared to private access specifier, it
highest level of protection. for the data it holds. offers less security.
A friend function can be Access to public members Protected members are inaccessible
used to access private is possible both with and outside the class and cannot be
members. without the aid of the accessed by the friend function, but they
friend feature. may be accessed by any subclass
(derived class) of that class.
virtual function
o A C++ virtual function is a member function in the base class that you redefine in
a derived class. It is declared using the virtual keyword.
o It is used to tell the compiler to perform dynamic linkage or late binding on the
function.
o There is a necessity to use the single pointer to refer to all the objects of the
different classes. So, we create the pointer to the base class that refers to all the
derived objects. But, when base class pointer contains the address of the derived
class object, always executes the base class function. This issue can only be resolved
by using the 'virtual' function.
o A 'virtual' is a keyword preceding the normal declaration of a function.
o When the function is made virtual, C++ determines which function is to be invoked
at the runtime based on the type of the object pointed by the base class pointer.
#include <iostream.h>
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
int main()
{
A* a; //pointer of base class
B b; //object of derived class
a = &b;
a->display(); //Late Binding occurs
}
Output:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
std::cout << "Derived class is derived from the base class." << std::endl;
}
};
int main()
{
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}
Output:
In the above example, the base class contains the pure virtual function. Therefore, the
base class is an abstract base class. We cannot create the object of the base class.