AppDividend
Latest Code Tutorials

Abstract Classes in C++ Example | C++ Abstract Class Tutorial

0

Abstract Classes in C++ Example | C++ Abstract Class Tutorial is today’s topic. Before understanding abstract classes in C++, it is essential to know the concepts of virtual functions as it is an integral part of learning the concept of abstract classes in C++. We cannot create the instance of an abstract class. We can create pointer and reference of base abstract class points to the instance of the child class.

Abstract Classes in C++

A class is the abstract if it has at least one pure virtual function. A pure virtual function in C++ can be stated as the function which is only declared in the base class, and it is defined in the derived classes. In simpler words, it can be stated as the functions which are only declared, and it’s not implemented in the base class. Those functions are known as virtual functions.

#The syntax for declaring a virtual function

See the following syntax.

class class_name
{
public:
  virtual return_type function_name() = 0;
};

See the following example.

class A
{
public:
  virtual void fun_1() = 0;
};

Now coming to abstract classes, a class which has at least one pure virtual function is known as abstract class. In other words, if we only declare the function in the base class and doesn’t write its implementation in the base class and we use derived classes for writing an implementation of the declared function, then that class is known as abstract class.

#Properties of abstract classes

  1. Abstract classes cannot have objects.
  2. To be an abstract class, it must have a presence of at least one virtual class.
  3. We can use pointers and references to abstract class type.
  4. If we don’t override the virtual function in the derived class, then the derived class also becomes an abstract class.
  5. We can create constructors of an abstract class.

#Abstract classes in java / Abstract classes in C++

If we want to make a class abstract in java, we use the abstract keyword. Similarly, we can make pure virtual functions to make the class the class abstract. In C++, we don’t need to add an abstract keyword as the base class, which has at least one pure virtual function is understood to be an abstract class.

#Examples of Abstract Class in C++

Write one program to show the mechanism of the abstract class.

#include <iostream>
using namespace std;

class A
{
public:
  virtual void test() = 0; //declaring a virtual function
};

class B : public A
{

public:
  void test()
  {
    cout << "Hello I am the virtual function running in derived class!! :)" << endl;
  }
};

int main(void)
{
  B obj;
  obj.test();
  return 0;
}

See the following output.

 

Abstract Classes in C++ Example

Write a program to use constructors of the abstract class to find the sum of two numbers and display the results.

#include <iostream>
using namespace std;
class A
{
public:
  int a, b, c;
  virtual void test() = 0;
  A(int a, int b)
  {
    cout << "Hello I am the constructor" << endl;
  }
};

class B : public A
{
  int y;

public:
  B(int i, int j) : A(a, b)
  {
    a = i;
    b = j;
    c = a + b;
  }
  void test() { cout << "The sum is = " << c << endl; }
};

int main(void)
{
  B obj(4, 5);
  obj.test();
  return 0;
}

See the following output.

 

C++ Abstract Class Tutorial

Now let’s write the last program. Show that we cannot create the objects of abstract class in C++.

#include <iostream>
using namespace std;
class A
{
public:
  virtual void test() = 0;
};

class B : public A
{
public:
  void test()
  {
    cout << "Hello I will not run!! :(" << endl;
  }
};

int main(void)
{
  A obj;
  return 0;
}

See the following output.

 

C++ Abstract Class

Finally, Abstract Classes in C++ Example | C++ Abstract Class Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.