1、设计一个学生类Student,该类包含 保护性 数据成员: 学号(NUM),姓名(Name),性别(Sex);定义公用成员函数:ReadData() 用于输入Num, Name和Sex, OutputData() 用于输出Num, Name和Sex。
在学生类的基础上采用公用继承派生出研究生类Graduate, 在Graudate类中增加私有数据成员研究方向(R_Interest),增加成员函数Set_Research()设置研究方向,增加成员函数ResearchWork()用于输出学号和研究方向。
#include <iostream> #include <string> using namespace std; class Student{ protected: int num ; string name ; string sex ; public: Student(int n=0,string m="xu",string s="nan"):num(n),name(m),sex(s){} void ReadData(){ cout << "输入学生的信息: "<<endl; cin>> num >> name >> sex ; } void OutputData(){ cout << "该学生的信息是: "<<endl; cout << "学号: " << num << endl <<"姓名: "<< name << endl <<"性别: "<< sex << endl; } void display(){ cout << num ; } }; class Graduate : public Student{ string R_Interest ; public: Graduate(string r="mu"):R_Interest(r){} void Set_Research(){ cout << "输入这个研究生的发展方向: " ; cin >> R_Interest ; } void ResearchWork(Student &s){ cout << "输出学生的学号和研究方向: "<< endl; cout << "学号: ";s.display();cout<<endl; cout << "研究方向: " << R_Interest << endl; } }; int main(){ Student s ; s.ReadData() ; s.OutputData() ; cout << "读到研究生后: " << endl; Graduate g ; g.Set_Research() ;cout<<endl; g.ResearchWork(s) ; }其运行结果如下:
2、定义一个 “点” 类Point,包含数据成员 x,y(坐标点),定义带参数的构造函数初始化数据成员x,y,定义析构函数Point()(函数中输出内容自己给出)。由点类派生出“圆”类Circle, 增加新的数据成员radious(double类型),定义带参数的构造函数初始化radious,定义析构函数Circle()(函数中输出内容自己给出)。由圆类派生出“圆柱体”类Cylinder, 并增加新的数据成员height(double类型), 定义带参数的构造函数初始化height, 定义析构函数Cylinder()(函数中输出内容自己给出),定义函数Volume()计算圆柱体体积。
建立一个圆柱体对象 cyl1 并下列值进行初始化(x=1,y=1, raidou=10, height=100), 调用Volume函数求圆柱体体积,并观察构造函数和析构函数的调用顺序。
#include <iostream> #define PI 3.14 using namespace std; class Point{ protected: int x ; int y ; public: Point(int x,int y):x(x),y(y){} ~Point(){ cout<<"点空间已清除"<<endl; } }; class Circle:public Point{ //圆 protected: double radious ; public: Circle(int x,int y,double r):Point(x,y),radious(r){} ~Circle(){ cout<<"圆空间已清除"<<endl; } }; class Cylinder:public Circle{ //圆柱体 double height ; public: Cylinder(int x,int y,double r,double h):Circle(x,y,r),height(h){} ~Cylinder(){ cout<<"圆柱体空间已清除"<<endl; } void Volume(){ cout<<"圆柱体面积是: "<<endl; double s; s=PI*radious*radious*height ; cout<< s <<endl; } }; int main(){ Cylinder c(1,1,10,100); c.Volume(); }其运行结果如下:
3、分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式有这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:1)、在两个基类中的包含姓名、年龄、性别、地址、电话、等数据成员。2)、在Teacher类中包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。3)、对两个基类中的姓名、年龄、性别、职称、地址、电话等数据成员用相同的名字,在引用数据成员时制定作用域。4)、在类中声明成员函数,在类外定义成员函数 。5)、在派生类Teacher_cadre的成员函数show中调用Teacher类中的display函数。输出姓名,年龄,性别,职称,地址,电话,然后再用cout语句输出职务与工资。
#include <iostream> using namespace std; class Teacher{ //教师 string name ; int age ; string sec ; string address ; int telemphone ; string title ; //职称 public: Teacher(string n,int a,string s,string ad,int t,string ti):name(n),age(a),sec(s),address(ad),telemphone(t),title(ti){} void display(); }; void Teacher::display(){ cout<< "name= " << name <<endl; cout<< "age= " << age <<endl; cout<< "sec= " << sec <<endl; cout<< "title= " << title <<endl; cout<< "address= " << address <<endl; cout<< "telemphone= " << telemphone <<endl; } class Cadre{ //干部 string name ; int age ; string sec ; string address ; int telemphone ; protected: string post ; //职务 public : Cadre(string n,int a,string s,string ad,int t,string p):name(n),age(a),sec(s),address(ad),telemphone(t),post(p){} }; class Teacher_Cadre:public Teacher , public Cadre{ int wages ; //工资 public: Teacher_Cadre(string n,int a,string s,string ad,int t,string ti,string p,int w): Teacher(n,a,s,ad,t,ti),Cadre(n,a,s,ad,t,p),wages(w){} void show(); }; void Teacher_Cadre::show(){ display();cout<<endl; cout<<"post= " <<post <<endl; cout<<"wages= " <<wages<<endl; } int main(){ Teacher_Cadre T("xxs",19,"男","开封",8691797,"professor","Help the students to study. ",20000) ; T.show(); }其运行结果如下:
4、定义一个 “点” 类Dot,包含数据成员 x,y(坐标点),在平面上两点连成一条直线,定义一个直线类 Line,求直线的长度和直线中点坐标, 数据自拟。
#include <iostream> #include <math.h> using namespace std; class Dot{ public: int x ; int y ; Dot(int a=0,int b=0):x(a),y(b){} void display(){ cout<<"点坐标为: "<<"(" << x << "," << y << ")" <<endl; } }; class Line:public Dot{ double x , y ; double length ; public: Dot d1,d2 ; Line(int x1,int x2,int x3,int x4,int x5=0,int x6=0):d1(x1,x2),d2(x3,x4){ x=x5; y=x6; } Line(Dot x1,Dot x2){ d1=x1 ; d2=x2 ; } void midd(){ x=(double)(d1.x+d2.x)/2 ; y=(d1.y+d2.y)/2 ; cout<<"该两点间中点为: " << "(" << x << "," << y <<")" <<endl; } void juli(){ int a, b ; a=d2.x-d1.x ; b=d2.y-d1.y ; length=sqrt(pow(a,2)+pow(b,2)) ; cout << "两点间距离为: "<< length <<endl; } }; int main(){ Line l(1,2,4,6) ; /*Dot d1(1,2) , d2(3,4) ; Line l(d1,d2);*/ l.d1.display(); l.d2.display(); l.midd(); l.juli(); }其运行结果如下:
5、分别定义类如下:(1) Birthday(生日类) 含有:year(年), month (月),日 (day) 等数据成员;(2) Staff(职工类)含有:num(工号),name(姓名),sex(性别) 等数据成员;(3) Teacher(教师类)含有:职工类和生日类的数据成员。
要求:(1)通过对Staff和Birthday使用继承和组合的方式设计Teacher;(2)定义Teacher类对象teach,并给出所有数据的初值,数据自拟;(3)修改teach的生日数据;(4)输出teach的全部最新数据。
#include <iostream> using namespace std; class Birthday{ int year , month , day ; public: Birthday(int y=0,int m=0,int d=0):year(y),month(m),day(d){} void display(){ cout << "year= "<<year <<endl; cout << "moth= "<<month <<endl; cout << "day= "<<day<<endl; } void change(){ cout<<"重新输入生日: "<<endl; cin>>year>>month>>day; } }; class Staff{ int num ; string name , sex ; public: Staff(int n=0, string na="na", string s="s"):num(n),name(na),sex(s){} void display(){ cout << "num= "<<num <<endl; cout << "name= "<<name <<endl; cout << "sex= "<<sex <<endl; } }; class Teacher:public Birthday { public: Staff st; Teacher(int y,int m,int d,int n,string na,string s):Birthday(y,m,d),st(n,na,s){} void display(){ Birthday::display(); st.display(); } }; int main(){ Teacher teach(2018,12,10,1710252149,"xxs","男"); teach.display(); cout<<endl; teach.change();cout<<endl; cout<<"再次显示: "<<endl; teach.display(); }其运行结果如下:
暂时就这么多,这些题都是当时老师给的作业,到现在差不多已经过去一年了,代码我都跑了一遍,都没有问题,至于设计思路我没太注意,可能会有点小毛病,如果有哪里写的不合适了,希望读者说一下,我再改改。