大二(下)C++ 多态性实验

实验题目(1):
定义一个抽象类容器类,其中定义了若干纯虚函数,实现求表面积、体积、输出等功能。由此抽象类派生出正方体、球体和圆柱体等多个派生类,根据需要定义自己的成员变量,在各个派生类中重新定义各纯虚函数,实现各自类中相应功能,各个类成员的初始化均由本类构造函数实现。
① 在主函数中,定义容器类的指针和各个派生类的对象,使指针指向不同对象处调用相同的函数能执行不同的函数代码,从而实现动态多态性。
② 定义一个顶层函数void TopPrint(Container &r);使得主函数中调用该函数时,根据实在参数所有的类自动调用对应类的输出函数。
③ 主函数中定义一个Container类对象,观察编译时的错误信息,从而得出什么结论?

#include<iostream>
using namespace std;
class Container
{
protected:
	double radius;
public:
	Container(double r)
	{
		radius = r;    
	}
	Container()
	{
		radius = 0;
	}
	virtual double area() = 0;   //求表面积
	virtual double volume() = 0; //求体积
	virtual void print() = 0;    //输出相关信息
};

class Cube:public Container  //正方体
{
private:
	double length;
public:
	Cube(double x)
	{
		length = x;
	}
	virtual double area()    //求表面积
	{
		return 6 * length * length;
	}
	virtual double volume()  //求体积
	{
		return length * length * length;
	}
	virtual void print()     //输出相关信息
	{
		cout << "正方体表面积为:" << area() << "  正方体体积为:" << volume() << endl;
	}

};

class Sphere:public Container //球体
{
private:
	int radius;
public:
	Sphere(int r)
	{
		radius = r;
	}
	virtual double area()    //求表面积
	{
		return 4 * 3.141592 * radius * radius;
	}
	virtual double volume()  //求体积
	{
		return  (3.141592 * (4.0 / 3) * radius * radius * radius);
	}
	virtual void print()     //输出相关信息
	{
		cout << "球体表面积为:" << area() << "  球体体积为:" << volume() << endl;
	}
};

class Cylinder :public Container //圆柱体
{
private:
	double height;
	double radius;
public:
	Cylinder(double r, double h)
	{
	radius = r;
	height = h;
    }
	virtual double area()    //求表面积
	{
		return 2 * 3.141592 * radius *( radius+ height);
	}
	virtual double volume()  //求体积
	{
		return  3.141592 * height * radius * radius;
	}
	virtual void print()     //输出相关信息
	{
		cout << "圆柱体表面积为:" << area() << "  圆柱体体积为:"<< volume() << endl;
	}

};


int main()
{
	Container *p;
	Cube cu(1); //正方体
	Sphere sp(1); //球体
	Cylinder cy(2,3); //圆柱体

	p = &cu;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();

	p = &sp;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();

	p = &cy;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();

	getchar();
	return 0;
}

或者

#include<iostream>
using namespace std;
class Container
{
protected:
	double radius;
public:
	Container(double r)
	{
		radius = r;
	}
	Container()
	{
		radius = 0;
	}
	virtual double area() = 0;   //求表面积
	virtual double volume() = 0; //求体积
	virtual void print() = 0;    //输出相关信息
};

class Cube :public Container  //正方体
{
private:
	double length;
public:
	Cube(double x)
	{
		length = x;
	}
	double area()    //求表面积
	{
		return 6 * length * length;
	}
	double volume()  //求体积
	{
		return length * length * length;
	}
	void print()     //输出相关信息
	{
		cout << "正方体表面积为:" << area() << "  正方体体积为:" << volume() << endl;
	}

};

class Sphere :public Container //球体
{
public:
	Sphere(int r) :Container(r)
	{
		;
	}
	double area()    //求表面积
	{
		return 4 * 3.141592 * radius * radius;
	}
	double volume()  //求体积
	{
		return  (3.141592 * (4.0 / 3) * radius * radius * radius);
	}
	void print()     //输出相关信息
	{
		cout << "球体表面积为:" << area() << "  球体体积为:" << volume() << endl;
	}
};

class Cylinder :public Container //圆柱体
{
private:
	double height;
public:
	Cylinder(double r, double h) :Container(r)
	{
		height = h;
	}
	double area()    //求表面积
	{
		return 2 * 3.141592 * radius *(radius + height);
	}
	double volume()  //求体积
	{
		return  3.141592 * height * radius * radius;
	}
	void print()     //输出相关信息
	{
		cout << "圆柱体表面积为:" << area() << "  圆柱体体积为:" << volume() << endl;
	}

};


