C++ 类与对象 附加练习题

2021/5/22 20:27:53

本文主要是介绍C++ 类与对象 附加练习题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

6-1 Point类的运算 (10 分)
定义Point类,有坐标x,y两个私有成员变量;对Point类重载“+”(相加)、“-”(相减)和“==”(相等)运算符,实现对坐标的改变,要求用友元函数和成员函数两种方法实现。对Point类重载<<运算符,以使得代码
Point p; cout<<p<<endl;可以输出该点对象的坐标。
函数接口定义:
实现Point类。

裁判测试程序样例:

/* 请在这里填写答案 */



int main(int argc, char const *argv[])
{
    Point p1(2,3);
    cout<<p1<<endl;
    Point p2(4,5);
    cout<<p2<<endl;
    Point p3 = p1+p2;    
    cout<<p3<<endl;
    p3 = p2-p1;
    cout<<p3<<endl;
    p1 += p2;
    cout<<p1<<endl;
    cout<<(p1==p2)<<endl;
    return 0;
}

输入样例:
无

输出样例:
在这里给出相应的输出。例如:

2,3
4,5
6,8
2,2
6,8
0

我的代码:

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



class Point{
    int x,y;
public:
    Point(int a,int b){
        x = a;
        y = b;
    }

    Point operator+(const Point &point2){
        return Point(x + point2.x,y + point2.y);
    }

    Point operator-(const Point &point2){
        return Point(x - point2.x,y - point2.y);
    }

    void operator+=(const Point &point2){
        x += point2.x;
        y += point2.y;
    }

    bool operator==(const Point &point2){
        if (x == point2.x && y == point2.y){
            return true;
        }
        else
            return false;
    }

    friend ostream& operator<<(ostream &out, Point &point){
        out<<point.x<<","<<point.y;
        return out;
    }
};



int main()
{
    Point p1(2,3);
    cout<<p1<<endl;
    Point p2(4,5);
    cout<<p2<<endl;
    Point p3 = p1+p2;
    cout<<p3<<endl;
    p3 = p2-p1;
    cout<<p3<<endl;
    p1 += p2;
    cout<<p1<<endl;
    cout<<(p1==p2)<<endl;
    return 0;
}

6-2 车的不同行为 (10 分) 定义一个车(vehicle)基类,有虚函数Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,它们都有Run、Stop等成员函数。完成这些类使得主函数可以运行并得到正确的输出结果。
函数接口定义: 完成类代码

裁判测试程序样例:

/* 请在这里填写答案 */
int main(int argc, char const *argv[])
{
    Vehicle veh;
    Bicycle bic;
    Motorcar mot;
    run(veh);
    run(bic);
    run(mot);
    return 0;
}
输入样例:
无

输出样例:
在这里给出相应的输出。例如:

Vehicle run
Bicycle run
Motorcar run

我的代码:

#include <iostream>
using namespace std;

class Vehicle{
public:
    virtual void run(){
        cout<<"Vehicle run\n";
    }
};

class Bicycle:public Vehicle{
public:
    virtual void run(){
        cout<<"Bicycle run\n";
    }
};

class Motorcar:public Vehicle{
public:
    virtual void run(){
        cout<<"Motorcar run\n";
    }
};

void run(Vehicle &a){
    a.run();
}

int main()
{
    Vehicle veh;
    Bicycle bic;
    Motorcar mot;
    run(veh);
    run(bic);
    run(mot);
    return 0;
}

6-3 点和线段 (20 分) 已知表示点的类CPoint和表示线段的CLine类, 类CPoint包含:(1)表达点位置的私有数据成员x,y (2)构造函数及复制构造函数 类CLine包含: (1)两个CPoint的点对象(该两点分别为线段的两个端点) (2)构造函数(提示:构造函数中用初始化列表对内嵌对象进行初始化) (3)公有成员函数GetLen,其功能为返回线段的长度,返回值类型为整型 (4)类属性成员count用于记录创建的CLine类对象的个数,及用于显示count值的ShowCount函数; 要求: (1)实现满足上述属性和行为的CPoint类及CLine类定义; (2)保证如下主函数能正确运行。

裁判测试程序样例:

/* 请在这里填写答案 */
int main(){
     int x,y;
     cin>>x>>y;
     CPoint p1(x,y);
     cin>>x>>y;
     CPoint p2(x,y);
     CLine line1(p1,p2);
     cout<<"the length of line1 is:"<<line1.GetLen()<<endl;
     CLine line2(line1);
     cout<<"the length of line2 is:"<<line2.GetLen()<<endl;
     cout<<"the count of CLine is:"<<CLine::ShowCount()<<endl; 
     return 0;
}
输入样例:
在这里给出一组输入。例如:

1 1 
4 5
输出样例:
在这里给出相应的输出。例如:

The length of line1 is:5
The length of line2 is:5
The count of Line is:2

我的代码:

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
/* 请在这里填写答案 */
class CPoint{
    int x,y;
public:
    CPoint(){
        x = 0;
        y = 0;
    }
    CPoint(int a,int b){
        x = a;
        y = b;
    }
    CPoint(const CPoint &obj){
        x = obj.x;
        y = obj.y;
    }
    int getx(){
        return x;
    }
    int gety(){
        return y;
    }

};

class CLine:public CPoint{
    CPoint p1,p2;
    static int count;
public:
    CLine(const CLine &l){
        p1 = l.p1;
        p2 = l.p2;
        count++;
    }
    CLine(CPoint a,CPoint b){
        p1 = a;
        p2 = b;
        count++;
    }
    int GetLen(){
        return sqrt(pow(p1.getx()-p2.getx(),2) + pow(p1.gety()-p2.gety(),2));
    }
    static int ShowCount(){
        return count;
    }
};

int CLine::count = 0;

int main(){
    int x,y;
    cin>>x>>y;
    CPoint p1(x,y);
    cin>>x>>y;
    CPoint p2(x,y);
    CLine line1(p1,p2);
    cout<<"the length of line1 is:"<<line1.GetLen()<<endl;
    CLine line2(line1);
    cout<<"the length of line2 is:"<<line2.GetLen()<<endl;
    cout<<"the count of CLine is:"<<CLine::ShowCount()<<endl;
    return 0;
}



这篇关于C++ 类与对象 附加练习题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程