KEMBAR78
Pure Virtual Functions and Abstract Classes in C | PDF | Class (Computer Programming) | C++
0% found this document useful (0 votes)
12 views7 pages

Pure Virtual Functions and Abstract Classes in C

The document explains the concept of pure virtual functions and abstract classes in C++. An abstract class cannot be instantiated and must have at least one pure virtual function, which derived classes must implement. It also discusses the ability to use pointers and references of abstract class types and the implications of not overriding pure virtual functions in derived classes.

Uploaded by

gp3837730
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Pure Virtual Functions and Abstract Classes in C

The document explains the concept of pure virtual functions and abstract classes in C++. An abstract class cannot be instantiated and must have at least one pure virtual function, which derived classes must implement. It also discusses the ability to use pointers and references of abstract class types and the implications of not overriding pure virtual functions in derived classes.

Uploaded by

gp3837730
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Pure Virtual Functions and Abstract Classes in C++

Sometimes implementation of all functions cannot be provided in a base class because


we don’t know the implementation. Such a class is called an abstract class. For
example, let Shape be a base class. We cannot provide the implementation of function
draw() in Shape, but we know every derived class must have an implementation of draw().
Similarly, an Animal class doesn’t have the implementation of move() (assuming that all
animals move), but all animals must know how to move. We cannot create objects of
abstract classes.

A pure virtual function (or abstract function) in C++ is a virtual function for which we can
have an implementation, But we must override that function in 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.

SYNTAX

// An abstract class
class Test {
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;

/* Other members */
};

A pure virtual function is implemented by classes that are derived from an Abstract
class.

/ C++ Program to illustrate the abstract class and virtual functions


#include <iostream>
using namespace std;

class Base {
// private member variable
int x;
public:
// pure virtual function
virtual void fun() = 0;

// getter function to access x


int getX() { return x; }
};

// This class inherits from Base and implements fun()


class Derived : public Base {
// private member variable
int y;

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
fun() called
Some Interesting Facts
1. A class is abstract if it has at least one pure virtual function.
Example
In the below C++ code, Test is an abstract class because it has a pure virtual function
show().

SYNTAX

// 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
Test t;

return 0;
}

Output

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()
2. We can have pointers and references of abstract class type.
For example, the following program works fine.
// C++ program that demonstrate that we can have pointers and references
//of abstract class type.

#include <iostream>
using namespace std;

class Base {
public:
// 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;
}

Output
In Derived
3. If we do not override the pure virtual function in the derived class, then the derived
class also becomes an abstract class.

The following example demonstrates the same.

// C++ program to demonstrate that if we do not override


// the pure virtual function in the derived class, then
// the derived class also becomes an abstract class

#include <iostream>
using namespace std;

class Base {
public:
// pure virtual function
virtual void show() = 0;
};

class Derived : public Base {


};

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++ program to demonstrate that


// an abstract class can have constructors.

#include <iostream>
using namespace std;

// An abstract class with constructor


class Base {
protected:
// protected member variable
int x;

public:
// pure virtual function
virtual void fun() = 0;

// constructor of Base class


Base(int i)
{
x = i;
cout << "Constructor of base called\n";
}
};

class Derived : public Base {


// private member variable
int y;

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();

// creating an object of Derived class using


// a pointer of the Base class
Base* ptr = new Derived(6, 7);

// calling the fun() function using the


// pointer
ptr->fun();

return 0;
}

Output
Constructor of base called
x = 4, y = 5
Constructor of base called
x = 6, y = 7

You might also like