第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<iostream>
using namespace std;
class Pet
{
public:
virtual void speak();
};
void Pet::speak()
{
cout << "How does a pet speak?" << endl;
}
/********************************************/
class Cat :public Pet
{
public:
virtual void speak()
{
cout << "miao!miao!" << endl;
}
};
/************************************************/
class Dog :public Pet
{
public:
virtual void speak()
{
cout << "wang!wang!" << endl;
}
};
/**********************************************/
int main()
{
Pet a;
Cat b;
Dog c;
Pet* p;
p = &a;
p->speak();
p = &b;
p->speak();
p = &c;
p->speak();
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!
//抽象宠物类的实现
#include<iostream>
using namespace std;
#pragma warning(disable:4996)//防止安全报错
#include<cstring>
class Pet
{
private:
char name[20];
int age;
char color[20];
public:
virtual void speak() = 0;//纯虚函数
virtual void getinfo() = 0;//纯虚函数
Pet(char* p, int a, char* q);
char* getname();
int getage();
char* getcolor();
};
Pet::Pet(char* p, int a, char* q)
{
strcpy(name, p);
age = a;
strcpy(color, q);
}
char* Pet::getname()
{
return name;
}
char* Pet::getcolor()
{
return color;
}
int Pet::getage()
{
return age;
}
/***********************************/
class Cat :public Pet
{
public:
virtual void speak();
virtual void getinfo();
Cat(char* p, int a, char* q);
};
Cat::Cat(char* p, int a, char* q) :Pet(p, a, q)
{}
void Cat::speak()
{
cout << "猫的叫声:miao!miao!" << endl;
}
void Cat::getinfo()
{
cout << "猫的名字:" << getname() << endl;
cout << "猫的年龄:" << getage() << endl;
cout << "猫的颜色:" << getcolor() << endl;
}
/*******************************************/
class Dog :public Pet
{
public:
virtual void speak();
virtual void getinfo();
Dog(char* p, int a, char* q);
};
Dog::Dog(char* p, int a, char* q) :Pet(p, a, q)
{}
void Dog::speak()
{
cout << "狗的叫声:wang!wang!" << endl;
}
void Dog::getinfo()
{
cout << "狗的名字:" << getname() << endl;
cout << "狗的年龄:" << getage() << endl;
cout << "狗的颜色:" << getcolor() << endl;
}
/********************************************/
int main()
{
char p1[20], q1[20], p2[20], q2[20];
int a1, a2;
cin >> p1 >> a1 >> q1 >> p2 >> a2 >> q2;
Cat cat(p1, a1, q1);
Dog dog(p2, a2, q2);
Pet* p;
p = &cat;
p->getinfo();
p->speak();
p = &dog;
p->getinfo();
p->speak();
return 0;
}
3.重载加法运算符的复数运算(20分)
题目内容:
定义一个复数类,并重载加法运算符(+)和赋值运算符(=)以适用对复数运算的要求。
输入格式:
依次输入两个复数的实部和虚部
输出格式:
先按照要求格式输出两个复数,然后输出这两个复数的和
输入样例:
1 2
3 4
输出样例:
1+j2
3+j4
4+j6
//重载加法运算符的复数运算
#include<iostream>
using namespace std;
class Complex
{
private:
double x, y;
public:
Complex(double, double);
Complex operator+(const Complex&);
Complex operator=(const Complex&);
void show();
};
Complex::Complex(double x, double y)
{
this->x = x;
this->y = y;
}
Complex Complex::operator+(const Complex& a)
{
Complex temp(0,0);
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}
Complex Complex::operator=(const Complex& a)
{
this->x = a.x;
this->y = a.y;
return *this;
}
void Complex::show()
{
if (y >= 0)
cout << x << "+" << "j" << y << endl;
if (y < 0)
cout << x << "-" << 'j' << -y << endl;
}
/****************************************************/
int main()
{
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Complex a(x1, y1), b(x2, y2), c(0, 0);
c = (a + b);
a.show();
b.show();
c.show();
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
提示:由于涉及深浅拷贝的问题,不建议使用动态数组。
//重载矩阵加法运算
#include<iostream>
using namespace std;
class Matrix
{
private:
int p[10][10], m, n;
public:
Matrix(int,int);//默认构造函数
~Matrix();//默认析构函数
void input();
void output();
Matrix operator+(const Matrix& a);
};
Matrix::Matrix(int m,int n)
{
this->m = m;
this->n = n;
}
Matrix::~Matrix()
{
}
void Matrix::input()
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> p[i][j];
}
void Matrix::output()
{
for (int i = 0; i < m; i++)
{
cout << p[i][0];
for (int j = 1; j < n; j++)
{
cout << " " << p[i][j];
}
cout << endl;
}
}
Matrix Matrix::operator+(const Matrix& a)
{
Matrix temp(m,n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
temp.p[i][j] = this->p[i][j] + a.p[i][j];
return temp;
}
/************************************************/
int main()
{
int m, n;
cin >> m >> n;
Matrix A(m, n), B(m, n), C(m, n);
A.input();
B.input();
C = A + B;
C.output();
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
//纯虚函数与基类指针数组的应用
#include<iostream>
using namespace std;
#define PI 3.14159
class Shape
{
public:
virtual void print_name() = 0;
virtual void print_area() = 0;
virtual double area() = 0;
};
/*******************************************/
class Circle :public Shape
{
private:
double radius;
public:
Circle(double r)
{
radius = r;
}
virtual void print_name();
virtual void print_area();
virtual double area()
{
return PI * radius * radius;
}
};
void Circle::print_area()
{
cout << ",面积:" << (PI * radius * radius) << endl;
}
void Circle::print_name()
{
cout << "圆:半径=" << radius;
}
/*****************************************/
class Square :public Shape
{
double a;
public:
Square(double a)
{
this->a = a;
}
virtual void print_name();
virtual void print_area();
virtual double area()
{
return a * a;
}
};
void Square::print_area()
{
cout << ",面积:" << (a * a) << endl;
}
void Square::print_name()
{
cout << "正方形:边长=" << a;
}
/********************************************/
class Rectangle :public Shape
{
double a, b;
public:
Rectangle(double a, double b)
{
this->a = a;
this->b = b;
}
virtual void print_name();
virtual void print_area();
virtual double area()
{
return a * b;
}
};
void Rectangle::print_area()
{
cout << ",面积:" << (a * b) << endl;
}
void Rectangle::print_name()
{
cout << "长方形:长=" << a;
cout << ",宽=" << b;
}
/***************************************/
class Trapezoid :public Shape
{
double a, b, h;
public:
Trapezoid(double a, double b, double h)
{
this->a = a;
this->b = b;
this->h = h;
}
virtual void print_name();
virtual void print_area();
virtual double area()
{
return (a + b) * h / 2.0;
}
};
void Trapezoid::print_area()
{
cout << ",面积:" << ((a+b)*h/2.0) << endl;
}
void Trapezoid::print_name()
{
cout << "梯形:上底=" << a;
cout << ",下底=" << b;
cout << ",高=" << h;
}
/*********************************************/
class Triangle :public Shape
{
double a, h;
public:
Triangle(double a, double h)
{
this->a = a;
this->h = h;
}
virtual void print_name();
virtual void print_area();
virtual double area()
{
return 0.5 * a * h;
}
};
void Triangle::print_area()
{
cout << ",面积:" << (a*h*0.5) << endl;
}
void Triangle::print_name()
{
cout << "三角形:底边=" << a;
cout << ",高=" << h;
}
/*******************************/
int main()
{
double S=0;
double r1, a2, a3, b3, a4, b4, h4, a5, h5;
cin >> r1 >> a2 >> a3 >> b3 >> a4 >> b4 >> h4 >> a5 >> h5;
Shape* p;
Circle s1(r1);
Square s2(a2);
Rectangle s3(a3, b3);
Trapezoid s4(a4, b4, h4);
Triangle s5(a5, h5);
p = &s1;
p->print_name();
p->print_area();
S += p->area();
/**************/
p = &s2;
p->print_name();
p->print_area();
S += p->area();
/**************/
p = &s3;
p->print_name();
p->print_area();
S += p->area();
/**************/
p = &s4;
p->print_name();
p->print_area();
S += p->area();
/**************/
p = &s5;
p->print_name();
p->print_area();
S += p->area();
/**************/
cout << "总面积:" << S << endl;
return 0;
}