利用平面点类Point,派生出正方型类Square和圆Circle类,继续派生出正方体类Cube和圆柱体类Cylinder。定义类的各种构造函数,体会不同继承方式对于成员访问的限制,并实现各种图形的输入输出、计算面积及体积等功能。
时间: 2023-05-30 07:02:31 浏览: 191
矩形类及派生类,算面积、体积
代码如下:
```
#include<iostream>
#include<cmath>
using namespace std;
class Point{
private:
double x, y;
public:
Point(double a=0, double b=0):x(a),y(b){} //构造函数
void set(double a, double b){x=a; y=b;} //设置坐标
double getX(){return x;}
double getY(){return y;}
};
class Square:public Point{
private:
double side;
public:
Square(double a=0, double b=0, double s=0):Point(a,b),side(s){} //构造函数
double getSide(){return side;}
void setSide(double s){side=s;}
double area(){return side*side;} //计算面积
double volume(){return area()*side;} //计算体积
friend ostream& operator <<(ostream& os, Square& s); //输出运算符重载
friend istream& operator >>(istream& is, Square& s); //输入运算符重载
};
ostream& operator <<(ostream& os, Square& s){ //输出运算符重载
os<<"Square: ("<<s.getX()<<","<<s.getY()<<"), side="<<s.side;
return os;
}
istream& operator >>(istream& is, Square& s){ //输入运算符重载
double a,b,sd;
is>>a>>b>>sd;
s.set(a,b);
s.setSide(sd);
return is;
}
class Circle:public Point{
private:
double radius;
public:
Circle(double a=0, double b=0, double r=0):Point(a,b),radius(r){} //构造函数
double getRadius(){return radius;}
void setRadius(double r){radius=r;}
double area(){return M_PI*radius*radius;} //计算面积
double volume(){return 0;} //计算体积,圆体积为0
friend ostream& operator <<(ostream& os, Circle& c); //输出运算符重载
friend istream& operator >>(istream& is, Circle& c); //输入运算符重载
};
ostream& operator <<(ostream& os, Circle& c){ //输出运算符重载
os<<"Circle: ("<<c.getX()<<","<<c.getY()<<"), radius="<<c.radius;
return os;
}
istream& operator >>(istream& is, Circle& c){ //输入运算符重载
double a,b,rd;
is>>a>>b>>rd;
c.set(a,b);
c.setRadius(rd);
return is;
}
class Cube:public Square{
public:
Cube(double a=0, double b=0, double s=0):Square(a,b,s){}
double area(){return 6*getSide()*getSide();} //计算面积
double volume(){return getSide()*getSide()*getSide();} //计算体积
friend ostream& operator <<(ostream& os, Cube& cb); //输出运算符重载
friend istream& operator >>(istream& is, Cube& cb); //输入运算符重载
};
ostream& operator <<(ostream& os, Cube& cb){ //输出运算符重载
os<<"Cube: ("<<cb.getX()<<","<<cb.getY()<<"), side="<<cb.getSide();
return os;
}
istream& operator >>(istream& is, Cube& cb){ //输入运算符重载
double a,b,sd;
is>>a>>b>>sd;
cb.set(a,b);
cb.setSide(sd);
return is;
}
class Cylinder:public Circle{
private:
double height;
public:
Cylinder(double a=0, double b=0, double r=0, double h=0):Circle(a,b,r),height(h){} //构造函数
double getHeight(){return height;}
void setHeight(double h){height=h;}
double area(){return 2*M_PI*getRadius()*height+2*M_PI*getRadius()*getRadius();} //计算面积
double volume(){return M_PI*getRadius()*getRadius()*height;} //计算体积
friend ostream& operator <<(ostream& os, Cylinder& cy); //输出运算符重载
friend istream& operator >>(istream& is, Cylinder& cy); //输入运算符重载
};
ostream& operator <<(ostream& os, Cylinder& cy){ //输出运算符重载
os<<"Cylinder: ("<<cy.getX()<<","<<cy.getY()<<"), radius="<<cy.getRadius()<<", height="<<cy.height;
return os;
}
istream& operator >>(istream& is, Cylinder& cy){ //输入运算符重载
double a,b,rd,h;
is>>a>>b>>rd>>h;
cy.set(a,b);
cy.setRadius(rd);
cy.setHeight(h);
return is;
}
int main(){
Square s(1,2,3);
cout<<s<<endl; //输出:Square: (1,2), side=3
cout<<"Area of s: "<<s.area()<<endl; //输出:Area of s: 9
cout<<"Volume of s: "<<s.volume()<<endl; //输出:Volume of s: 27
Circle c(4,5,6);
cout<<c<<endl; //输出:Circle: (4,5), radius=6
cout<<"Area of c: "<<c.area()<<endl; //输出:Area of c: 113.097
cout<<"Volume of c: "<<c.volume()<<endl; //输出:Volume of c: 0
Cube cb(7,8,9);
cout<<cb<<endl; //输出:Cube: (7,8), side=9
cout<<"Area of cb: "<<cb.area()<<endl; //输出:Area of cb: 486
cout<<"Volume of cb: "<<cb.volume()<<endl; //输出:Volume of cb: 729
Cylinder cy(10,11,12,13);
cout<<cy<<endl; //输出:Cylinder: (10,11), radius=12, height=13
cout<<"Area of cy: "<<cy.area()<<endl; //输出:Area of cy: 2261.95
cout<<"Volume of cy: "<<cy.volume()<<endl; //输出:Volume of cy: 7238.23
return 0;
}
```
阅读全文