先谢谢各位先知了!
编写一个程序计算圆、圆内接正方形和圆外切正方形的面积和周长。 要求: 1、由于要计算机圆和正方形的面积和周长,所以可把图形定义为一个类,它包含
的数据成员为长度,把求面积和周长定义为虚函数。 2、定义好图形类后,再把圆类、正方形类定义为图形的派生类。在这些类中同样
包含有新定义的构造函数、求面积的函数和求周长的函数。 3、在完成上述要求后,再将图形类定义中的求面积和周长的函数均定义为纯虚函
数,重新编写该程序。
#include<iostream.h> #define Pi 3.141592 class TRound { public: void Tround(double diameter); void Nei(double diameter); void Wai(double diameter); void Print(); private: double diameter,Rperimeter,Wperimeter,Nperimeter,Rdimension,Wdimension,Ndimension; }; void TRound::Tround(double diameter) {
Rperimeter=2*Pi*diameter; Rdimension=Pi*diameter*diameter; } void TRound::Nei(double diameter) { double d; d=diameter*1.414; Nperimeter=4*d; Ndimension=d*d; } void TRound::Wai(double diameter) { double d; d=2*diameter; Wperimeter=4*d; Wdimension=d*d; } void TRound::Print() { cout<<"圆周长:"<<Rperimeter<<"\t"<<"圆面积:"<<Rdimension<<endl; cout<<"内接正方形周长:"<<Nperimeter<<"\t"<<"内接正方形面积:"<<Ndimension<<endl; cout<<"外接圆正方形周长:"<<Wperimeter<<"\t"<<"外接圆正方形面积:"<<Wdimension<<endl; }
void main() { TRound round; double r; cout<<"输入圆直径:"; cin>>r; round.Tround(r); round.Nei(r); //请问下 为什么我不写这两行 答案不正确 round.Wai(r); round.Print(); }
刚刚做的 我也刚学类 不知道要怎么做
这个可以运行 数据也正确 但觉得怪怪 很多地方是凑出来的
望高手改改
[此贴子已经被作者于2004-11-12 02:00:34编辑过]
先上虚函数的,晚上在上纯虚函数的(我要上课)
楼上的应为你的round.Print()函数需要用到Nperimeter,Ndimension,Wperimeter和Wdimension的值,你没执行round.Nei(r); round.Wai(r);
怎么知道 这些值呢?
------------------------
#include<iostream.h> #include<math.h> #define Pi 3.141592 class Circle { private: double R; public: Circle(){}; Circle(double r) { R=r; } virtual double area() { return Pi*R*R; } virtual double round() { return 2*Pi*R; } virtual void display() { cout<<"圆面积:"<<area()<<endl; cout<<"圆周长: "<<round()<<endl; } }; class Square_nei : public Circle { private: double S_n_b; public: Square_nei(double s) { S_n_b=s; } double area() { return 2*S_n_b*S_n_b; } double round() { double i=sqrt(2); return 4*S_n_b*i; } void display() { cout<<"圆内接正方行面积:"<<area()<<endl; cout<<"圆内接正方行周长: "<<round()<<endl; } }; class Square_wai : public Circle { private: double S_w_b; public: Square_wai(double q) { S_w_b=q; } double area() { return 4*S_w_b*S_w_b; } double round() { return 8*S_w_b; } void display() { cout<<"圆外接正方行面积:"<<area()<<endl; cout<<"圆外接正方行周长: "<<round()<<endl; } };
int main() { int R; cout<<"请输入圆的半径:"; cin>>R; cout<<"你要的结果是:"<<endl; cout<<"------------------------"<<endl; Circle C(R); Square_nei S_n(R); Square_wai S_w(R); C.display(); S_n.display(); S_w.display(); return 0; }
--------------------------
本人愚笨,望大家 海函!!
[此贴子已经被作者于2004-11-12 09:47:52编辑过]
这就是你要的纯虚函数了,
#include<iostream.h> #include<math.h> #define Pi 3.141592
class Object { public: virtual double area()=0; virtual double round()=0; virtual void display()=0; }; class Circle:public Object { private: double R; public: Circle(double r) { R=r; } double area() { return Pi*R*R; } double round() { return 2*Pi*R; } virtual void display() { cout<<"圆面积:"<<area()<<endl; cout<<"圆周长: "<<round()<<endl; } }; class Square_nei : public Object { private: double S_n_b; public: Square_nei(double s) { S_n_b=s; } double area() { return 2*S_n_b*S_n_b; } double round() { double i=sqrt(2); return 4*S_n_b*i; } void display() { cout<<"圆内接正方行面积:"<<area()<<endl; cout<<"圆内接正方行周长: "<<round()<<endl; } }; class Square_wai : public Object { private: double S_w_b; public: Square_wai(double q) { S_w_b=q; } double area() { return 4*S_w_b*S_w_b; } double round() { return 8*S_w_b; } void display() { cout<<"圆外接正方行面积:"<<area()<<endl; cout<<"圆外接正方行周长: "<<round()<<endl; } }; int main() { int R; cout<<"请输入圆的半径:"; cin>>R; cout<<"你要的结果是:"<<endl; cout<<"------------------------"<<endl; Circle C(R); Square_nei S_n(R); Square_wai S_w(R); C.display(); S_n.display(); S_w.display(); return 0; }
[此贴子已经被作者于2004-11-12 09:48:31编辑过]
你想要的是这样的 吗??
我只是 粗粗的改了一下!
#include<iostream.h> #include<math.h> #define Pi 3.141592 class TRound { public: TRound(double r) { R=r; } double Tround_area(); double Tround_round(); double Nei_area(); double Nei_round(); double Wai_area(); double Wai_round(); void Print(); private: double R; }; double TRound::Tround_area() { double d=Pi*R*R; return d; } double TRound::Tround_round() { double d=2+Pi*R; return d; } double TRound::Nei_area() { double d=2*R*R; return d; } double TRound::Nei_round() { double i=sqrt(2); double d=4*R*i; return d; }
double TRound::Wai_area() { double d=4*R*R; return d; } double TRound::Wai_round() { double d=8*R; return d; } void TRound::Print() { cout<<"圆周长:"<<Tround_round()<<"\t"<<"圆面积:"<<Tround_area()<<endl; cout<<"内接正方形周长:"<<Nei_round()<<"\t"<<"内接正方形面积:"<<Nei_area()<<endl; cout<<"外接圆正方形周长:"<<Wai_round()<<"\t"<<"外接圆正方形面积:"<<Wai_area()<<endl; }
void main() { TRound round(5); round.Print(); }