100% found this document useful (1 vote)
1K views

Abstract Class in C++

An abstract class is used when a class has some but not all methods implemented. It contains at least one pure virtual function defined with "=0". Pure virtual functions do not have a body and must be implemented in derived classes. We cannot create objects of an abstract class since it contains undefined pure virtual functions, but derived classes that implement these functions can be instantiated. The example shows a BaseClass declared as abstract with two pure virtual functions Display1 and Display2. DerivedClass inherits from BaseClass and implements these functions, allowing an object to call all methods.

Uploaded by

Ashutosh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

Abstract Class in C++

An abstract class is used when a class has some but not all methods implemented. It contains at least one pure virtual function defined with "=0". Pure virtual functions do not have a body and must be implemented in derived classes. We cannot create objects of an abstract class since it contains undefined pure virtual functions, but derived classes that implement these functions can be instantiated. The example shows a BaseClass declared as abstract with two pure virtual functions Display1 and Display2. DerivedClass inherits from BaseClass and implements these functions, allowing an object to call all methods.

Uploaded by

Ashutosh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ Abstract Class

Abstract class is used in situation, when we have partial set of implementation of methods in a
class. For example, consider a class have four methods. Out of four methods, we have an
implementation of two methods and we need derived class to implement other two methods. In
these kind of situations, we should use abstract class.

A virtual function will become pure virtual function when you append "=0" at the end of
declaration of virtual function.

A class with at least one pure virtual function or abstract function is called abstract class.

Pure virtual function is also known as abstract function.

 We can't create an object of abstract class b'coz it has partial implementation of methods.
 Abstract function doesn't have body
 We must implement all abstract functions in derived class.

Example of C++ Abstract class

#include<iostream.h>
#include<conio.h>

class BaseClass //Abstract class


{

public:
virtual void Display1()=0; //Pure virtual function or
abstract function
virtual void Display2()=0; //Pure virtual function or
abstract function

void Display3()
{
cout<<"\n\tThis is Display3() method of Base Class";
}

};

class DerivedClass : public BaseClass


{

public:
void Display1()
{
cout<<"\n\tThis is Display1() method of Derived Class";
}

void Display2()
{
cout<<"\n\tThis is Display2() method of Derived Class";
}

};

void main()
{

DerivedClass D;

D.Display1(); // This will invoke Display1() method


of Derived Class
D.Display2(); // This will invoke Display2() method
of Derived Class
D.Display3(); // This will invoke Display3() method
of Base Class

Output :

This is Display1() method of Derived Class


This is Display2() method of Derived Class
This is Display3() method of Base Class

You might also like