Fork me on Gitee

C++题目东华

1. 定义一个点类Point,其有两个double型的私有数据成员x和y。此外还包含以下公有成员函数:

(1)构造函数,给点初始化;

(2)setPoint函数,设置点坐标值;

(3)distance函数,计算两点间的距离并返回,返回类型为double;

除此之外,需要重载运算符>>,用于输入点坐标;重载运算符<<,用于输出点坐标。输入输出格式见下面运行示例。

下面是该类的测试程序,请设计出类Point。

int main( )

{

    Point a, b(9, 9);

    cin>>a;

    cout<<a<<endl;

    cout<<b<<endl;

    a.setPoint(7, 7);

    cout<<"the distance of "<<a<<" and "<<b<<" is "<<a.distance(b)<<endl;

    return 0;

}

当输入5 6↙时,输出为:

(5,6)

(9,9)

the distance of (7,7) and (9,9) is 2.82843

2. 设计Person类,其有3个private数据成员,string类型的code和name,分别表示编号和姓名;char 类型的sex,表示性别;两个public成员函数,input函数用于输入数据给数据成员,output函数用于输出数据成员的值。

在此基础上派生出Student类,其有1个private数据成员,int类型的score,表示分数;有1个public成员函数input用于输入数据给数据成员。除此需要重载<<运算符。

请编写Person和Student类,并可使用提供的main函数测试。

int main( ) {

     Student s1, s2;

     s1.input();

     s2.input();

     cout<<s1<<s2;

     return 0;

}

下面是某次运行的情况,其中带下划线的为输入,其它为输出。

请输入编号 姓名 性别:101 zhang f

请输入成绩:90

请输入编号 姓名 性别:103 yuan m

请输入成绩:89

101 zhang f 90

103 yuan m 89

3.已知基类Building有3个int类型protected数据成员level、room、area;通过public继承获得2个派生类,派生类Housing有2个int类型private数据成员livingroom、bathroom;派生类Office有2个int类型private数据成员telephone、extinguisher。请设计这3个类,并编写合适的成员函数,要求使用以下main函数测试时,输出结果如下图:

wps9int main( ) {

     Building *f;

     Housing hos(5,3,140,2,2);

     Office ofc(2,12,500,12,2);

     f=&hos;

     f->show();

     f=&ofc;

     f->show();

     return 0;

}

答案:

#include<iostream>
#include<cmath>
using namespace std;

class Building
{
 protected:
   int level,room,area;
  public:
   virtual  void *show(){
    
 }
};

class Housing:public Building{
    private:
      int livingroom,bathroom;
    public:
        Housing(const int &level_, const int & room_, int area_, int livingroom_,int bathroom_){
            level=level_;
            room=room_;
            area=area_;
            livingroom=livingroom_;
            bathroom=bathroom_;
        }
    void *show(){
        cout<<"住宅楼:"<<endl;
        cout<<"楼 层:"<<level<<endl;
        cout<<"房间数:"<<room<<endl;
        cout<<"总面积:"<<area<<endl;
        cout<<"厅 数:"<<livingroom<<endl;
        cout<<"浴室数:"<<bathroom<<endl<<endl;
     }
    
};
class Office:public Building{
    private:
    int telephone,extinguisher;
    public:
    Office(const int &level_, const int & room_, int area_, int telephone_,int extinguisher_){
            level=level_;
            room=room_;
            area=area_;
            telephone=telephone_;
            extinguisher=extinguisher_;
        }
    void *show(){
        cout<<"办公楼:"<<endl;
        cout<<"楼 层:"<<level<<endl;
        cout<<"房间数:"<<room<<endl;
        cout<<"总面积:"<<area<<endl;
        cout<<"电话数:"<<telephone<<endl;
        cout<<"灭火器数:"<<extinguisher<<endl;
     }
};
int main( ) {
     Building *f;
     Housing hos(5,3,140,2,2);
     Office ofc(2,12,500,12,2);
     f=&hos;
     f->show();
     f=&ofc;
     f->show();
     return 0;
}


#include<iostream>
#include<cmath>
using namespace std;

class Person
{
public:
    void input(const string name_, const string  code_, char sex_){
        name=name_;
        code=code_;
        sex=sex_; 
        output();
    }
    void output(){
        cout<<code<<" "<<name<<" "<<sex<<" ";
    }
private:
    string name;
    string code;
    char sex;
};

class Student:public Person{
    private:
    int score;
    public:
    friend ostream & operator<<( ostream & os,const Person & c);
    void input(){
        cout<<"请输入编号 姓名 性别:";
        string name1,code1;
        char sex1;
        cin>>code1;
        cin>>name1;
        cin>>sex1;
        cout<<"请输入成绩:";
        cin>>score;
        Person::input(name1, code1, sex1);
        cout<<score<<endl;;
    }
};
ostream & operator<<( ostream & os,const Person & c)
{    
    return os;
}
#include<iostream>
#include<cmath>
using namespace std;

class Point
{
public:
    Point(double x=0, double y=0){
        this->x= x;
        this->y = y;
    }
    void setPoint(double x, double y) 
    {
        this->x = x;
        this->y = y;
    }
    friend ostream & operator<<( ostream & os,const Point & c);
    friend istream & operator>>( istream & is,Point & c);
    double distance(const Point &c) 
    {
        int x1 =x- c.x;  
        int x2 =y- c.y;  
        return sqrt(x1*x1+x2*x2);  
    }
private:
    double x;
    double y;
};
 ostream & operator<<( ostream & os,const Point & c)
{    
    os << "(" << c.x<<","<<c.y<< ")"; 
    return os;
}
istream & operator>>( istream & is,Point & c)
{
    cin>>c.x>>c.y;
    return is;
}
int main( )
{
    Point a, b(9, 9);
    cin>>a;
    cout<<a<<endl;
    cout<<b<<endl;
    a.setPoint(7,7);
    cout<<"the distance of "<<a<<" and "<<b<<" is "<<a.distance(b)<<endl;
    return 0;
}
posted @ 2021-01-14 11:56  ---dgw博客  阅读(310)  评论(0编辑  收藏  举报