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;
}


7-1 运算符重载 (10 分) 请定义一个分数类,拥有两个整数的私有数据成员,分别表示分子和分母(分母永远为正数,符号通过分子表示)。 重载运算符加号"+",实现两个分数的相加,所得结果必须是最简分数。
输入: 第一行的两个数 分别表示 第一个分数的分子和分母(分母不为0)。 第二行的两个数 分别表示 第二个分数的分子和分母。
输出: 第一个数表示分子,第二个数表示分母(若分数代表的是整数,则不输出分母)。

输入:
第一行的两个数 分别表示 第一个分数的分子和分母(分母不为0)。 第二行的两个数 分别表示 第二个分数的分子和分母。

输出:
第一个数表示分子,第二个数表示分母(若分数代表的是整数,则不输出分母)。

输入样例:
1  5
2  5
输出样例:
3 5

收获:

1.约分分数的好方法转载自ataraxy_thinking):

/*分数化简,关键是求出分母和分子的最大公因数。
  这里采用“辗转相除法”求两个整数的最大公因数。
  辗转相除法, 又名欧几里德算法(Euclidean algorithm),是求最大公约数的一种方法。
  它的具体做法是:用较大数除以较小数,再用出现的余数(第一余数)去除除数,再用出现的余数(第二余数)去除第一余数,
  如此反复,直到最后余数是0为止。如果是求两个数的最大公约数,那么最后的除数就是这两个数的最大公约数。
*/
 
#include<iostream>
 
using namespace std;
 
int main() {
	int n, m;	
	cout << "请分别输入分母和分子:" << endl;
	cin >> m >> n;
	int m0 = m, n0 = n;    //m0和n0保存m和n的原始值
	while (m%n != 0) {
		int temp = m ;
		m = n;
		n = temp%n;
	}
	cout << "原分式的最简分式为: " << n0 / n << "/" << m0 / n << endl;		                                                   
}

区分大小的情况如下:

int a = t.A, b = t.B;
	while (a % b != 0)
	{
		int temp = a;
		a = b;
		b = temp % b;
	}
	int m = (abs(a) < abs(b) ? a : b);
	t.A /= m;
	t.B /= m;

2.要注意分子等于0的情况!

我的代码:

#include <iostream>
using namespace std;

class Fraction{
    int son,mon;
public:
    Fraction(){};
    Fraction(int x,int y){
        son = x;
        mon = y;
    }
    Fraction operator+(Fraction &obj){
        Fraction f;

        f.mon = mon * obj.mon;
        f.son = son * obj.mon + obj.son *mon;

        int a = f.son;
        int b = f.mon;

        if (a == 0){
            return f;
        }
        while (a % b != 0)
        {
            int temp = a;
            a = b;
            b = temp % b;
        }
        int m = (abs(a) < abs(b) ? a : b);
        f.son /= m;
        f.mon /= m;
        return f;
    }

    Fraction(const Fraction &obj){
        son = obj.son;
        mon = obj.mon;
    }

    int getson(){
        return son;
    }
    int getmon(){
        return mon;
    }
};

int main(){
    int a,b;
    cin>>a>>b;
    Fraction f1(a,b);
    cin>>a>>b;
    Fraction f2(a,b);
    Fraction f3(f1 + f2);
    if (f3.getmon() != 1 && f3.getson() != 0) {
        printf("%d %d", f3.getson(), f3.getmon());
    }
    else
        cout<<f3.getson();
}

