C++第12周mooc在线测评—第12周 统一接口不同实现-多态性(虚函数与符号重载)

//1虚函数实现多态性(20分)
//题目内容:
//定义宠物类Pet,包含虚函数Speak,显示如下信息“How does a pet speak ? ”; 定义公有派生类Cat和Dog,其Speak成员函数分别显示:“miao!miao!”和“wang!wang!”。主函数中定义Pet,Cat和Dog对象,再定义Pet指针变量,分别指向Pet,Cat和Dog对象,并通过指针调用Speak函数,观察并分析输出结果。
//
//输入格式 :
//不需要输入
//
//输出格式:
//各类调用Speak函数输出的结果
//
//输入样例:
//
//
//输出样例:
//How does a pet speak ?
//miao!miao!
//wang!wang!
#include

using namespace std;

class Pet
{
public:
	Pet()
	{}
	~Pet()
	{}
	virtual void Speak()
	{
		cout << "How does a pet speak?" << endl;
	}
private:
};

class Cat:public Pet
{
public:
	Cat()
	{}
	~Cat()
	{}
	virtual void Speak()
	{
		cout << "miao!miao!" << endl;
	}
private:

};

class Dog:public Pet
{
public:
	Dog()
	{}
	~Dog()
	{}
	virtual void Speak()
	{
		cout << "wang!wang!" << endl;
	}

private:

};

int main()
{
	Pet pet1;
	Dog dog1;
	Cat cat1;
	Pet *point;
	point = &pet1;
	point->Speak();
	point = &cat1;
	point->Speak();
	point = &dog1;
	point->Speak();
	system("pause");
	return 0;
}

//2抽象宠物类的实现(20分)
//题目内容:
//定义抽象宠物类Pet,其中数据成员包括:名字,年龄和颜色;成员函数包括:构造函数;获取成员数据值的函数;纯虚函数Speak和纯虚函数GetInfo;
//定义Pet的派生类Cat和Dog,其中Speak函数分别显示猫和狗的叫声, 而GetInfo函数分别输出Cat和Dog的属性。主函数中定义Pet指针变量,分别指向动态生成的Cat和Dog对象,并通过指针分别调用GetInfo函数和Speak函数,观察并分析输出结果。
//
//输入格式 :
//猫和狗的信息
//
//输出格式:
//请参考输出样例,严格遵照格式要求输出,建议直接复制样例中的部分文字粘贴进自己的代码中.(冒号是英文冒号,冒号后无空格)
//
//输入样例:
//mikey 2 blue
//benben 1 black
//
//输出样例:
//猫的名字 : mikey
//   猫的年龄 : 2
//	  猫的颜色 : blue
//		 猫的叫声 : miao!miao!
//			狗的名字 : benben
//			   狗的年龄 : 1
//				  狗的颜色 : black
//					 狗的叫声 : wang!wang!
//
//							时间限制:500ms内存限制:32000kb
#include 
#include 
using namespace std;
class Pet
{
protected:
	char Name[10];
	int Age;
	char Color[10];
public:
	Pet(char name[],int age,char color[])
	{
		this->Age = age;
		strcpy(this->Color , color);
		strcpy(this->Name, name);
	}
	~Pet()
	{}
	virtual void Speak() = 0;
	virtual void GetInfo() = 0;
};

class Cat:public Pet
{
public:
	Cat(char name[], int age, char color[]):
		Pet(name,age,color)
	{}
	~Cat()
	{}
	virtual void Speak()
	{
		cout << "猫的叫声:miao!miao!"<> Name >> Age >> Color;
	Cat cat1(Name, Age, Color);
	cin >> Name >> Age >> Color;
	Dog dog1(Name, Age, Color);

	cat1.GetInfo();
	cat1.Speak();
	dog1.GetInfo();
	dog1.Speak();
	system("pause");
	return 0;
}

//3重载加法运算符的复数运算(20分)
//题目内容:
//定义一个复数类,并重载加法运算符( + )和赋值运算符( = )以适用对复数运算的要求。
//
//输入格式 :
//依次输入两个复数的实部和虚部
//
//输出格式:
//先按照要求格式输出两个复数,然后输出这两个复数的和
//
//输入样例:
//1 2
//3 4
//
//输出样例:
//1 + j2
//3 + j4
//4 + j6
//时间限制:500ms内存限制:32000kb

#include 

using namespace std;

class Complex
{
private:
	double real;
	double img;
public:
	Complex(double real = 0,double img = 0)
	{
		this->real = real;
		this->img = img;
	}

	Complex operator +(Complex &c)
	{
		Complex temp;
		temp.real = real + c.real;
		temp.img = img + c.img;
		return temp;
	}

	//Complex operator =(Complex &d)
	//{
	//	this->real =  d.real;
	//	this->img =  d.img;
	//	return *this;
	//}
	~Complex()
	{
	}

