35 Abstract Class, Pure Virtual Function, Access private members of a class zoom | C++ Programming

Published: 14 March 2021
on channel: tech fort
70
2

#abstract class:
----------------

A Class is a abstract class which contains atleast one Pure/partial virtual function is present is called as abstract Class.

Abstract classes are used to provide an Interface for its sub classes.

Sub-Classes inheriting an abstract class must provide definition(body) to the pure/partial virtual function, otherwise they will also become abstract class.


Characteristics of abstract Class:
----------------------------------
Abstract class cannot be instantiated(object creation is not allowed), but programmer can create pointers and refrences of abstract class type.

normal functions and variables along with a pure virtual function can be present in abstract class.

Purporse: abstract classe are mainly used for Upcasting, so that its derived classes can use its interface.

Sub-Classes inheriting a abstract Class must implement(define the body of function) all pure/partial virtual functions, or else sub-classes also will become abstract too.

in some situation implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. 

Ex:


Late Binding in C++ :
---------------------
In Late Binding function call is resolved at runtime. Hence, now compiler determines the type of object at runtime, and then binds the function call. Late Binding is also called Dynamic Binding or Runtime Binding.


#How to access private methods/functions of derived class:
Ans: using virtual keyword

Programmer can call private function of derived class from the base class pointer with the help of virtual keyword. Compiler checks for access specifier only at compile time. So at run time when late binding occurs it does not check whether we are accessing/calling the private functions or public functions.


#Pure Virtual Function:
pure virtual function are those functions with no definition(no body)(Only Function declarations)

Pure virutal function start with virtual keyword and ends with =0.

virtual returnType functionName() = 0; //Pure Virtaul Function

Ex:
//abstract class
class AbstractDemo{
int id;
.......
public:
virtual float calculateValue()=0; // Pure virtual finction
......
//other member functions / data members

};