C++ Data Types C++ Operators C++Please LoginC++
Input/Output ToControl
Continue
Statements C++ Functions ×
C++ Strings C+
Pure Virtual Functions
Sign In and Abstract Classes in
Sign Up
C++
Readaccount_circle Discuss
UsernameCourses
or email Practice
Sometimes Password
implementation of all functions cannot be provided in a base
lock
class because we don’t know the implementation. Such a class is called an
Remember me
abstract class.For example, let Shape be a base class. We cannot provide
Forgot Password
the implementation of function draw()
SigninInShape, but we know every derived
class must have an implementation of draw(). Similarly, an Animal class
or
doesn’t have the implementation of move() (assuming that all animals
move),
but all animals must know how to move.
Google We cannot create objects of
Facebook
abstract classes.
A pure LinkedIn
virtual function in C++ is aGitHub
(or abstract function) virtual function for
Why Create an Account?
which we can have an implementation, But we must override that function in
By creating this account, you agree to our Privacy Policy & Cookie Policy.
the derived class, otherwise, the derived class will also become an abstract
class. A pure virtual function is declared by assigning 0 in the declaration.
Example of Pure Virtual Functions
C++
// An abstract class
class Test {
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
Complete Example
We use cookies to ensure you have the best browsing experience on our website. By using our site,
Got It !
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
A pure virtual function is implemented by classes that are derived from an
Abstract class.
Please Login To Continue ×
C++
Sign In Sign Up
// C++ Program to illustrate the abstract class and virtual
// functions
#include <iostream>
account_circle
using namespace std;
class Base {
lock
// private member variable
int x;
Remember me
Forgot Password
public:
// pure virtual function
virtual void fun() = 0;
or
// getter function to access x
int getX() { return x; }
};
Google Facebook
// This class inherits from Base and implements fun()
LinkedIn GitHub
class Derived : public Base {
// private member variableWhy Create an Account?
int y; By creating this account, you agree to our Privacy Policy & Cookie Policy.
public:
// implementation of the pure virtual function
void fun() { cout << "fun() called"; }
};
int main(void)
{
// creating an object of Derived class
Derived d;
// calling the fun() function of Derived class
d.fun();
return 0;
}
Output
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Master C++ Programming - Complete
Beginner to Advanced
Please Login To Continue
C++ or CPP is a general-purpose programming language
×
and acts as a stepping stone into the world of...
Sign In 35+ hours of content
Sign Up
149k+ views
Mentorship by Experts LEARN MORE
account_circle
lock
Remember me
fun() called Forgot Password
Some Interesting Facts
or
1. A class is abstract if it has at least one pure virtual function.
Google Facebook
Example
C++ code, Test is an abstract class
In the below
LinkedIn because it has a pure virtual GitHub
Why Create an Account?
function show().
By creating this account, you agree to our Privacy Policy & Cookie Policy.
C++
// C++ program to illustrate the abstract class with pure
// virtual functions
#include <iostream>
using namespace std;
class Test {
// private member variable
int x;
public:
// pure virtual function
virtual void show() = 0;
// getter function to access x
int getX() { return x; }
};
int main(void)
{
// Error: Cannot instantiate an abstract class
We use cookies to ensure you have the best browsing experience on our website. By using our site,
Test t;
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
return 0;
}
Please Login To Continue ×
Output
Sign In Sign Up
Compiler Error: cannot declare variable 't' to be of abstract
type 'Test' because the following virtual functions are pure
within 'Test': note: virtual void Test::show()
account_circle
2. We can have pointers and references of abstract class type.
lock
For example, the following program works fine.
Remember me
Forgot Password
C++
// C++ program that demonstrate that or
// we can have pointers and references
// of abstract class type.
Google Facebook
#include <iostream>
using namespace std;
LinkedIn GitHub
class Base { Why Create an Account?
public: By creating this account, you agree to our Privacy Policy & Cookie Policy.
// pure virtual function
virtual void show() = 0;
};
class Derived : public Base {
public:
// implementation of the pure virtual function
void show() { cout << "In Derived \n"; }
};
int main(void)
{
// creating a pointer of type
// Base pointing to an object
// of type Derived
Base* bp = new Derived();
// calling the show() function using the
// pointer
bp->show();
return 0;
}
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Output
In Derived Please Login To Continue ×
3. If we do not override the pure virtual function in the derived class, then
Sign Up
the derived classSign
alsoInbecomes an abstract class.
The following example demonstrates the same.
account_circle
C++
lock
// C++ program to demonstrate that if we do not override
// the pure virtual
Remember me function in the derived class, then
// the derived class also becomes an abstract class Forgot Password
#include <iostream>
using namespace std;
or
class Base {
public: Google Facebook
// pure virtual function
virtual void show() = 0;
}; LinkedIn GitHub
Why Create an Account?
class Derived : public Base {
By creating this account, you agree to our Privacy Policy & Cookie Policy.
};
int main(void)
{
// creating an object of Derived class
Derived d;
return 0;
}
Output
Compiler Error: cannot declare variable 'd' to be of abstract type
'Derived' because the following virtual functions are pure within
'Derived': virtual void Base::show()
4. An abstract class can have constructors.
For example, the following program compiles and runs fine.
C++
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
// C++ program to demonstrate that
// an abstract class can Please
have constructors.
Login To Continue ×
#include <iostream>
using namespace std;
Sign In Sign Up
// An abstract class with constructor
class Base {
protected:
account_circle
// protected member variable
int x;
public:
lock
// pure virtual
Remember me function
virtual void fun() = 0; Forgot Password
// constructor of Base class
Base(int i)
{ or
x = i;
Google
cout << "Constructor of base called\n"; Facebook
}
};
LinkedIn GitHub
class Derived : public Base { Why Create an Account?
// private member variable
int y; By creating this account, you agree to our Privacy Policy & Cookie Policy.
public:
// calling the constructor of Base class
Derived(int i, int j)
: Base(i)
{
y = j;
}
// implementation of pure virtual function
void fun()
{
cout << "x = " << x << ", y = " << y << '\n';
}
};
int main(void)
{
// creating an object of Derived class
Derived d(4, 5);
// calling the fun() function of Derived class
d.fun();
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
// creating an object of Derived class using
// a pointer of the Base class
Base* ptr = new Derived(6, 7);
Please Login To Continue
// calling the fun() function using the ×
// pointer
ptr->fun();
Sign In Sign Up
return 0;
}
account_circle
Output
lock
Constructor of base called
x = 4, Remember
y = 5 me
Forgot Password
Constructor of base called
x = 6, y = 7
or
5. An abstract class in C++ can also be defined using struct keyword.
Example Google Facebook
struct shapeClassLinkedIn
GitHub
{
Why Create an Account?
virtual void Draw()=0;
By creating this account, you agree to our Privacy Policy & Cookie Policy.
}
Comparison with Java
In Java, a class can be made abstract by using an abstract keyword. Similarly,
a function can be made pure virtual or abstract by using an abstract
keyword. See Abstract Classes in Java for more details.
Interface vs Abstract Classes
An interface does not have an implementation of any of its methods, it can
be considered as a collection of method declarations. In C++, an interface
can be simulated by making all methods pure virtual. In Java, there is a
separate keyword for the interface.
We can think of Interface as header files in C++, like in header files we only
provide the body of the class that is going to implement it. Similarly in Java
in Interface we only provide the body of the class and we write the actual
We use cookies to ensure you have the best browsing experience on our website. By using our site,
code in whatever class implements it.
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy