This C++ program illustrates abstract classes. Abstract classes are used to represent general concepts, which can be used as base classes for concrete classes. An abstract class is one which has atleast one purely virtual function. We can not create instance of such class. The program creates a derived class whose instance is created and is manipulated.
Here is the source code of the C++ program which illustrates abstract classes. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Illustrate Abstract Class
*/
#include <iostream>
using namespace std;
class Abstract {
int i, j;
public:
virtual void setData(int i = 0, int j = 0) = 0;
virtual void printData() = 0;
};
class Derived : public Abstract {
int i, j;
public:
Derived(int ii = 0, int jj = 0) : i(ii), j(jj)
{
cout << "Creating object " << endl;
}
void setData(int ii = 0, int jj = 0)
{
i = ii;
j = jj;
}
void printData()
{
cout << "Derived::i = " << i << endl
<< "Derived::j = " << j << endl;
}
};
int main()
{
// Cannot create an instance of Abstract Class
// Abstract a;
Derived d;
cout << "Current data " << endl;
d.printData();
d.setData(10, 20);
cout << "New data " << endl;
d.printData();
}
$ a.out Creating object Current data Derived::i = 0 Derived::j = 0 New data Derived::i = 10 Derived::j = 20
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Programming MCQs
- Check Programming Books
- Apply for C++ Internship
- Check Computer Science Books
- Practice Computer Science MCQs