Loading....
Coupon Accepted Successfully!

 

Abstract Classes

Abstract classes are similar to a skeleton on which new classes are designed to assemble a well-designed class hierarchy. The set of well-tested abstract classes can be used, and the programmer only extends them. Abstract classes containing virtual functions can be used to help in program debugging. When various programmers work on the same project, it is necessary to create a common abstract base class for them. The programmers are restricted to create a new base class.
 
The development of such software can be demonstrated by creating a header file. The file abstract.h is an example of a file containing an abstract base class that is used for debugging purpose. Contents of the file abstract.h are as follows:
 

Contents of abstract.h header file

#include<iostream.h>

struct debug

{

virtual void show()

{

cout<<“\n No function show() defined for this class”;

}

};

 

 

15.9 Write program to use abstract class for program debugging.

#include“abstract.h”

#include<constream.h>

class A : public debug

{

int a;

public:

A(int j=0) {a=j;

}

void show() {cout<<“\nIn class A a=”<<a;}

};

class B : public debug

{

int b;

public:

B (int k ) {b=k;}

};

void main()

{

clrscr();

A a(1);

B b(5);

a.show();

b.show();

}

OUTPUT
In class A a=1
No function show() defined for this class
 
Explanation: Observe the contents of file abstract.h. The struct debug contains the virtual function show(). This function is declared in the file abstract.h and the same is inserted using #include directive in the above program. The classes A and B are derived from class debug, which is defined in the header file abstract.h. In function, a and b are the objects of classes A and B, respectively. The statement a.show(); invokes the function show(), and the value of a is displayed. The object b also invokes the function show(). However, the class B does not have the function show(). Hence, the function show() of an abstract base class is executed, which displays a warning message.
 
Traditional languages do not provide such facilities. An abstract class develops into a dominant and powerful interface when the software system undergoes various changes. It is essential to confirm that the debugging interface is accurately constructed. If changes are made in the actual project, it is compulsory to add appropriate methods to the abstract base class. In case the programmer needs to define a function warn() to the class debug, then the header file can be updated. The contents of the file would be as follows:
 

Contents of abstract.h header file

#include<iostream.h>

struct debug

{

virtual void show()

{

cout<<“\n No function show() defined for this class”;

}

};

virtual void warn()

{

cout<<“\n No function warn() defined for this class”;

}

};

 
While defining such an abstract class, the following points should be kept in mind:
  1. Do not declare an object of abstract class type.
  2. An abstract class can be used as a base class.
  3. The derived class should not have pure virtual functions. Objects of the derived class can be declared.




Test Your Skills Now!
Take a Quiz now
Reviewer Name