Home > Back-end >  Write a program to define abstract base class container three derived class derived from it, the sph
Write a program to define abstract base class container three derived class derived from it, the sph

Time:09-17

Write a program to define abstract base class container three derived class derived from it,

Sphere (ball), cylinder (cylinder) cube (cube)

With a virtual function several graphics are calculated separately, and the surface area and volume
In addition, write overloaded operator functions, overloaded operators & gt; And & lt; Used to determine the size of the object, such as b1 & gt; B2, the return value is true or false,




A great god can help me have a look, what's the problem with my code?




# include
Class Container
{
Protected:
Double r;
Double l;
Public:
The Container (double x=0, double y=0)
{
R=x;
L=y;
}
Virtual int area ()=0;//surface area
Virtual int volume ()=0;//size
Virtual int the show ()=0;

};
The class Sphere: public Container {
Public:
Sphere (double x=0, double y=0) : the Container (x, y)
{};
Int area ()
{
Cout<" The surface area of the sphere, "& lt; <4 * 3.14 * r * rreturn 0;
}
Int volume ()
{
Cout<" The volume of the sphere, "& lt; <(4.0/3) * 3.14 * r * r * rreturn 0;
}
Int the show ()
{
CoutCoutreturn 0;
}


Int operator> (Sphere& A, Sphere& B)
{
int i;
If (a.a rea () & gt; B.a rea ())
I=1;
Else if (Dr. Olume () & gt; B.v olume ())
I=1;
Else I=0;
return i;
}
Int operator<(Sphere& A, Sphere& B)
{
int i;
If (a.a rea () & lt; B.a rea ())
I=1;
Else if (Dr. Olume () & lt; B.v olume ())
I=1;
Else I=0;
return i;
}
};//the sphere
The class Cylinder: public Container {
Public:
Cylinder (double x=0, double y=0) : the Container (x, y)
{};
Int area ()
{
Cout<" The surface area of the cylinder, "& lt; <2 + 2 * 3.14 * 3.14 * r * r * r * lreturn 0;
}
Int volume ()
{
Cout<" The volume of this cylinder: "& lt; <3.14 * r * r * lreturn 0;
}
Int the show ()
{
CoutCoutreturn 0;
}

Int operator> (Cylinder& A, Cylinder& B)
{
int i;
If (a.a rea () & gt; B.a rea ())
I=1;
Else if (Dr. Olume () & gt; B.v olume ())
I=1;
Else I=0;
return i;
}
Int operator<(Cylinder& A, Cylinder& B)
{
int i;
If (a.a rea () & lt; B.a rea ())
I=1;
Else if (Dr. Olume () & lt; B.v olume ())
I=1;
Else I=0;
return i;
}
};//cylinder
The class Cube: public Container {
Public:
Cube (double x=0, double y=0) : the Container (x, y)
{};
Int area ()
{
Cout<" The surface area of the cube, "& lt; <6 * r * rreturn 0;
}
Int volume ()
{
Cout<" The volume of the cube, "& lt; return 0;
}
Int the show ()
{
CoutCoutreturn 0;
}

Int operator> (Cube& A, Cube& B)
{
int i;
If (a.a rea () & gt; B.a rea ())
I=1;
Else if (Dr. Olume () & gt; B.v olume ())
I=1;
Else I=0;
return i;
}

Int operator<(Cube& A, Cube& B)
{
int i;
If (a.a rea () & lt; B.a rea ())
I=1;
Else if (Dr. Olume () & lt; B.v olume ())
I=1;
Else I=0;
return i;
}
};//cube
Int main ()
{
The Container * PTR.
Sphere b1 (5.0), b2 (4.0), b3, b4.
Cylinder c1 (5.0, 4.0) and c2 (4.0, 7.0);
Cube c3 (5.0), c4 (6.0);
PTR=& amp; B1.
PTR - & gt; Area (s);
PTR - & gt; Volume ();
PTR=& amp; B2.
PTR - & gt; Area (s);
PTR - & gt; Volume ();
int i;
I=(b1 & gt; B2);
If (I==1)
{
CoutCout}
The else
{
CoutCout}
/* PTR=& amp; C1.
PTR - & gt; Area (s);
PTR - & gt; Volume ();
PTR=& amp; C2.
PTR - & gt; Area (s);
PTR - & gt; Volume ();
PTR=& amp; C3.
PTR - & gt; Area (s);
PTR - & gt; Volume ();
PTR=& amp; C4.
PTR - & gt; Area (s);
PTR - & gt; Volume (); */
return 0;
}

CodePudding user response:

Overloading symbols, there are two ways you use method 2, adding friend, define a friend function & lt; , and there is a small flaw is the return value should be a bool type
Bool operator> (Cylinder& A) {}//method 1: take this compared with a
Friend bool operator> (Cylinder& A, Cylinder& B) {}//method 2: compare two Cylinder objects outside the class

CodePudding user response:

#include
using namespace std;
Class Container
{
Public:
Virtual double area ()=0;//surface area
Virtual double volume ()=0;//size
Virtual void show ()=0;
};
The class Sphere: public Container
{
Private:
Double r;
Public:
Sphere (double x=0) {r=x; }
Virtual double area ()
{
Return 4 * 3.14 * r * r;
}
Virtual double volume ()
{
Return (4.0/3) * 3.14 * r * r * r;
}
Virtual void show ()
{
Cout<" Surface: "& lt; Cout<" Volume: "& lt;
  • Related