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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Information Technology Internship
- Practice Computer Science MCQs
- Buy Computer Science Books
- Apply for C++ Internship
- Apply for Computer Science Internship