7-2 类的继承与派生 (25 分)
定义平面二维点类CPoint,有数据成员x坐标,y坐标,函数成员(构造函数复制构造函数、虚函数求面积GetArea,虚函数求体积函数GetVolume、输出点信息函数print。由CPoint类派生出圆类Cirle类(新增数据成员半径radius),函数成员(构造函数、复制构造函数、求面积GetArea,虚函数求体积函数GetVolume、输出圆信息函数print。 再由Ccirle类派生出圆柱体Ccylinder类(新增数据成员高度height),函数成员(构造函数、复制构造函数、求表面积GetArea,求体积函数GetVolume、输出圆柱体信息函数print。在主函数测试这个这三个类。

输入格式:
0 0 例如:第一行读入圆心坐标。 1 2 第二行读入半径与高度。

输出格式:
分四行输出,分别代表圆心、底面积、表面积、体积。

输入样例:
在这里给出一组输入。例如:

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

Center:(0,0)
radius=1
height:2
BasalArea:3.14159
SupfaceArea:18.8496
Volume:6.28319

别人的代码(转自Here):

#include <iostream>
#include <math.h>
#define PI 3.1415926
using namespace std;

//点
class CPoint{ 
	double X,Y;
public:
	CPoint(double x,double y);
	CPoint(CPoint &cP);
	virtual double GetArea(){}//求面积
	virtual double GetVolume(){}//求体积
	void print();
};
CPoint::CPoint(double x,double y):X(x),Y(y){}
CPoint::CPoint(CPoint &cP):X(cP.X),Y(cP.Y){}
void CPoint::print(){ cout<<"Center:("<<X<<","<<Y<<")"<<endl; }

//圆 
class Cirle :public CPoint{
	double Radius;
public:
	Cirle(double x,double y,double r);	
	Cirle(Cirle& c);
	double GetRadius(){ return Radius; }
	double GetArea();//求面积
	virtual double GetVolume();//求体积
	void print();
};
Cirle::Cirle(double x,double y,double r):CPoint(x,y),Radius(fabs(r)){} 
Cirle::Cirle(Cirle& c):CPoint(c),Radius(c.Radius){}
double Cirle::GetArea(){ return PI*Radius*Radius; }
double Cirle::GetVolume(){ return 3*PI*Radius*Radius*Radius/4; }
void Cirle::print(){ cout<<"radius="<<GetRadius()<<endl; }

//圆柱
class Ccylinder :public Cirle{ 
	double Height;
public:
	Ccylinder(double x,double y,double r,double h);
	Ccylinder(Ccylinder &cc);
	double GetHeight(){ return Height; }
	double GetArea();//求面积
	virtual double GetVolume();//求体积 
	void print();
};
Ccylinder::Ccylinder(double x,double y,double r,double h):Cirle(x,y,r),Height(fabs(h)){} 
Ccylinder::Ccylinder(Ccylinder& cc):Cirle(cc),Height(cc.Height){}
double Ccylinder::GetArea(){ return PI*GetRadius()*GetRadius(); }
double Ccylinder::GetVolume(){ return PI*GetRadius()*GetRadius()*Height; }
void Ccylinder::print(){
	cout<<"height:"<<GetHeight()<<endl;
	cout<<"BasalArea:"<<GetArea()<<endl;
	cout<<"SupfaceArea:"<<(2*GetArea()+2*PI*GetRadius()*Height)<<endl;
	cout<<"Volume:"<<GetVolume()<<endl;
}
 
int main(){
	double x,y,r,h;
	cin>>x>>y>>r>>h;
	CPoint cp(x,y);
	Cirle c(x,y,r);
	Ccylinder cc(x,y,r,h);
	cp.print();
	c.print();
	cc.print();
	return 0;
} 

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 点类Point)是一个表示二维平面上点的类,它包含两个私有成员变量x和y,分别表示点的横坐标和纵坐标点类还包含一些公有成员函数,用于获取和设置点的坐标,计算两点之间的距离等操作。例如: class Point { private: double x; // 横坐标 double y; // 纵坐标 public: Point(); // 默认构造函数 Point(double x, double y); // 带参构造函数 double getX() const; // 获取横坐标 double getY() const; // 获取纵坐标 void setX(double x); // 设置横坐标 void setY(double y); // 设置纵坐标 double distance(const Point& p) const; // 计算两点之间的距离 }; 其中,构造函数用于初始化点的坐标,getX和getY函数用于获取点的坐标,setX和setY函数用于设置点的坐标,distance函数用于计算两点之间的距离。在实现distance函数时,可以使用勾股定理计算两点之间的距离: double Point::distance(const Point& p) const { double dx = x - p.x; double dy = y - p.y; return sqrt(dx * dx + dy * dy); } 这样,我们就可以使用点类来表示二维平面上的点,并进行一些常见的操作,如计算两点之间的距离等。 ### 回答2: 点是二维平面上的一个位置,它有两个坐标,分别是x和y。因此,设计一个表示二维平面上点的类point,需要包含以下成员变量和成员函数: 1. 成员变量:x, y,分别表示点的坐标, 2. 成员函数构造函数和析构函数。其中,构造函数可以重载多个版本,以便于用户选择不同的方式进行初始化,如没有参数、有一个参数或者有两个参数的构造函数。析构函数用于释放point类所占用的资源,一般情况下不需要特殊定义, 3. 成员函数:getX()和getY(),用于获得点的坐标值。setX()和setY(),用于设置点的坐标值。print()函数用于输出点的坐标值, 根据上述要求,point类的框架如下: class point{ private: double x, y; public: point();//无参构造函数 point(double x, double y);//有参构造函数 point(double a[]);//初始化坐标数组 double getX();//获取x坐标 double getY();//获取y坐标 void setX(double x);//设置x坐标 void setY(double y);//设置y坐标 void print();//输出点的坐标 ~point();//析构函数 }; 接下来,我们分别对这些成员变量和成员函数进行详细介绍: 成员变量: private: double x, y; 成员变量包含在类中,是为了方便存储和操作数据。这里定义了两个坐标x和y,并将它们声明为private类型,即私有的成员变量。这种方式可以避免无意中对成员变量进行误操作。 构造函数point();//无参构造函数 point(double x, double y);//有参构造函数 point(double a[]);//初始化坐标数组 构造函数是用来创建对象并初始化对象的,与类同名。在point类中,我们需要定义多条构造函数,以便于用户选择不同的方式对点进行初始化。 无参构造函数point(),用于创建一个空的点,即x和y坐标都是0。 有参构造函数point(double x, double y),用于用给定的坐标值创建一个点。 初始化坐标数组:point(double a[]),用于将一个包含两个元素的数组作为参数,用这个数组初始化点的坐标。 getX()和getY(): double getX();//获取x坐标 double getY();//获取y坐标 getX()和getY()函数分别用于获取点的x坐标和y坐标。它们都是基本访问函数,因此在声明中加上const修饰符。 setX()和setY(): void setX(double x);//设置x坐标 void setY(double y);//设置y坐标 setX()和setY()函数分别用于设置点的x坐标和y坐标,用于更改点的坐标值。这两个函数都不需要返回值,它们都是修改成员变量的值。 print()函数: void print();//输出点的坐标 print()函数用于输出点的坐标值,可以用来查看点的坐标值。它可以通过cout函数输出,也可以将坐标值存储在字符串中。 析构函数: ~point();//析构函数 析构函数是用来清理对象并释放对象所占用资源的函数。在point类中,一般情况下不需要特殊定义析构函数,因为point类并不涉及动态内存分配。如果类中包含了指针类型的成员变量,那么析构函数就需要删除这些指针所指向的动态内存。 ### 回答3: point类是一个表示二维平面上具有x坐标和y坐标的点的类。该类应该包含以下数据成员成员函数数据成员: 1. x坐标(double类型) 2. y坐标(double 类型) 成员函数: 1. 构造函数point类应该至少有两个构造函数,用于初始化x坐标和y坐标,一个不带任何参数的默认构造函数,另一个带有两个参数用于初始化x和y坐标。 2. 复制构造函数:用于复制另一个point对象。 3. get_x()和get_y():用于获取点的x坐标和y坐标。 4. set_x(double x)和set_y(double y):用于设置点的x坐标和y坐标。 5. distance_to(const point& p):用于计算当前点与另一个点p之间的距离。通过计算勾股定理得到两个点的距离。 6. to_string():用于将该点转换为字符串形式,例如"(x, y)"。 在这个point类中,我们可以定义一些运算符重载函数。例如: 1. “+”运算符重载:使用该运算符,可以实现两个point对象相加的操作,返回一个新的point对象,其x和y坐标都是其中两个点的坐标之和。 2. “-”运算符重载:使用该运算符,可以实现两个point对象相减的操作,返回一个新的point对象,其x和y坐标都是其中两个点的坐标之差。 3. “<<”运算符重载:使用该运算符,可以实现将point对象的x和y坐标以字符串形式输出。 这些成员函数数据成员足以完成point类的设计,可以方便地表示二维平面上的点,并对这些点进行一些基本的运算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值