0% found this document useful (0 votes)
25 views

Visibility Modes and Virtual Function

The document discusses visibility modes in C++, specifically private, public, and protected access specifiers. It explains that access specifiers define the limitations on accessing class members and are used to specify the accessibility and visibility of class members. The document also provides details on the differences between private, public, and protected access specifiers.

Uploaded by

ushapal70413
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Visibility Modes and Virtual Function

The document discusses visibility modes in C++, specifically private, public, and protected access specifiers. It explains that access specifiers define the limitations on accessing class members and are used to specify the accessibility and visibility of class members. The document also provides details on the differences between private, public, and protected access specifiers.

Uploaded by

ushapal70413
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Visibility Modes in C++

One of the key components of object-oriented programming in C++ is data hiding,


which enables us to conceal internal object characteristics, such as data members, and
prohibits program functions from directly accessing an object's internal
representation, data members, and member functions.

Access modifiers define the limitations on access to class member functions. In


C++, visibility modes, such as private, public, and protected, are also referred to
as access modifiers. In the following sections of this article, we will examine each of them
in further detail.

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++:

Private Public Protected

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.

Late binding or Dynamic linkage


In late binding function call is resolved during runtime. Therefore, compiler determines
the type of object at runtime, and then binds the function call.

Rules of Virtual Function

o Virtual functions must be members of some class.


o Virtual functions cannot be static members.
o They are accessed through object pointers.
o They can be a friend of another class.
o A virtual function must be defined in the base class, even though it is not used.
o The prototypes of a virtual function of the base class and all the derived classes
must be identical. If the two functions with the same name but different prototypes,
C++ will consider them as the overloaded functions.
o We cannot have a virtual constructor, but we can have a virtual destructor
o Consider the situation when we don't use the virtual keyword.
C++ virtual function Example
Let's see the simple example of C++ virtual function used to invoked the derived class in
a program.

#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:

Derived Class is invoked


Pure Virtual Function
o A virtual function is not used for performing any task. It only serves as a
placeholder.
o When the function has no definition, such function is known as "do-nothing"
function.
o The "do-nothing" function is known as a pure virtual function. A pure virtual
function is a function declared in the base class that has no definition relative to
the base class.
o A class containing the pure virtual function cannot be used to declare the objects
of its own, such classes are known as abstract base classes.
o The main objective of the base class is to provide the traits to the derived classes
and to create the base pointer used for achieving the runtime polymorphism.

Pure virtual function can be defined as:

virtual void display() = 0;

Let's see a simple example:

#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:

Derived class is derived from the base class.

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.

You might also like