0% found this document useful (0 votes)
37 views

Interfaces in C++ (Abstract Classes) 2

1) The document discusses how abstract classes can be used to define interfaces in C++. An abstract base class Shape is defined with pure virtual functions getArea() and setWidth() and setHeight(). 2) Two derived classes, Rectangle and Triangle, inherit from Shape and implement their own getArea() functions to calculate the area specific to each shape. 3) main() demonstrates how Rect and Tri objects can call the common interface of getArea() despite having different area calculation strategies due to polymorphism through the abstract base class.

Uploaded by

kuxalabe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Interfaces in C++ (Abstract Classes) 2

1) The document discusses how abstract classes can be used to define interfaces in C++. An abstract base class Shape is defined with pure virtual functions getArea() and setWidth() and setHeight(). 2) Two derived classes, Rectangle and Triangle, inherit from Shape and implement their own getArea() functions to calculate the area specific to each shape. 3) main() demonstrates how Rect and Tri objects can call the common interface of getArea() despite having different area calculation strategies due to polymorphism through the abstract base class.

Uploaded by

kuxalabe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1/16/2019 Interfaces in C++ (Abstract Classes)

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

When the above code is compiled and executed, it produces the following result −

Total Rectangle area: 35


Total Triangle area: 17

You can see how an abstract class defined an interface in terms of getArea and two other classes implemented
same function but with different algorithm to calculate the area specific to the shape.

Designing Strategy
An object-oriented system might use an abstract base class to provide a common and standardized interface
appropriate for all the external applications. Then, through inheritance from that abstract base class, derived

https://www.tutorialspoint.com/cgi-bin/printpage.cgi 2/3

You might also like