问题及代码:
#include <iostream> using namespace std; const double pi=3.14; class Point { public: Point(int xx,int yy):x(xx),y(yy){} void setx(int xx){x=xx;} void sety(int yy){y=yy;} int getx(){return x;} int gety(){return y;} private: int x; int y; }; class Circle:public Point { public: Circle(int xx,int yy,double rr):Point(xx,yy),r(rr){} void Area() { area=pi*r*r; } void Printarea() { cout<<"圆的面积为:"<<area<<endl; } double getarea(){return area;} double getr(){return r;} private: double r; double area; }; class Cylinder:public Circle { public: Cylinder(int xx,int yy,double rr,double hh):Circle(xx,yy,rr),h(hh){} void Area1() { area1=getarea()*h; } void Volume() { volume=getarea()*2+2*pi*getr()*h; } void print() { cout<<"圆柱的体积为:"<<area1<<endl; cout<<"圆柱的表面积为:"<<volume<<endl; } private: double h; double area1; double volume; }; int main() { Cylinder c(1,2,3,4); cout<<c.getx()<<" "<<c.gety()<<endl; c.Printarea(); c.print(); return 0; }
运行结果:
知识点总结:
从这个程序中我学会了对一个特别复杂程序,要学会分步骤实现的思想。
学习心得:
遇到难题别气馁,多多思考就能找到解决的办法
时间: 2024-10-01 03:51:45