Assignment 9

来源:互联网 发布:淘宝店铺导航分类横着 编辑:程序博客网 时间:2024/06/05 18:14

1.将例11.2的程序片断补充和改写成一个完整、正确的程序,用私有继承方式。在程序中应包括输入数据的函数,在程序运行时输入num, name, sex,age, addr的值,程序应输出以上5个数据的值。

程序代码:

#include <iostream>

#include <string>

using namespace std;

class Student         

{

public:

      void get_value()

      {

           cin >> num>> name >> sex;

      }

      void display()

      {

           cout << "num= " << num << endl;

           cout <<"name = " << name << endl;

           cout << "sex= " << sex << endl;

      }

private:

      int num;

      string name;

      char sex;

};

 

class Student1:private Student

{

public:

      void get_value_1()

      {

           get_value();

           cin >> age>>addr;

      }

      void display_1()

      {

           display();

           cout << "age= " << age << endl;

           cout <<"address = " << addr << endl;

      }

private:

      int age;

      string addr;

};

 

 

int main(int argc,char* argv[])

{

      Student1 stud1;

      cout << "请输入学生的学号,姓名,性别(F/M),年龄,家庭住址:" <<endl;

      stud1.get_value_1();

      stud1.display_1();

      return 0;

}

2. 编写一个程序计算出圆和圆柱体的表面积和体积。

要求:

(1) 定义一个点(point)类,包含数据成员x,y(坐标点),以它为基类,派生出一个circle类(圆类),增加数据成员r(半径),再以circle作为直接基类,派生出一个cylinder(圆柱体)类,再增加数据成员h(高)。设计类中数据成员的访问属性。

(2) 定义基类的派生类圆、圆柱都含有求表面积和体积的成员函数和输出函数。

(3) 定义主函数,求圆、圆柱的面积和体积。

程序代码:

#include <iostream>

using namespacestd;

class Point

{

public:

    Point(doublex = 0,double y = 0)

    {

        dX = x;

        dY = y;

    }

protected:

    double dX;

    double dY;

};

class Circle:publicPoint

{

public:

    Circle(doublex,double y,double r):Point(x,y)

    {

        dRadius= r;

    }

    double evaluteArea();

    double evaluteVolume();

    void show_Circle();

protected:

    double dRadius;

};

double Circle::evaluteArea()

{

    return dRadius * dRadius *3.14;

}

double Circle::evaluteVolume()

{

    return 0;

}

void Circle::show_Circle()

{

    cout<< "所输入的圆的面积是:" << endl;

    cout << evaluteArea() << endl;

    cout << "圆的体积是:" << endl;

    cout << evaluteVolume()<< endl;

}

class Cylinder:public Circle

{

public:

    Cylinder(doublex,double y,double r,double h):Circle(x,y,r)

    {

        dHeight= h;

    }

    double evaluteSurface();

    double evaluteVolume();

    void show_Cylinde();

private:

    doubledHeight;

};

double Cylinder::evaluteSurface()

{

    return 2 *3.14 *dRadius * dHeight;

}

double Cylinder::evaluteVolume()

{

    return 3.14* dRadius * dRadius *dHeight;

}

void Cylinder::show_Cylinde()

{

    cout<< "所输入的圆柱体的面积是:" << endl;

    cout << evaluteSurface() <<endl;

    cout << "所输入的圆柱体的体积是:" << endl;

    cout << evaluteVolume() << endl;

}

int main(intargc,char* argv[])

{

    Circle cir_1(0,0,3);

    cir_1.show_Circle();

    Cylindercyl_1(1,2,3,3);

    cyl_1.show_Cylinde();

    return 0;

}


3. 开发一个名为Vehicle的类层次。创建两个类:Taxi和Truck,它们均以public模式从类Vehicle继承而来。Taxi类应包含一个数据成员,以表明其是否正在载客。Truck类也应该包含一个数据成员,以表明其是否正在载货。添加必要的成员函数,以操作和访问类的数据。编写一个测试程序,将一个Truck对象和一个Taxi对象答应到屏幕上(使用重载的流插入运算符)。

程序代码:

#include <iostream>

using namespacestd;

class Vehicle    //¡§°?°??ä?¨¦¨´¤¨¤

{

public:

    virtual void setBool() = 0;

};

classTaxi:public Vehicle   //¡§°?°??taxi¤¨¤ê??®?¨¬D¢?¨¦¨®¤¨¤

{

public:

    voidsetBool();

    friendostream& operator<<(ostream&,Taxi&);//??¢?¢¡Â?¨???¤?

private:

    bool flag;

};

 

voidTaxi::setBool()

{

    cout << "Pleaseinput 0/1 to set the message:" << endl;

    cin >> flag;

}

ostream&operator<<(ostream& output,Taxi& t)

{

    if (t.flag)

    {

        output<<"The taxi is serving weight!"<<endl;

    }

    else

        output << "Thetaxi is empty!" <<endl;

    returnoutput;

}

class Truck:public Vehicle          //¡§°?°??truck¤¨¤ê??®?¨¬D¢?¨¦¨´¤¨¤

{

public:

    voidsetBool();

    friendostream& operator << (ostream& output,Truck&);

private:

    bool flag;

};

void Truck::setBool()

{

    cout << "Pleaseinput 0/1 to set the truck's message:"<< endl;

    cin >> flag;

}

ostream&operator << (ostream&output,Truck&tr)

{

    if(tr.flag)

    {

        output<< "The truck is loading things!"<< endl;

    }

    else

        output << "Thetruck is empty!" << endl;

    return output;

}

intmain(int argc,char*argv[])

{

    Taxi tax_1;

    tax_1.setBool();

    cout<< tax_1 ;

    Truck tru_1;

    tru_1.setBool();

    cout << tru_1;

    return 0;

}

4. 编写一个程序用于院校的人事管理,要求能够输入、显示各类人员的信息。假设院校中有四类人员:学生、教师、员工和在职进修教师,他们都有姓名、性别、联系电话等共同信息,学生还有年级、专业课程和各课成绩的信息,员工还有所在部门、工资的信息,教师还有所在部门、工资和所授课程的信息,在职进修教师兼有教师和学生两类信息。

程序代码:

#include <iostream>

#include <string>

using namespace std;

classPerson    //¡§°?¨´¤¨¤¨?

{                

protected:

    string name;

    char chSex;

    long lPhone;

};

classStudent:virtualpublicPerson  //¡§°?¡ì¦¨²¤¨¤ê?¡é¨°ã?¨?¤¨¤¡§°?a¨¦¨´¤¨¤

{

public:

    voidset_Message();

    voidshow_Message();

protected:

    int iGrade;

    string tech;

    floatfScore[4];

};

voidStudent::set_Message()

{

    cout << "Pleaseinput the student's name,sex,phone,grade,technology,four scoresêo" << endl;

    cin >> name >> chSex;

    cin >> lPhone >>iGrade>>tech;

    for (int i = 0;i < 4;i ++)

    {

        cin >> fScore[i];

    }

}

void Student::show_Message()

{

    cout << "Student'sname :" << name << endl;

    cout << "sex:"<< chSex << endl;

    cout << "phone:"<< lPhone << endl;

    cout << "grade:"<< iGrade << endl;

    cout << "technology:"<< tech << endl;

    cout << "Student'sfour scores:" << endl;

    for (int i = 0; i < 4; i ++)

    {

        cout << fScore[i] << " " ;

    }

    cout << endl;

}

classTeacher:virtualpublicPerson   //¡§°?¤?º|¤¨¤

{

public:

    voidset_Message();

    voidshow_Message();

protected:

    string depart;

    int iWages;

    string course;

};

voidTeacher::set_Message()

{

    cout << "Pleaseinput teacher's name ,sex,phone,department,wages,course:" <<endl;

    cin >> name >> chSex >>lPhone >> depart >> iWages >> course;

}

voidTeacher::show_Message()

{

    cout <<"Teacher'sname:" << name << endl;

    cout <<"Teacher'ssex:" << chSex << endl;

    cout << "Teacher'sphone:" << lPhone << endl;

    cout << "Teacher'sdepartment:" << depart << endl;

    cout << "Teacher'swage:" <<iWages << endl;

    cout << "Teacher'scourse:" << course << endl;

}

classStaff:virtualpublicPerson       //¡§°?¡ã¡è¤¨¤

{

public:

    voidset_Message();

    voidshow_Message();

private:

    string depart;

    int iWage;

};

voidStaff::set_Message()

{

    cout << "Pleaseinput the staff's name,sex,phone,department,wage:" << endl;

    cin >> name >> chSex >>lPhone >> depart >> iWage ;

}

voidStaff::show_Message()

{

    cout << "Thestaff's name:" << name << endl;

    cout << "Thestaff's sex:" << chSex << endl;

    cout << "Thestaff's phone:" << lPhone << endl;

    cout << "Thestaff's department:" << depart << endl;

    cout << "Thestaff's wage:" << iWage << endl;

}

classRefresher:public Student,public Teacher

{

public:

    voidset_Message();

    voidshow_Message();

};

voidRefresher::set_Message()

{

    cout << "Pleaseinput refresher's name,sex,phone,grade-" << endl;

    cout << "technology,fourscores,department,wage,course:" << endl;

    cin >> name >> chSex >>lPhone >> iGrade >> tech;

    for (int i = 0;i < 4; i ++)

    {

        cin >> fScore[i];

    }

    cin >> depart >> iWages >>course;

}

voidRefresher::show_Message()

{

    cout << "Therefresher's name:"<< name << endl;

    cout << "Therefresher's sex:" << chSex << endl;

    cout << "Therefresher's phone:" << lPhone << endl;

    cout << "Therefresher's grade:"<< iGrade << endl;

    cout << "Therefresher's technology:" << tech << endl;

    cout << "Therefresher's four scores:" << endl;

    for (int i = 0; i < 4; i ++)

    {

        cout << fScore[i] <<" ";

    }

    cout << "Therefresher's department:" << depart << endl;

    cout << "Therefresher's wage:" << iWages <<endl;

    cout << "Therefresher's course:" << course << endl;

}

intmain(int argc,char*argv[])

{

    Student stu;

    Teacher tea_1;

    Staff sta_1;

    Refresher ref_1;

    cout << "Inputthe student's message:" << endl;

    stu.set_Message();

    stu.show_Message();

    cout << "Inputthe teacher's message:" << endl;

    tea_1.set_Message();

    tea_1.show_Message();

    cout << "Inputthe staff's message:" << endl;

    sta_1.set_Message();

    sta_1.show_Message();

    cout << "Inputthe refresher's message:" << endl;

 

    ref_1.set_Message();

    ref_1.show_Message();

    return 0;

}