计算机程序设计C++ MOOC(第12周编程作业)

时间:2024-03-16 09:10:29

本博客内容为中国大学生MOOC国家精品课程《计算机程序设计C++》作业记录,仅供参考,观者忌照搬照抄,欢迎交流批评指正!

##第十二周编程作业

本周作业内容C++中的多态性

  1. 虚函数实现多态性
    计算机程序设计C++ MOOC(第12周编程作业)
#include <iostream>

using namespace std;

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

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

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

int main()
{
	Pet p, *pet;
	Cat c;
	Dog d;
	pet = &p;
	pet->Speak(); cout << endl;
	pet = &c;
	pet->Speak(); cout << endl;
	pet = &d;
	pet->Speak(); cout << endl;
	return 0;
}
  1. 抽象宠物类的实现
    计算机程序设计C++ MOOC(第12周编程作业)
#include <iostream>
#include <string.h>

using namespace std;

class Pet
{
private:
	char name[20];
	int age;
	char color[20];
public:
	Pet(char *n="xxx", int age=0, char *c="xxx"){ strcpy(name, n); this->age = age;; strcpy(color, c); }
	char *getname(){ return name; }
	int getage(){ return age; }
	char *getcolor(){ return color; }
	virtual void Speak() = 0;
	virtual void GetInfo() = 0;
};

class Cat :public Pet
{
public:
	Cat(char *n = "xxx", int age = 0, char *c = "xxx") :Pet(n, age, c){};
	void GetInfo()
	{
		cout << "猫的名字:" << getname() << endl;
		cout << "猫的年龄:" << getage() << endl;
		cout << "猫的颜色:" << getcolor() << endl;
	}
	void Speak()
	{
		cout << "猫的叫声:miao!miao!" << endl;
	}
};

class Dog :public Pet
{
public:
	Dog(char *n = "xxx", int age = 0, char *c = "xxx") : Pet(n, age, c){};
	void GetInfo()
	{
		cout << "狗的名字:" << getname() << endl;
		cout << "狗的年龄:" << getage() << endl;
		cout << "狗的颜色:" << getcolor() << endl;
	}
	void Speak()
	{
		cout << "狗的叫声:wang!wang!" << endl;
	}
};

int main()
{
    Pet *pet;
	char name[20], color[20];
	int age;
	cin >> name >> age >> color;
	Cat c(name,age,color);
	cin >> name >> age >> color;
	Dog d(name,age,color);
	pet = &c;
	pet->GetInfo();
	pet->Speak();
	pet = &d;
	pet->GetInfo();
	pet->Speak();
	return 0;
}
  1. 重载加法运算符的复数运算
    计算机程序设计C++ MOOC(第12周编程作业)
#include <iostream>

using namespace std;

class Complex
{
private:
	double real, imag;
public:
	Complex(double a = 0, double b = 0) :real(a), imag(b){};
	double Real(){ return real; }
	double Imag(){ return imag; }
	void set(double a, double b){ real = a; imag = b; }
	Complex operator +(Complex);
	friend ostream &operator <<(ostream &out,const Complex &a);//友元函数
}; 

Complex Complex::operator +(Complex a)
{
	Complex temp;
	temp.real = real + a.real;
	temp.imag = imag + a.imag;
	return temp;
}

ostream & operator <<(ostream &out,const Complex &a)
{
	out << a.real << "+j" << a.imag;
	return out;
}

int main()
{
	double real1,imag1,real2,imag2;
	cin >> real1 >> imag1;
	cin >> real2 >> imag2;
	Complex a(real1, imag1), b(real2, imag2);
	cout << a << endl;
	cout << b << endl;
	cout << a + b << endl;
	return 0;
}

此处要对<<运算符进行重载,需要使用友元函数,代码格式如下

//类内需要关键字 friend
friend ostream &operator <<(ostream &out,const Classname &a);
//类外
ostream & operator <<(ostream &out,const Classname &a)
{...}
  1. 重载矩阵加法运算
    计算机程序设计C++ MOOC(第12周编程作业)
#include <iostream>

using namespace std;

class Matrix
{
private:
	int row, column;
	int **M;
public:
	Matrix(int r = 0, int c = 0, int **m = NULL)
	{
		row = r;
		column = c;
		M = m;
	}
	Matrix operator +(Matrix);
	int get(int i, int j){ return M[i][j]; }
	bool ifnull()//判断矩阵是否为空
	{
		if (M != NULL) return true;
		else return false;
	}
	friend ostream &operator <<(ostream &out, const Matrix &a);
};

