answersLogoWhite

0

#include<iostream>

struct shape

{

virtual double area () const = 0;

};

struct triangle : shape

{

triangle (double b, double h): base (b), height (h) {}

double base, height;

double area () const override { return base * height / 2; }

};

struct circle : shape

{

circle (double r): radius (r) {}

double radius;

double area () const override { return 4 * atan(1) * radius * radius; }

};

struct rectangle : shape

{

rectangle (double w, double h): width (w), height (h) {}

double width, height;

double area () const override { return width * height; }

};

int main()

{

triangle t (10, 5);

std::cout << "triangle with base " << t.base << " and height " << t.height << " has area " << t.area() << std::endl;

circle c (5);

std::cout << "circle with radius " << c.radius << " has area " << c.area() << std::endl;

rectangle r (10, 5);

std::cout << "rectangle with width " << r.width << " and height " << r.height << " has area " << r.area() << std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Continue Learning about Engineering

What is a super class in terms of object oriented programming?

A superclass, also referred to as a parent class, is a class what which other classes are derived from. These derived classes are known as either subclasses or child classes.


A pure virtual function is a virtual function that has?

A pure-virtual function is a function that must be overridden in derived classes. You simply add "=0" to the end of the function declaration. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


Can a friend function of derived class access private data of base class?

In some computer languages it is possible to do so, but I would not even think or design any application in this way. A base class SHOULD NEVER know what the derived classes are. Perhaps it was created by generalizing some classes. Even at that point, this new base class should have no knowledge of the derived classes whatsoever. To do a good OO design, the base class should have a method like getPrivatePartOfDerivedClass() as abstract, then force the derived class to provide the implementation of this method.


Is it necessary to provide a definition to the pure virtual function in the inherited derived class?

No, it is not necessary, but the derived class will be rendered abstract as well. Sometimes that can be desirable. Only derived classes that provide a complete implementation of all inherited pure-virtual methods can actually be instantiated. However, derived classes can also inherit pure-virtual implementations from any intermediate base classes, but not from the least-derived abstract base classes (where the pure-virtual methods originated). Abstract base classes can also provide their own default implementations of their own pure-virtual methods, but they cannot be called implicitly, even from the base class itself. These methods must still be overridden by derived classes, even if an override only needs to explicitly call the base class method.


What are the limitations of friend function?

Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope.

Related Questions

How is the triangle formula derived from the area of rectangles?

The area of a triangle is expressed using the formula A=(1/2)(bh) Where A is area Where B is length of the base of the triangle. Where H is the height of the triangle. The area of a rectangle is A=BH, Where B is the length of the base of the rectangle. Where H is the height of the rectangle. Because a triangle, essentially, is a half of a rectangle, you find the area of the whole rectangle that the triangle comes from, then divide that in half.


What are the merits and demerits of defining and declaring a pure virtual function in a program?

Merits of defining a pure virtual function: It enforces derived classes to implement the function, ensuring polymorphic behavior. It enables abstract classes to define a common interface. Demerits: It can hinder flexibility as derived classes must implement the function. It may also increase code complexity.


Teach a child area of triangle without formula?

You will have to use a formula, but this can easily be derived for the special case of a right triangle. If you divide a rectangle by a diagonal, you divide it into two congruent triangles, therefore, the triangle has half the area of the rectangle, that is, 1/2 times base times height.


How do you get the diameter of a rectangle?

A rectangle does not have a diameter, only circles or similar obects have diameters. A rectangle has a diagonal which is obtained by joining two opposite corners. Any two adjacent sides of the rectangle together with the diagonal form a right angled triangle and so the length of the diagonal can be derived using Pythagoras's theorem.


When the concept of method overriding is necessary?

concept of overriding is very important as due to overriding the derived class can use the function of the base class! when the function has same name and prototype in both the classes(base and derived) then the derived class can use the funtion of base class!


What is a super class in terms of object oriented programming?

A superclass, also referred to as a parent class, is a class what which other classes are derived from. These derived classes are known as either subclasses or child classes.


A pure virtual function is a virtual function that has?

A pure-virtual function is a function that must be overridden in derived classes. You simply add "=0" to the end of the function declaration. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


Can a friend function of derived class access private data of base class?

In some computer languages it is possible to do so, but I would not even think or design any application in this way. A base class SHOULD NEVER know what the derived classes are. Perhaps it was created by generalizing some classes. Even at that point, this new base class should have no knowledge of the derived classes whatsoever. To do a good OO design, the base class should have a method like getPrivatePartOfDerivedClass() as abstract, then force the derived class to provide the implementation of this method.


What are chemical mediators of inflammation?

to classes: Plasma derived Cell Derived


Is it necessary to provide a definition to the pure virtual function in the inherited derived class?

No, it is not necessary, but the derived class will be rendered abstract as well. Sometimes that can be desirable. Only derived classes that provide a complete implementation of all inherited pure-virtual methods can actually be instantiated. However, derived classes can also inherit pure-virtual implementations from any intermediate base classes, but not from the least-derived abstract base classes (where the pure-virtual methods originated). Abstract base classes can also provide their own default implementations of their own pure-virtual methods, but they cannot be called implicitly, even from the base class itself. These methods must still be overridden by derived classes, even if an override only needs to explicitly call the base class method.


What are the limitations of friend function?

Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope.


Why are Inheritance super classes fragile?

Superclasses are considered fragile because seemingly safe modifications to a super class, when inherited by the derived classes, may cause the derived classes to malfunction.