实验五 多态性

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

#include<iostream>
using namespace std;
class Complex
{
public:
	Complex(double r=0.0, double i=6.0);
	void show();
	friend Complex operator+(Complex& com1, Complex& com2);
	friend Complex operator+(Complex& com4, double a);
private:
	double real;
	double imag;
};
Complex::Complex(double r, double i)
{
	real = r;
	imag = i;
}
void Complex::show()
{
	cout << real << "+" << imag << "i" << endl;
}
Complex  operator+(Complex& com1, Complex& com2)
{
	Complex temp;
	temp.real = com1.real + com2.real;
	temp.imag= com1.imag + com2.imag;
	return temp;
}
Complex operator+(Complex& com4, double a)
{
	return com4.real + a;
}
int main()
{
	Complex com1(10.2, 1.32);
	Complex com2(22.23, 32.5);
	Complex com3 = com1 + com2;
	com3.show();
	Complex com4 = com1 + 10;
	com4.show();
	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 Array
{
public:
    Array operator+(const Array& B);
    Array operator-(const Array& B);
    Array(int m, int n);
    void input();
    void print();
private:
    int M;
    int N;
    int** a;
};

Array::Array(int m, int n)
{
    M = m;
    N = n;
    a = new int* [M];
    for (int i = 0; i < M; ++i) {
        a[i] = new int[N];
    }
}

Array Array::operator+(const Array& B)
{
    Array result(M, N);
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            result.a[i][j] = a[i][j] + B.a[i][j];
        }
    }
    return result;
}

Array Array::operator-(const Array& B)
{
    Array result(M, N);
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            result.a[i][j] = a[i][j] - B.a[i][j];
        }
    }
    return result;
}

void Array::input() {
    cout << "Enter array elements:" << endl;
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < N; ++j) {
            cin >> a[i][j];
        }
    }
}

void Array::print() {
    cout << "Array:" << endl;
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < N; ++j) {
            cout << a[i][j] << "\t";
        }
        cout << endl;
    }
}

int main() {
    int m, n;
    cin >> m >> n;
    Array A1(m, n);
    A1.input();
    Array A2(m, n);
    A2.input();
    Array A3 = A1 + A2;
    A3.print();
    Array A4 = A1 - A2;
    A4.print();
    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 r)
    {
        radius = r;
    }
    virtual double suface_area() = 0;
    virtual double volume() = 0;
};
class Cube :public container
{
public:
    virtual double suface_area()
    {
        return radius * radius * 6;

    }
    virtual double volume()
    {
        return radius * radius * radius;

    }
    Cube(double r) :container(r)
    {
    }
};
class sphere :public container
{
public:
    virtual double suface_area()
    {
        return 4 * 3.14 * radius * radius;

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

    }
    sphere(double r) :container(r)
    {
    }
};
class cylinder :public container
{
public:
    virtual double suface_area()
    {
        return ((2 * 3.14 * radius * 6.0) + 3.14 * radius * radius * 2);
    }
    virtual double volume()
    {
        return 3.14 * radius * radius * height;

    }
    cylinder(double h, double r) :container(r)
    {
        height = h;
    }
private:
    double height;
};
int main()
{
    container* ptr;
    Cube c(6.0);
    ptr = &c;
    cout << ptr->suface_area() << endl;
    cout << ptr->volume() << endl;
    sphere s(5.0);
    ptr = &s;
    cout << ptr->suface_area() << endl;
    cout << ptr->volume() << endl;
    cylinder cy(5.0, 6.0);
    ptr = &cy;
    cout << ptr->suface_area() << endl;
    cout << ptr->volume() << endl;
    return 0;
}

 

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值