Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

Abstract Classes

C++ Object Oriented Programming

Questions: 1. What is an abstract class ? 2. What is dynamic binding? How is it achieved. 3. What is the difference between a virtual function and a pure virtual function? Give example of each.

Abstract Classes

• 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 In be 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;

       }

virtual double getarea() = // 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;

       }

};

int 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';

return 0;

}

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

Program Explanation

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

 

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 :

#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;

}

};

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

return 0;

}

Output

Enter the name:

Ramesh

Ramesh is a an engineering student

Enter the name:

Suresh

Suresh is a medical student

 

Example:2

Develop an abstract class Polygon from which Triangle and Rectangle are derived. Each Polygon should contain the function Area() to calculate the area of them. Invoke appropriate Area() function to calculate the area using pointer to base class and pointers to derived classes.

Solution :

#include<iostream>

using namespace std;

class Polygon //Abstract Base Class

{

private:

      double length, breadth;

public:

      void Set(double 1,double b)

      {

            length = 1;

            breadth = b;

      }

      void Get(double &len,double &br)

      {

            len = length;

            br = breadth;

      }

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

};

class Triangle :public Polygon

{

public:

      double Area()

      {

            double len,br,result;

            Get(len, br);

            result = 0.5*len*br;

            return result;

      }

};

class Rectangle :public Polygon

{

public:

      double Area()

      {

            double len, br, result;

            Get(len, br);

            result =len*br;

            return result;

      }

};

void main()

{

Polygon *ptr;//pointer to base class

Triangle T;//object of Triangle class

Rectangle R;//object of Rectangle class

double a, b;

cout << "\n Enter length: ";

cin >> a;

cout << "\n Enter breadth: ";

cin >> b;

T.Set(a, b);

ptr = &T;

cout << "\n Area of Triangle is: " << ptr‒>Area();

R.Set(a, b);

ptr = &R;

cout << "\n Area of Rectangle is: " << ptr‒>Area();

}

Output

Enter length: 10

Enter breadth: 20

Area of Triangle is: 100

Area of Rectangle is : 200

Program Explanation :

i) In above code Area() is a function which is defined as a pure virtual function in base class.

ii) This function is redefined in the derived classes Triangle and Rectangle. This redefinition is called as function overriding.

iii) In Triangle class the Area() is redefined to calculate the area of triangle. Similarly

In Rectangle class the Area() is redefined to calculate the area of rectangle.

iv) In the main(), we have invoked the Area() function using base class pointer. This pointer value is then changed by the addresses of derived classes.

 

Review Questions

1. What is an abstract class ?

2. What is dynamic binding? How is it achieved.

3. What is the difference between a virtual function and a pure virtual function? Give example of each.

 

Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Abstract Classes


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 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


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

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


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation