Home > Back-end >  In a class C in the main function call function error
In a class C in the main function call function error

Time:09-17



Want to ask you how to call functions in a class in the main function, I don't know this why
 # include & lt; Iostream> 
using namespace std;
Const double PI=3.14159;
The class Figure {
Public:
Virtual double area ()=0;
Virtual double volume ()=0;

};
The class Circle: public Figure {
Public:
Circle (double r) : the radius (r) {}
Double getR () {
Cout<" Please enter the radius "& lt; Cin> The radius.
Return the radius.


}
Double setR () {
Return the radius.
}


Private:
Double radius;//R
};


The class Sphere: public Circle {//ball
Public:
Sphere (double x) : Circle (x) {}

Virtual double volume () {
Double a=setR ();
Return four thirds * PI * a * a * a;
}
Virtual double area () {
Double a=setR ();
Return 4 * PI * a * a;
}
};
The class Cylinder: public Circle {//Cylinder
Public:
Cylinder (double a, double b) : Circle (a), height (b) {}
Virtual double volume () {
Double a=setR ();
Return a * a * PI * height;
}
Virtual double area () {
Double a=setR ();
Return a * a * PI * 2 + 2 * PI * a * height;
}
Private:
A double height;//H

};
Int main () {
Circle, Circle getR ();
The Sphere (Circle. The sp setR ());
Cylineder cy (Circle. SetR (), 3.0);
Cout<" Surface area of the circle is: "& lt; Cout<" The volume of the circle is: "& lt; Cout<" Surface area of the cylinder is: "& lt; Cout<" The volume of a cylinder is: "& lt; return 0;
}

CodePudding user response:

Circle the Cir;
Cir. GetR

CodePudding user response:

So or not ah

CodePudding user response:

Circle Circle;
Circle. GetR ();
The Sphere (circle. The sp setR ());
Cylineder cy (circle. SetR (), 3.0);
Cout<" Surface area of the circle is: "& lt; Cout<" The volume of the circle is: "& lt; Cout<" Surface area of the cylinder is: "& lt; Cout<" The volume of a cylinder is: "& lt; CodePudding user response:

You didn't reflect this polymorphic
Circle Circle;
Circle. GetR ();
The Sphere (circle. The sp setR ());
Cylineder cy (circle. SetR (), 3.0);
Circle * c1=& amp; sp;
Circle * c2=& amp; Cy.
Cout<" Surface area of the circle is: "& lt; Cout<" The volume of the circle is: "& lt; Cout<" Surface area of the cylinder is: "& lt; Cout<" The volume of a cylinder is: "& lt; CodePudding user response:

Your estimate is not very understand what is the class, what is the object that is instantiated
  • Related