11.3k views
0 votes
Assume you have a class Square. This class has an instance variable, side that is protected and of type double. Write the class definition of a subclass of Square called FancySquare that has a method called getDiagonal. The getDiagonal method gets no arguments but returns the value of side multiplied by the square root of two.

User Terry Roe
by
7.9k points

1 Answer

7 votes

Answer:

#include<iostream>

#include<math.h>

using namespace std;

class Square

{

protected:

double side;

public:

void set(int s) //sets value of side

{

side=s;

}

};

class FancySquare : public Square //inheriting publicly

{

public:

double getDiagonal() //returns diagonal of type double

{

return side*(sqrt(2));

}

};

int main() {

FancySquare f; //instantiating class

f.set(4);

cout<<f.getDiagonal(); //returns diagonal of side 4

return 0;

}

OUTPUT :

5.65685

Step-by-step explanation:

In the above code, a set() method with public access specifier is defined which takes a value of int type and sets its value to side variable, which is of int type. FancySquare is a class which inherits publicly from class Square and contains a member function getDiagonal() which uses data member side of its parent class Square. In the main method, an object of class FancySquare is created and getDiagonal() is called.

User Franco Pettigrosso
by
6.9k points