Home > Back-end >  Design a c program calculating the round and rectangular area and perimeter
Design a c program calculating the round and rectangular area and perimeter

Time:10-17

Requirements:
1, design a graphic base class, Graph class and derived class;
2, the Graph class includes virtual function area and girth;
3, respectively, in the Circle and Rectangle class reloading these two functions,

CodePudding user response:

This thing in there seems to be a "tutorial c university" as an example

CodePudding user response:

#include

using namespace std;
The class hierarchy
{
Public:
Virtual double GetArea ()=0;
Virtual double GetP ()=0;
};

Class a Rectangle: public Shape
{
Private:
Double a;
Double b;
Public:
A Rectangle (double aa, double bb) {a=aa; B=bb; Cout<& lt;" "Long & lt; Virtual double GetArea () {return a * b; }
Virtual double GetP () {return a + a + b + b; }
};

The class Circle: public Shape
{
Private:
Double r;
Public:
Circle (double rr) {r=rr. Cout<& lt;" Radius "& lt; Virtual double GetArea () {return r * r * 3.14; }
Virtual double GetP () {return 2 * r * 3.14; }
};


Void main ()
{
Double length, width,
Cout & lt; <"Input length and width:";
Cin & gt;> Length & gt;> Width;
A Rectangle the rect (length, width);
Cout & lt; <"Area is:" & lt;

Double rr.
Cout & lt; <"Input radius:";
Cin & gt;> The rr.
Circle cir (rr);
Cout & lt; <"Area is:" & lt;
}

CodePudding user response:

refer to the second floor songhtao response:
# include & lt; Iostream>

using namespace std;
The class hierarchy
{
Public:
Virtual double GetArea ()=0;
Virtual double GetP ()=0;
};

Class a Rectangle: public Shape
{
Private:
Double a;
Double b;
Public:
A Rectangle (double aa, double bb) {a=aa; B=bb; Cout<& lt;" "Long & lt; Virtual double GetArea () {return a * b; }
Virtual double GetP () {return a + a + b + b; }
};

The class Circle: public Shape
{
Private:
Double r;
Public:
Circle (double rr) {r=rr. Cout<& lt;" Radius "& lt; Virtual double GetArea () {return r * r * 3.14; }
Virtual double GetP () {return 2 * r * 3.14; }
};


Void main ()
{
Double length, width,
Cout & lt; <"Input length and width:";
Cin & gt;> Length & gt;> Width;
A Rectangle the rect (length, width);
Cout & lt; <"Area is:" & lt;

Double rr.
Cout & lt; <"Input radius:";
Cin & gt;> The rr.
Circle cir (rr);
Cout & lt; <"Area is:" & lt;
}


  • Related