(2022级)成都工业学院面向对象程序设计(C++)实验五:多态性

文章介绍了如何在C++中实现复数类Complex的加法运算,包括与整数和另一个复数的运算,以及如何重载运算符为二维数组进行加法和减法操作。还展示了如何使用抽象基类和多态性计算立方体、球体和圆柱体的表面积和体积。
摘要由CSDN通过智能技术生成

写在前面

1、基于2022级计算机类实验指导书

2、代码仅提供参考

3、如果代码不满足你的要求,请寻求其他的途径

运行环境

window11家庭版

CLion 2023.2.2

实验要求、源代码和运行结果

1、定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中一个是整数,顺序任意。例如c1+c2,i+c1,c1+i均合法(设i为整数,c1、c2为复数.)编写程序,分别求两个复数之和,整数和复数之和。

#include <iostream>
using namespace std;

class Complex {
private:
    double real;  // 复数的实部
    double imag;  // 复数的虚部

public:
    Complex() : real(0), imag(0) {}

    Complex(double r, double i) : real(r), imag(i) {}

    Complex operator+(const Complex& other) const {
        Complex result;
        result.real = this->real + other.real;
        result.imag = this->imag + other.imag;
        return result;
    }

    Complex operator+(const int& num) const {
        Complex result;
        result.real = this->real + num;
        result.imag = this->imag;
        return result;
    }

    friend Complex operator+(const int& num, const Complex& comp);

    void display() const {
        cout << real << " + " << imag << "i" << endl;
    }
};

Complex operator+(const int& num, const Complex& comp) {
    Complex result;
    result.real = num + comp.real;
    result.imag = comp.imag;
    return result;
}

int main() {
    Complex c1(2.5, 3.7);
    Complex c2(1.2, -2.8);

    Complex result1 = c1 + c2;
    Complex result2 = 5 + c1;
    Complex result3 = c2 + 10;

    cout << "c1 + c2 = ";
    result1.display();

    cout << "5 + c1 = ";
    result2.display();

    cout << "c2 + 10 = ";
    result3.display();

    return 0;
}

2、编写一个程序,用成员函数重载运算符“+”和“-”将两个二维数组相加和相减,要求第一个二维数组的值由构造函数设置,另一个二维数组的值由键盘输入。

成员函数重载运算符“+”示例:

Array Array::operator+(Array& B)
{
    Array A1;
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            A1.a[i][j] = a[i][j] + B.a[i][j];
        }
    }
    return A1;
}
#include <iostream>
using namespace std;

class Complex {
private:
    double real;  // 复数的实部
    double imag;  // 复数的虚部

public:
    Complex() : real(0), imag(0) {}

    Complex(double r, double i) : real(r), imag(i) {}

    Complex operator+(const Complex& other) const {
        Complex result;
        result.real = this->real + other.real;
        result.imag = this->imag + other.imag;
        return result;
    }

    Complex operator+(const int& num) const {
        Complex result;
        result.real = this->real + num;
        result.imag = this->imag;
        return result;
    }

    friend Complex operator+(const int& num, const Complex& comp);

    void display() const {
        cout << real << " + " << imag << "i" << endl;
    }
};

Complex operator+(const int& num, const Complex& comp) {
    Complex result;
    result.real = num + comp.real;
    result.imag = comp.imag;
    return result;
}

int main() {
    Complex c1(2.5, 3.7);
    Complex c2(1.2, -2.8);

    Complex result1 = c1 + c2;
    Complex result2 = 5 + c1;
    Complex result3 = c2 + 10;

    cout << "c1 + c2 = ";
    result1.display();

    cout << "5 + c1 = ";
    result2.display();

    cout << "c2 + 10 = ";
    result3.display();

    return 0;
}

3、给出下面的抽象基类container:

class container{
protected:
    double radius;
public:
    container(double radius);
    virtual double suface_area()=0;
    virtual double volume()=0;
};

要求建立3个继承container的派生类cube、sphere与cylinder,让每一派生类都包含虚函数surface_area()和volume(),分别用来计算正方体、球体和圆柱体的表面积及体积。要求写出主程序,应用C++的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积和体积。

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

class container {
protected:
    double radius;

public:
    container(double radius) {
        this->radius = radius;
    }

    virtual double surface_area() = 0;
    virtual double volume() = 0;
};

class cube : public container {
private:
    double side;

public:
    cube(double side) : container(side) {
        this->side = side;
    }

    double surface_area() {
        return 6 * side * side;
    }

    double volume() {
        return side * side * side;
    }
};

class sphere : public container {
public:
    sphere(double radius) : container(radius) {}

    double surface_area() {
        return 4 * M_PI * radius * radius;
    }

    double volume() {
        return (4.0 / 3.0) * M_PI * radius * radius * radius;
    }
};

class cylinder : public container {
private:
    double height;

public:
    cylinder(double radius, double height) : container(radius) {
        this->height = height;
    }

    double surface_area() {
        return 2 * M_PI * radius * (radius + height);
    }

    double volume() {
        return M_PI * radius * radius * height;
    }
};

int main() {
    cube cube_obj(6.0);
    sphere sphere_obj(5.0);
    cylinder cylinder_obj(5.0, 6.0);

    cout << "正方体的表面积:" << cube_obj.surface_area() << endl;
    cout << "正方体的体积:" << cube_obj.volume() << endl;

    cout << "球体的表面积:" << sphere_obj.surface_area() << endl;
    cout << "球体的体积:" << sphere_obj.volume() << endl;

    cout << "圆柱体的表面积:" << cylinder_obj.surface_area() << endl;
    cout << "圆柱体的体积:" << cylinder_obj.volume() << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值