题目 3、类的定义与基本操作

class Point {
double m_x = 0, m_y = 0;
public:
Point(double x=0, double y=0) : m_x(x), m_y(y) {
cout << “Constructor of Point” << endl;
}
Point(const Point &p) :m_x(p.m_x), m_y(p.m_y) {
cout << “Copy constructor of Point” << endl;
}
~Point() {
cout << “Destructor of Point” << endl;
}
};
class Circle {
Point m_center; double m_radius = 1.0;
public:
Circle(double r=1, const Point &p=Point()) :m_center§, m_radius® {
cout << “Constructor of Circle” << endl;
}
~Circle() {
cout << “Destructor of Circle” << endl;
}
};
int main()
{
Circle a(2, Point(1, 1));
cout << “end” << endl;
return 0;
}

  1. 说明上述程序执行流程和输出结果;
    Circle a(2, Point(1, 1))首先进入circle类中 执行:
    Circle(double r=1, const Point &p=Point()) :m_center§, m_radius® {
    cout << “Constructor of Circle” << endl;
    }
    需要先进入point类中point(1,1)执行
    Point(double x=0, double y=0) : m_x(x), m_y(y) {
    cout << “Constructor of Point” << endl;
    } //输出Constructor of Point
    随后执行
    Point(const Point &p) :m_x(p.m_x), m_y(p.m_y) {
    cout << “Copy constructor of Point” << endl;
    } //输出Copy constructor of Point
    随后输出:"Constructor of Circle
    输出end,
    生命期结束,进行析构函数,
    输出Destructor of Circle
    输出Destructor of Point

  2. 在Point类中完善获取点的横坐标、获取点的纵坐标成员函数,并在主函数中测试;

  3. 通过友元函数实现平面上任意两个点的距离计算辅助函数;

  4. 在Circle类中完善圆的面积计算与圆的周长计算成员函数,并在主函数中测试;

#include
#include
using namespace std;
class Point {
double m_x = 0, m_y = 0;
public:
Point(double x = 0, double y = 0) : m_x(x), m_y(y) {
cout << “Constructor of Point” << endl;
}
Point(const Point& p) :m_x(p.m_x), m_y(p.m_y) {
cout << “Copy constructor of Point” << endl;
}
double getx()const{ return m_x; }//获取横坐标,这里应该用double进行定义,返回值为double类型。
double gety()const{ return m_y; }//获取纵坐标
friend double len(const Point& a, const Point& b);
~Point() {
cout << “Destructor of Point” << endl;
}
};
double len(const Point& a, const Point& b) {
double s = a.m_x - b.m_x, m = a.m_y - b.m_y;
double length = sqrt(s * s + m * m);
return length;
}
class Circle {
Point m_center; double m_radius = 1.0;
public:
Circle(double r = 1, const Point& p = Point()) :m_center§, m_radius® {
cout << “Constructor of Circle” << endl;
}
double vol(const Circle p) {
double result;
result = p.m_radius * p.m_radius * 3.14;
return result;
}
double c(const Circle p) {
double result;
result = 2 * 3.14 * p.m_radius;
return result;
}
~Circle() {
cout << “Destructor of Circle” << endl;
}
};

在主函数所在的文件里:
#include “point.h”
#include
using namespace std;

int main()
{
Circle a(2, Point(1, 1));
Point b(1, 1);
Point c(2, 2);
cout <<“圆心的坐标为:”<< b.getx() <<" "<< b.gety();
cout << “圆的面积为:” << a.vol(a) << endl;
cout << “圆的周长为:” << a.c(a) << endl;
cout << “坐标轴上(1.1)与(2.2)的距离为” << len(c, b) << endl;
cout << “end” << endl;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值