}
23、定义盒子Box 类,要求具有以下成员:可设置盒子形状;可计算盒子体积;可计算盒子的表面积。 #include using namespace std; class Box {
public: Box(double,double,double); double area(); double v(); private: double x,y,z; };
Box::Box(double x1,double y1,double z1) { x=x1; y=y1; z=z1; }
double Box::area() { return (2*(x*y+y*z+x*z)); }
double Box::v() { return (x*y*z);
}
int main() { Box B(2,3,4); cout<<\表面积:\ cout<<\体积:\ return 0; }
24、33、声明一个哺乳动物Mammal 类,再由此派生出狗Dog 类,声明一个Dog 类的对象,观察基类与派生类的构造函数与
析构函数的调用顺序。 #include using namespace std; class mammal {
public: mammal()//默认构造函数 { cout<<\ } ~mammal() { cout<<\ } };
class dog :public mammal {
public: dog() { cout<<\ } ~dog() { cout<<\ } };
int main() { dog a; return 0; }
25、30、定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增
加学号、班级、专业和入学成绩。 #include #include using namespace std; class person {
public: person(string ,string,int); protected: string name; string sex; int age; };
person::person(string n, string s, int a) { name=n; sex=s; age=a; }
class teacher :public person {
public: teacher(string ,string,int,string,string,double); void display(); private: string work_num; string title; double wage; };
teacher::teacher(string n, string s, int a, string wo, string t, double wa):person( n,s,a) { work_num=wo; title=t; wage=wa; }
void teacher::display() { cout<<\姓名:\性别:\年龄:\ cout<<\工号:\职称:\工资:\}
class student: public person {
public: student(string,string,int,string,string,string,double); void show(); private: string num; string grade; string major; double score; };
student::student(string n, string s, int a, string nu, string g, string m, double sc):person(n,s,a) //注意:
只写 n,不是string n { num=nu; grade=g; major=m; score=sc; }
void student::show() { cout<<\姓名:\性别:\年龄:\ cout<<\学号:\年级:\专业:\入学成绩:\}
int main() { teacher t(\侯传旺\男\教授\ t.display();
student s(\秦洪敏\女\大二\应用心理学\ s.show(); return 0; }
26、写一个Complex类,将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的
友元函数。如例题10.3 #include class complex {
private: double a; double b; public:
complex(); complex(double i,double j); friend complex operator +(complex&,complex); void setreal(double x); void setimag(double y); void getreal(); void getimag(); friend ostream& operator <<(ostream& out,complex& x); friend istream& operator >>(istream& in,complex& x); };
complex::complex():a(0),b(0){}
complex::complex(double i,double j):a(i),b(j){} complex operator +(complex&x,complex y) { y.a+=x.a; y.b+=x.b; return complex(y.a,y.b); }
ostream& operator<<(ostream& out,complex &x) { out<istream& operator >>(istream& in,complex& x) { in>>x.a>>x.b; return in; }
void complex::setreal(double x){a=x;} void complex::setimag(double y){b=y;} void complex::getreal() {cin>>a;}
void complex::getimag() {cin>>b;} int main() {
complex a; complex b; cin>>a>>b;
//cout<27、实现一个名为SimpleCircle的简单圆类,其数据成员int *itsRadius为一个指向其半径值的指针,设计对数据成员的各种操作,给出这个类的完整实现并测试这个类。 #include #define PI 3.1415926 using namespace std; class SimpleCircle {
public: SimpleCircle(int *); double fun(); double area(); private: int *itsRadius; };
SimpleCircle::SimpleCircle(int * i) {