Matrix Matrix::operator+(Matrix a)
{
	Matrix temp;
	temp.row = a.row;
	temp.column = a.column;
	temp.M = new int*[row];
	for (int i = 0; i < row; i++)
	{
		temp.M[i] = new int[column];
		for (int j = 0; j < column; j++)
		{
			temp.M[i][j] = M[i][j] + a.M[i][j];
		}
	}
	return temp;
}

ostream &operator<<(ostream &out, const Matrix &a)
{
	for (int i = 0; i < a.row; i++)
	{
		int j = 0;
		for (j = 0; j < a.column - 1; j++)
		{
			cout << a.M[i][j] << ' ';
		}
		cout << a.M[i][j] << endl;
	}
	return out;
}

int main()
{
	int row, column;
	int **m1,**m2;
	cin >> row >> column;
	m1 = new int*[row];
	for (int i = 0; i < row; i++)
	{
		m1[i] = new int[column];
		for (int j = 0; j < column; j++)
		{
			cin >> m1[i][j];
		}
	}
	m2 = new int*[row];
	for (int i = 0; i < row; i++)
	{
		m2[i] = new int[column];
		for (int j = 0; j < column; j++)
		{
			cin >> m2[i][j];
		}
	}
	Matrix matrix1(row, column, m1), matrix2(row, column, m2), matrix3;
	matrix3 = matrix1 + matrix2;
	cout << matrix3;
	return 0;
}

此程序使用了动态二维数组

  1. 纯虚函数与基类指针数组的应用
    计算机程序设计C++ MOOC(第12周编程作业)
#include <iostream>

using namespace std;

const double PI = 3.14159;

class Shape
{
public:
	virtual void printName() = 0;
	virtual double printArea() = 0;
};

class Circle :public Shape
{
private:
	double r;
public:
	Circle(double r = 0){ this->r = r; }
	void printName()
	{
		cout << "圆:半径=" << r;
	}
	double printArea()
	{
		cout << "面积:" <<  PI * r * r;
		return PI * r * r;
	}
};

class Square :public Shape
{
public:
	double l;
public:
	Square(double l = 0){ this->l = l; }
	void printName()
	{
		cout << "正方形:边长=" << l;
	}
	double printArea()
	{
		cout << "面积:" << l*l;
		return l*l;
	}
};

class Rectangle :public Shape
{
public:
	double width,lengh;
public:
	Rectangle(double l = 0, double w = 0){ lengh = l; width = w; }
	void printName()
	{
		cout << "长方形:长="<< lengh<<",宽="<< width;
	}
	double printArea()
	{
		cout << "面积:" << width*lengh;
		return width*lengh;
	}
};

class Trapezoid :public Shape
{
public:
	double up, down, height;
public:
	Trapezoid(double u = 0, double d = 0, double h = 0){ up = u; down = d; height = h; }
	void printName()
	{
		cout << "梯形:上底=" << up << ",下底=" << down << ",高=" << height;
	}
	double printArea()
	{
		cout << "面积:" << (up+down)*height/2;
		return (up + down)*height / 2;
	}
};

class Triangle :public Shape
{
public:
	double h, l;
public:
	Triangle(double l = 0, double h = 0){ this->l = l; this->h = h; }
	void printName()
	{
		cout << "三角形:底边=" << l << ",高=" << h;
	}
	double printArea()
	{
		cout << "面积:" << h*l/2;
		return  h*l / 2;
	}
};

int main()
{
	double r, L, l, w, u, p, h, down, height, area=0;
	cin >> r >> L >> l >> w >> u >> p >> h >> down >> height;
	Circle a(r); Square b(L); Rectangle d(l, w); Trapezoid e(u, p, h); Triangle f(down, height);
	Shape *s;
	s = &a; s->printName(); cout << ','; area += s->printArea(); cout << endl;
	s = &b; s->printName(); cout << ','; area += s->printArea(); cout << endl;
	s = &d; s->printName(); cout << ','; area += s->printArea(); cout << endl;
	s = &e; s->printName(); cout << ','; area += s->printArea(); cout << endl;
	s = &f; s->printName(); cout << ','; area += s->printArea(); cout << endl;
	cout << "总面积:" << area << endl;
	return 0;
}

以上为第十二周的编程作业。