C++题目求解答

1. 已知一个名为Complex的复数类,这个类包含:
(1)私有成员:实部、虚部,且均为int 型
(2)公有的带默认形参值的构造函数、复制构造函数
(3)公有成员函数Display,其作用为显示复数
要求:
(1)实现满足上述属性和行为的Complex类定义;
(2)设计函数AddComplex,函数AddComplex功能为实现两个复数相加,要求该函数的形参为复数类的常引用;
(3)保证如下主函数能正确运行,
int main(){
Complex c0(2,-3);
Complex c1(c0);
cout<<"c1 is: ";
c1.Display();
Complex c2(3,4);
cout<<"c2 is: ";
c2.Display();
Complex c3;
c3 = AddComplex(c1,c2);
cout<<"c3 is: ";
c3.Display();
return 0;
}
且输出结果如下:
c1 is: 2-3i
c2 is: 3+4i
c3 is: 5+1i

2.已知表示点的类CPoint和表示线段的CLine类,
类CPoint包含:(1)表达点位置的私有数据成员x,y
(2)构造函数及复制构造函数
类CLine包含:
(1)两个CPoint的点对象(该两点分别为线段的两个端点)
(2)构造函数(提示:构造函数中用初始化列表对内嵌对象进行初始化)
(3)公有成员函数GetLen,其功能为返回线段的长度
(4)类属性成员count用于记录创建的CLine类对象的个数,及用于显示count值的ShowCount函数;
要求:
(1)实现满足上述属性和行为的CPoint类及CLine类定义;
(2)保证如下主函数能正确运行,
#include <iostream>
using namespace std;
int main(){
CPoint p1(1,1);
CPoint p2(4,5);
CLine line1(p1,p2);
cout<<"the length of line1 is: "<<line1.GetLen()<<endl;
CLine line2(line1);
cout<<"The count of line is: "<<CLine::ShowCount()<<endl;
return 0;
}

且输出结果如下:

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

3. 创建一个表示点的类Point,运用组合的方式创建圆形类。 圆形有个成员函数isPointIn用于判断一个点是否在该圆形的内部。 完成对应类代码,使得主函数可以正确运行。

int main(int argc, char const *argv[])
{
int x,y;
double r;
cin>>x>>y>>r;
Point p(x,y);
Circle c(p,r);
cin>>x>>y;
Point p1(x,y);
bool b = c.isPointIn(p1);
cout<<b<<endl;
}

输入
1 1 1.5 2 2

输出
1

// 给你个例子,仅供参考
#include <iostream>
#include <cmath>
using namespace std;

// 1.
class Complex {
public:
    Complex(int _real = 0, int _image = 0) : real(_real), image(_image) {}
    Complex(const Complex & obj) {
        real = obj.real; image = obj.image;
    }

    void Display() {
        if (0 == image)
            cout << real << endl;
        else
            cout << real << (image < 0 ? "" : "+") << image << "i" << endl;
    }

    int get_real() const {
        return real;
    }

    int get_image() const {
        return image;
    }

private:
    int real;
    int image;
};

Complex AddComplex(const Complex & c1, const Complex c2)
{
    return Complex(c1.get_real() + c2.get_real(), c1.get_image() + c2.get_image());
}

// 2.
class CPoint {
public:
    CPoint() : x(0), y(0) {}
    CPoint(int _x, int _y) : x(_x), y(_y) {}
    CPoint(const CPoint & point) {
        x = point.x;
        y = point.y;
    }

    int getx() const {
        return x;
    }

    int gety() const {
        return y;
    }

private:
    int x;
    int y;
};

class CLine {
public:
    CLine(const CPoint & point1, const CPoint & point2) : ep1(point1), ep2(point2) {
        ++count;
    }
    CLine(const CLine & line) {
        ep1 = line.ep1;
        ep2 = line.ep2;
        ++count;
    }
    CLine operator = (const CLine & line) {
        if (this == &line)
            return *this;

        ep1 = line.ep1;
        ep2 = line.ep2;
        ++count;

        return *this;
    }
    ~CLine() {
        --count;
    }

    float GetLen() const {
        return sqrt(pow(ep1.getx() - ep2.getx(), 2) + pow(ep1.gety() - ep2.gety(), 2));
    }
    static int ShowCount(){
        return count;
    }

private:
    CPoint ep1;
    CPoint ep2;
    static int count;
};

int CLine::count = 0;

// 3.
class Point {
public:
    Point(int _x, int _y) : x(_x), y(_y) {}
    int getx() const {
        return x;
    }
    int gety() const {
        return y;
    }

private:
    int x;
    int y;
};

class Circle {
public:
    Circle(const Point & point, double r) : center(point), radius(r) {}
    bool isPointIn(const Point & point) {
        return abs(point.getx() - center.getx()) <= radius && abs(point.gety() - center.gety()) <= radius;
    }

private:
    Point center;
    double radius;
};

int main()
{
    Complex c0(2, -3);
    Complex c1(c0);
    cout << "c1 is: ";
    c1.Display();
    Complex c2(3, 4);
    cout << "c2 is: ";
    c2.Display();
    Complex c3 = AddComplex(c1, c2);
    cout << "c3 is: ";
    c3.Display();

    CPoint p1(1,1);
    CPoint p2(4,5);
    CLine line1(p1,p2);
    cout<<"the length of line1 is: "<<line1.GetLen()<<endl;
    CLine line2(line1);
    cout<<"The count of line is: "<<CLine::ShowCount()<<endl;

    int x, y;
    double r;
    cin >> x >> y >> r;
    Point p(x, y);
    Circle c(p, r);
    cin >> x >> y;
    Point p3(x, y);
    bool b = c.isPointIn(p3);
    cout << b << endl;

    return 0;
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答