#include<iostream> #include<cmath> using namespace std; class Point{ protected: float x,y; public: Point(float a,float b); void setPoint(float,float); float getX(){return x;} float getY(){return y;} friend ostream& operator<<(ostream & ,const Point &); }; Point::Point(float a,float b){ x=a; y=b; } void Point::setPoint(float a,float b){ x=a; y=b; } ostream & operator<<(ostream &output ,const Point &p){ output<<"["<<p.x<<","<<p.y<<"]"<<endl; return output; } //======================================== class Circle:public Point{ protected: float radius; public: Circle(float x=0,float y=0,float r=0);//构造函数 void setRadius(float); float getRadius() const; float Area() const; friend ostream &operator<<(ostream &,const Circle &); }; Circle::Circle(float x,float y,float r):Point(x,y),radius(r){} void Circle::setRadius(float r){radius=r;} float Circle::getRadius() const {return radius;} float Circle::Area() const {return 3.14*radius*radius;} ostream &operator<<(ostream &output,const Circle &c){ output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.Area()<<endl; return output; } class Cylinder:public Circle{ protected: float height; public: Cylinder(float x=0,float y=0,float r=0,float h=0); void setHeight(float); float getHeight() const; float Area() const; float volume() const; friend ostream& operator<<(ostream & ,const Cylinder &); }; Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){} void Cylinder::setHeight(float h){height=h;} float Cylinder::getHeight() const {return height;} float Cylinder::Area() const {return 2*Circle::Area()+2*3.14*radius*height;} float Cylinder::volume() const {return Circle::Area()*height;} ostream& operator<<(ostream &output ,const Cylinder &cy) { output<<"Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height<<"area"<<cy.Area()<<",volume"<<cy.volume()<<endl; return output; } int main(){ Cylinder cy1(3.5,6.4,5.2,10); cout<<"x="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight<<",area="<<cy1.Area()<<",volume="<<cy1.volume()<<endl; cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); cout<<"new"<<cy1;
时间: 2024-04-13 12:30:06 浏览: 91
#include_iostream.h_
4星 · 用户满意度95%
你提供的代码存在一些问题。在调用 `cy1.getHeight` 和 `cy1.getHeight` 函数时,缺少了函数调用的括号。此外,在使用 `operator<<` 输出 `cy1` 时,缺少了分号。以下是修改后的代码:
```cpp
#include<iostream>
#include<cmath>
using namespace std;
class Point{
protected:
float x, y;
public:
Point(float a, float b);
void setPoint(float, float);
float getX() { return x; }
float getY() { return y; }
friend ostream& operator<<(ostream&, const Point&);
};
Point::Point(float a, float b){
x = a;
y = b;
}
void Point::setPoint(float a, float b){
x = a;
y = b;
}
ostream& operator<<(ostream& output, const Point& p){
output << "[" << p.x << "," << p.y << "]" << endl;
return output;
}
class Circle: public Point{
protected:
float radius;
public:
Circle(float x = 0, float y = 0, float r = 0);
void setRadius(float);
float getRadius() const;
float Area() const;
friend ostream& operator<<(ostream&, const Circle&);
};
Circle::Circle(float x, float y, float r): Point(x, y){
radius = r;
}
void Circle::setRadius(float r){
radius = r;
}
float Circle::getRadius() const {
return radius;
}
float Circle::Area() const {
return 3.14 * radius * radius;
}
ostream& operator<<(ostream& output, const Circle& c){
output << "Center=[" << c.x << "," << c.y << "], r=" << c.radius << ", area=" << c.Area() << endl;
return output;
}
class Cylinder: public Circle{
protected:
float height;
public:
Cylinder(float x = 0, float y = 0, float r = 0, float h = 0);
void setHeight(float);
float getHeight() const;
float Area() const;
float volume() const;
friend ostream& operator<<(ostream&, const Cylinder&);
};
Cylinder::Cylinder(float a, float b, float r, float h): Circle(a, b, r){
height = h;
}
void Cylinder::setHeight(float h){
height = h;
}
float Cylinder::getHeight() const {
return height;
}
float Cylinder::Area() const {
return 2 * Circle::Area() + 2 * 3.14 * radius * height;
}
float Cylinder::volume() const {
return Circle::Area() * height;
}
ostream& operator<<(ostream& output, const Cylinder& cy){
output << "Center=[" << cy.x << "," << cy.y << "], r=" << cy.radius << ", h=" << cy.height << ", area=" << cy.Area() << ", volume=" << cy.volume() << endl;
return output;
}
int main(){
Cylinder cy1(3.5, 6.4, 5.2, 10);
cout << "x=" << cy1.getX() << ", y=" << cy1.getY() << ", r=" << cy1.getRadius() << ", h=" << cy1.getHeight() << ", area=" << cy1.Area() << ", volume=" << cy1.volume() << endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5, 5);
cout << "new " << cy1;
return 0;
}
```
在这个代码中,我们定义了三个类:Point、Circle 和 Cylinder。Point类表示一个点,Circle类表示一个圆,Cylinder类表示一个圆柱体。每个类都有相应的成员函数和友元函数用于操作和输出对象。在main函数中,创建了一个 Cylinder 对象 cy1,并输出其属性。然后修改 cy1 的属性,并再次输出。
阅读全文