	void Print()
	{
		cout << real;
		if (img>=0)
		{
			cout << "+";
			cout << "j" << img << endl;
		}
		else
		{
			cout << "-";
			cout << "j" << -img << endl;
		}	
	}
};


int main()
{
	double real;
	double img;
	cin >> real >> img;
	Complex a1(real, img);
	cin >> real >> img;
	Complex b1(real, img);
	a1.Print();
	b1.Print();
	Complex c1;
	c1 = (a1 + b1);
	c1.Print();


	system("pause");
	return 0;
}

//4重载矩阵加法运算(20分)
//题目内容:
//编写一个矩阵类,重载矩阵加法运算。设A,B,C均为m行,n列的矩阵,要求程序能实现C = A + B的操作。
//
//输入格式 :
//第一行为矩阵的行数和列数,下面为两个矩阵的元素值
//
//输出格式:
//两个矩阵的和。注意,输出的每行元素,行末没有空格。
//
//输入样例:
//2 3
//1 3 2
//4 2 5
//2 3 4
//3 2 6
//
//输出样例:
//3 6 6
//7 4 11
//
//提示:由于涉及深浅拷贝的问题,不建议使用动态数组。
//时间限制:500ms内存限制:32000kb

#include
using namespace std;
class Mat
{
private:
	int row, col;
	double *data;

public:
	Mat(int row, int col)
	{
		this->row = row;
		this->col = col;
		this->data = new double[row*col];
	}
	void dataLoad()
	{
		for (int i = 0; i < row*col; i++)
		{
			cin >> data[i];	
		}
	}

	Mat operator+(Mat &mat)
	{
		static Mat temp(row,col);
		for (int i = 0; i < row*col; i++)
		{
			temp.data[i] = this->data[i] + mat.data[i];
		}
		return temp;
	}
	void dataPrint()
	{
		int index = 0;
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				cout << this->data[index];
				if ( j != col-1)
				{
					cout << " ";
				}
				else
				{
					cout << endl;
				}
				index++;
			}
			
		}

	}
	~Mat()
	{
	}


};


int main()
{
	int row, col;
	cin >> row>>col;
	Mat mat1(row, col);
	mat1.dataLoad();
	Mat mat2(row, col);
	mat2.dataLoad();
	
	Mat mat3(row,col);
	mat3 = (mat1 + mat2);
	mat3.dataPrint();
	system("pause");
	return 0;
}

//5纯虚函数与基类指针数组的应用(20分)
//题目内容:
//定义抽象基类Shape,
//其中纯虚函数printName()输出几何图形的名称和相应的成员数据、纯虚函数printArea()计算几何图形的面积。并由Shape类派生出5个派生类:Circle(圆形),数据成员为半径、Square(正方形)
//,数据成员为边长、Rectangle(长方形) ,数据成员为长和宽、Trapezoid(梯形) ,数据成员为上底、下底和高、Triangle(三角形)
//,数据成员为底和高。测试过程,定义一个指向基类的指针数组,使其每个元素指向一个动态产生的派生类对象,分别调用相应的成员函数显示各个几何图形的属性及面积,最终输出总面积值。
//
//输入格式 :
//依次输入圆半径、正方形边长、长方形长宽、梯形上底下底和高、三角形底边和高
//
//输出格式:
//请参考输出样例,建议直接复制样例中的部分文字粘贴进自己的代码。圆周率取 3.14159
//注意输出中的标点符号、空格。
//输入样例:
//10
//5
//2 4
//1 2 3
//4 3
//
//输出样例:
//圆 : 半径 = 10, 面积 : 314.159
//正方形 : 边长 = 5, 面积 : 25
//  长方形 : 长 = 2, 宽 = 4, 面积 : 8
//	梯形 : 上底 = 1, 下底 = 2, 高 = 3, 面积 : 4.5
//	 三角形 : 底边 = 4, 高 = 3, 面积 : 6
//	   总面积 : 357.659
//
//			 时间限制:500ms内存限制:32000kb

#include
# define PI 3.14159
using namespace std;

class Shape
{
private:
public:
	virtual void printName() = 0;
	virtual double printArea() = 0;
	Shape()
	{}
	~Shape()
	{}
};
class Circle:public Shape
{
private:
	double radius;
public:
	Circle(double radius)
	{
		this->radius = radius;
	}
	virtual void printName()
	{
		cout << "圆:半径=" << radius << ",";
	}
	virtual double printArea()
	{
		double area;
		area = radius*radius*PI;
		cout << "面积:" << area<width = width;
		this->height = height;
	}
	virtual void printName()
	{
		cout << "长方形:长=" << height << ",宽="<side = side;
	}
	virtual void printName()
	{
		cout << "正方形:边长=" << side << ",";
	}
	virtual double printArea()
	{
		double area;
		area = side*side;
		cout << "面积:" << area << endl;
		return area;
	}
	~Square()
	{
	}
};
class Trapezoid :public Shape
{
private:
	double upside;
	double downside;
	double height;
public:
	Trapezoid(double upside,double downside,double height)
	{
		this->upside = upside;
		this->downside = downside;
		this->height = height;
	}
	virtual void printName()
	{
		cout << "梯形:上底=" << upside << ",下底="<downside = downside;
		this->height = height;
	}
	virtual void printName()
	{
		cout << "三角形:底边=" << downside << ",高=" << height << ",";
	}
	virtual double printArea()
	{
		double area;
		area = downside / 2 * height;
		cout << "面积:" << area << endl;
		return area;
	}
	~Triangle()
	{
	}
};


int main()
{
	double sumArea = 0;
	double radius, side, heightRect, widthRect, upsideTra, downsideTra, heightTra, heightTri, downsideTri;
	cin >> radius >> side >> heightRect >> widthRect >> upsideTra >> downsideTra >> heightTra >> downsideTri >> heightTri;
	Shape *all[5];
	all[0] = new Circle(radius);
	all[1] = new Square(side);
	all[2] = new Rectangle(widthRect, heightRect);
	all[3] = new Trapezoid(upsideTra, downsideTra, heightTra);
	all[4] = new Triangle(downsideTri, heightTri);
	for (int i = 0; i < 5; i++)
	{
		all[i]->printName();
		sumArea = sumArea + all[i]->printArea();
	}
	cout << "总面积:" << sumArea << endl;
	system("pause");
	return 0;
}

你可能感兴趣的:(C++)