int main()
{
	Container *p;
	Cube cu(1); //正方体
	Sphere sp(1); //球体
	Cylinder cy(2, 3); //圆柱体

	p = &cu;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();

	p = &sp;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();

	p = &cy;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();

	getchar();
	return 0;
}

实验题目2:
定义复数类Complex,有实部、虚部两个私有成员变量,在该类中定义多个重载的构造函数、定义析构函数和输出函数print,复数的输出形如12-3i,在类中重载+、−、*、/、++(分前++和后++)。在主函数(直接用实验教材P210代码)中定义复数类的对象,实现复数的各种算术运算,通过重载实现静态多态性。

#include<iostream>
using namespace std;

class Complex
{
private:
	float real;
	float imag;
public:
	Complex(float r = 0, float i = 0)
	{
		real = r;
		imag = i;
	}
	void print()
	{
		cout << real;
		if (imag != 0)
		{
			if (imag > 0)
			{
				cout << " + ";
			}
			cout << imag << " i";
		}
		cout << endl;
	}
	friend Complex operator +(const Complex &a, const Complex &b);
	friend Complex operator -(const Complex &a, const Complex &b);
	friend Complex operator *(const Complex &a, const Complex &b);
	friend Complex operator /(const Complex &a, const Complex &b);
	friend Complex operator ++ ( Complex &a);
	Complex operator ++(int);
};

Complex operator +(const Complex &a,const Complex &b)
{
	Complex temp;
	temp.real = a.real + b.real;
	temp.imag = a.imag + b.imag;
	return temp;
}
Complex operator -(const Complex &a, const Complex &b)
{
	Complex temp;
	temp.real = a.real - b.real;
	temp.imag = a.imag - b.imag;
	return temp;
}
Complex operator *(const Complex &a, const Complex &b)
{
	Complex temp;
	temp.real = a.real * b.real;
	temp.imag = a.imag * b.imag;
	return temp;
}
Complex operator /(const Complex &a, const Complex &b)
{
	Complex temp;
	temp.real = a.real / b.real;
	temp.imag = a.imag / b.imag;
	return temp;
}

Complex operator ++ (Complex &a)
{
	++a.real;
	++a.imag;
	return a;
}
Complex Complex::operator ++(int)
{
	real++;
	imag++;
	return *this;
}


int main()
{
	//Complex A1(2.3f, 4.6f), A2(3.6f, 2.8f);
	Complex A1(12, 14), A2(8, 17);
	Complex A3, A4, A5, A6;
	A3 = A1 + A2;
	A4 = A1 - A2;

	A5 = A1 * A2;
	A6 = A1 / A2;
	cout << "A1 = ";
	A1.print();
	cout << endl << "A2 = ";
	A2.print();
	cout << endl << "A3 = A1 + A2 = ";
	A3.print();
	cout << endl << "A4 = A1 - A2 = ";
	A4.print();
	cout << endl << "A5 = A1 * A2 = ";
	A5.print();
	cout << endl << "A6 = A1 / A2 = ";
	A6.print();

	A3 = ++A1;
	cout << endl << "after A3 = ++A1 , ";
	cout << "A1= ";
	A1.print();
	cout << endl << "A3 =";
	A3.print();

	A4 = A2++;
	cout << endl << "after A4 = A2++ , ";
	cout << "A2= ";
	A2.print();
	cout << endl << "A4= ";
	A4.print();

	getchar();
	return 0;
}

用成员函数重载双目运算符*和/:

Complex operator *( Complex a);
Complex operator /( Complex a);
friend Complex operator ++ ( Complex &a);
Complex operator ++(int);
Complex Complex::operator *( Complex a)
{
	real = real * a.real;
	imag = imag * a.imag;
	return *this;
}
Complex Complex::operator /( Complex a)
{
	Complex temp;
	real = real / a.real;
	imag = imag / a.imag;
	return *this;
}

Complex operator ++ (Complex &a)
{
	++a.real;
	++a.imag;
	return a;
}
Complex Complex::operator ++(int)
{
	real++;
	imag++;
	return *this;
}

猜你喜欢

转载自blog.csdn.net/qq_43271844/article/details/90114621