Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism

Abstract Base Classes and Concrete Classes

C++ Program

Abstract class is a class which is mostly used as a base class. It contains at least one pure virtual function. Abstract classes can be used to specify an interface that must be implemented by all subclasses.

Abstract Base Classes and Concrete Classes

 

• Definition : Abstract class is a class which is mostly used as a base class. It contains at least one pure virtual function. Abstract classes can be used to specify an interface that must be implemented by all subclasses.

• The virtual function is function having nobody but specified by = 0. This tells the compiler that nobody exists for this function relative to the base class. When a virtual function is made pure, it forces any derived class to override it. If a derived class does not, an error occurs. Thus, making a virtual function pure is a way to guarantee that a derived class will provide its own redefinition.

For example

#include<iostream>

using namespace std;

class area

{

double dim1, dim2;

public:

void setarea(double d1, double d2)

{

   dim1 = d1;

   dim2 = d2;

}

void getdim(double &d1, double &d2)

{

   d1 = dim1;

   d2 = dim2;

}

}

void getdim(double &d1, double &d2)

{

   d1= dim1;

   d2 = dim2;

}

virtual double getarea() = 0; // pure virtual function

};

class square: public area

{

public:

double getarea()

{

   double d1, d2;

   getdim(d1, d2);

   return d1 * d2;

}

};

class triangle: public area

{

public:

double getarea()

{

   double d1, d2;

   getdim(d1, d2);

   return 0.5 * d1 * d2;

}

};

void main()

{

   area p;

   square s;

   triangle t;

   int num1,num2;

   cout<<"\n Enter The two dimensions for calculating area of square";

   cin>>num1>>num2;

   s.setarea(num1,num2);

   p = &s;

   cout << "Area of square is : " << p‒>getarea() << '\n';

   cout<<"\n Enter The two dimensions for calculating area of triangle";

   cin>>num1>>num2;

   t.setarea(num1,num2);

   p=&t;

   cout << "Area of Triangle is: " << p‒>getarea() << '\n';

}

Output

Enter The two dimensions for calculating area of square10 20

Area of square is: 200

Enter The two dimensions for calculating area of triangle6

8

Area of Triangle is: 24

• In above code the getarea() is a function which is defined as pure virtual function in base class. But it is redefined in derived class square and triangle. In derived class square the getarea() function calculates the area of square and in derived class triangle the getarea() function calculates the area of triangle. The definition of getarea in base class is overridden by the definitions of functions in derived class. The class area acts as an abstract class because –

• It specifies an interface which is used by all the derived classes. Thus it is never used directly it simply gives skeleton to other derived classes.

• It contains one pure virtual function getarea().

 

Difference between Abstract class and Concrete class


Abstract class

1. Abstract class is a class that is declared with the keyword abstract.

2. The abstract class can not be instantiated. But we can inherit the abstract class.

3. At least one method must be abstract in abstract class.

4. In the application class hierarchy, abstract class is at the first level

Concrete class

1. Only the keyword class is used for declaration of concrete class.

2. The concrete class can be instantiated as well as inherited.

3. No method should be abstract in concrete class.

4. In application class hierarchy, the class can be at any level.

 

Example: 1

What are abstract classes? Write a program having student as an abstract class and create many derived classes such as engineering, science, medical etc. from the student class. Create their object and process them.

Solution :

An abstract class in C++ is a class that cannot be instantiated (we cannot create its objects) and contains at least one pure virtual function.

#include<iostream>

#include<cstring>

using namespace std;

class Student

{

   char name[10];

public:

void SetName(char n[10])

{

   strcpy(name,n);

}

void GetName(char n[10])

{

   strcpy(n,name);

}

virtual void qualification()=0;

}:

class Engg:public Student

{

public:

void qualification()

{

   char n[10];

   GetName(n);

   cout<<n<<" is a an engineering student"<<endl;

}

};

class Medical:public Student

{

public.

void qualification()

{

   char n[10];

   GetName(n);

   cout<<n<<<<" is a medical student"<<endl;

}

};

void main()

{

Student *s;

Engg e;

Medical m;

char nm[10];

cout<<"\n Enter the name: "<<endl;

cin>>nm;

e.SetName(nm);

s=&e;

s‒>qualification();

cout<<"\n Enter the name: "<<endl;

cin>> nm;

m.SetName(nm);

s=&m;

s‒>qualification();

}

Output

Enter the name:

Ramesh

Ramesh is a an engineering student

Enter the name:

Suresh

Suresh is a medical student

 

Example: 2

Define an abstract class called Employee. Derive two classes called Hourly Employee and Salaried Employee. Hourly Employee has number of hours(integer) and wage per hour(float).

Salaried Employee has salary of type float. Calculate salary() in an abstract function in class employee which is inherited by derived classes. Write main program to create objects of all classes and calculate salary of each object.

Solution :

#include <iostream>

using namespace std;

class Employee

{

public:

   virtual float calculate_ salary() = 0;

};

class HourlyEmployee: public Employee

{

private:

   int Hours;

   float WagePerHour;

public:

void setHData()

{

   cout << "\n Enter the number of Hours: ";

   cin>> Hours:

   cout << "\n Enter the Wages per Hour: ";

   cin>> WagePerHour,

}

float calculate _salary())

{

   float salary;

   salary =Hours *WagePerHour;

   return salary;

}

};

class SalariedEmployee : public Employee

{

float HRA, DA, Basics;

float tax;

public:

void setSData()

{

   cout<<"\n Enter Basic payment: ";

   cin >> Basics;

   cout << "\n Enter HRA: ";

   cin>> HRA;

   cout<<"\n Enter DA: ";

   cin >> DA:

   cout << "\n Enter tax: ";

   cin>> tax;

}

float calculate_salary()

{

   float salary;

   salary = (Basics + DA + HRA) ‒ tax;

   return salary;

}

};

int main()

{

SalariedEmployee sEmp;

HourlyEmployee hEmp;

int choice;

char ans = 'y';

do

{

   cout << "\n 1. Salaried Employee \n 2. Hourly Employee \n 3. Exit";

   cout << "\n Enter your choice:

   cin>> choice:

   switch (choice)

   {

      case 1: sEmp.setSData();

          cout << "\n The salary of Salaried Employee is: ";

          cout << sEmp.calculate_salary() << endl;

          break;

      case 2:hEmp.setHData();

          cout << "\n The salary of Hourly Employee is: ";

          cout << hEmp.calculate_salary() << endl;

          break;

      default:exit(0);

   }

   cout << "\n Do you want to continue?";

   cin>> ans;

   } while (ans == 'y');

   return 0;

}

Output

1. Salaried Employee

2. Hourly Employee

3. Exit

Enter your choice: 2

Enter the number of Hours: 5

Enter the Wages per Hour: 20

The salary of Hourly Employee is: 100

Do you want to continue? y

1. Salaried Employee

2. Hourly Employee

3. Exit

Enter your choice: 1

Enter Basic payment: 1000

Enter HRA: 200

Enter DA: 100

Enter tax: 10

The salary of Salaried Employee is: 1290

Do you want to continue? n

 

Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : C++ Program - Abstract Base Classes and Concrete Classes


Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation