Abstract class in CPP

Abstract Class is a Class that can not be Instantiated. In programming languages, an Abstract class in CPP is a generic class (or type of object) that can not be instantiated directly. It’s used as a basis for creating hierarchies of classes and specific objects that conform its protocol.

Use of abstract class in c++

#include <iostream>

using namespace std;

// Base class

class Shape

{

public:

// pure virtual function providing interface framework.

virtual int getArea() = 0;

void setWidth(int w)

{

width = w;

}

void setHeight(int h)

{

height = h;

}

protected:

int width;

int height;

};

// Derived classes

class Rectangle: public Shape

{

public:

int getArea()

{

return (width * height);

}

};

class Triangle: public Shape

{

public:

int getArea()

{

return (width * height)/2;

}

};

int main(void)

{

Rectangle Rect;

Triangle Tri;

Rect.setWidth(5);

Rect.setHeight(7);

// Print the area of the object.

cout << “Total Rectangle area: ” << Rect.getArea() << endl;

Tri.setWidth(5);

Tri.setHeight(7);

// Print the area of the object.

cout << “Total Triangle area: ” << Tri.getArea() << endl;

return 0;

}

Pure virtual function in c++

abstract class in cpp
Abstract class in CPP

Example of abstract class in C++

END

C program to draw a square using graphics (click here)

Abstract program in CPP

Note:- Maybe sometime above code doesn’t get user-friendly output, in code block ‘or’ any other EDITOR, All of C & C++ program tested in DOS compiler Turbo C.

Also, Read

Types of functions in C    Read Here

I hope you will understand better if you study at M.U, in the exam of M.U you know how important this “Abstract class in CPP. So please don’t forget to give your valuable feedback. Go in the comment section and Give us some feedback about it. Abstract class in CPP or My website.

Our team check your every Comment per day Because your one feedback helps us in growing Thesmolt

I request you if you have any doubt or question regarding our site, then please don’t hesitate. We always waiting for your Suggestion.

Note:- If you want to Download This article’s in PDF form, then simply go (Top or Bottom of) Right corner and click on the PRINT icon, and it’s saved as a PDF of  Abstract class in CPP

For further questions, you can contact us Via mail [email protected]  otherwise, Our Help Page and Contact us Page, which is included below. I hope I’ll able to say all the possible ways to reach us. Or Download My Android Application Below Download Button.

thesmolt App

Leave a Reply

x