C++习题05_多态性
习题05(01)双目运算符重载
题目描述
编写程序,按要求重载“+”、“-”运算符,实现两个二维数组的相加和相减运算,要求第一个数组的值由构造函数设置(元素值依次为:11 22 33 44 55 66),第二个数组的值由键盘输入。
二组数组的行和列下标定义为const常量,为2行3列。参考类结构如下:
class MyArray
{
public:
MyArray();
MyArray(int a,int b,int c,int d,int e,int f);
void inputArray(); //键盘输入数组的值
void display(); //显示数组的值
MyArray operator+(MyArray &a); //加法运算符重载为成员函数
friend MyArray operator-(MyArray &a, MyArray &b); // 减法运算符重载为友元函数
private:
int var[Row][Col]; //Row、Col为const常量
};
输入描述
第二个数组各元素的值
输出描述
输出第一个数组的值
输出第二个数组的值
两个数组相加后的结果
两个数组相减后的结果
输入样例
1 2 3
4 5 6
输出样例
Display object x
11 22 33
44 55 66
Display object y
1 2 3
4 5 6
Display object z=x+y
12 24 36
48 60 72
Display object z=x-y
10 20 30
40 50 60
#include <iostream>
using namespace std;
const int Row = 3; //弄反了
const int Col = 2;
class MyArray
{
public:
MyArray()
{
}
MyArray(int a, int b, int c, int d, int e, int f) //2行3列
{
var[0][0] = a;
var[0][1] = b;
var[0][2] = c;
var[1][0] = d;
var[1][1] = e;
var[1][2] = f;
}
void inputArray()
{
for (int i = 0; i < Col; i++)
{
for (int j = 0; j < Row; j++)
cin >> var[i][j];
}
}
void display()
{
for (int i = 0; i < Col; i++)
{
for (int j = 0; j < Row; j++)
cout << '\t' << var[i][j];
cout << endl;
}
}
MyArray operator+(MyArray &a) //每个元素相加
{
MyArray temp;
for (int i = 0; i < Col; i++)
{
for (int j = 0; j < Row; j++)
temp.var[i][j] = var[i][j] + a.var[i][j];
}
return temp;
}
friend MyArray operator-(MyArray &a, MyArray &b)
{
MyArray temp;
for (int i = 0; i < Col; i++)
{
for (int j = 0; j < Row; j++)
temp.var[i][j] = a.var[i][j] - b.var[i][j];
}
return temp;
}
private:
int var[Col][Row];
};
int main()
{
MyArray x(11, 22, 33, 44, 55, 66), y, z;
y.inputArray();
cout << "Display object x" << endl;
x.display();
cout << "Display object y" << endl;
y.display();
cout << "Display object z=x+y" << endl;
z = x + y;
z.display();
cout << "Display object z=x-y" << endl;
z = x - y;
z.display();
return 0;
}
习题05(02)Complex单目运算符重载
题目描述
编程实现Complex类前置++和后置—运算符的重载,Complex类的私有数据成员real和imag为double型,要求:(1)前置自增++重载为成员函数;(2)后置自减--重载为友元函数;(3)定义print(),实现对象值的输出,格式为:(real,imag)
主函数中定义三个对象:c1,c2,c,对象c1通过构造函数直接指定复数的实部和虚部,分别为2.5及3.7;对象c2的实部和虚部由键盘输入。对象c的实部和虚部使用默认值,默认为0。
执行按顺序以下语句,然后输出c和c1的值。
c = ++c1;
执行按顺序以下语句,然后输出c和c1的值。
c = c2--;
输入描述
对象c2的初始值
输出描述
执行c = ++c1; 后c与c1的值
执行c = c2--; 后c与c2的值
输入样例
4.2 6.5
输出样例
(3.5,4.7)
(3.5,4.7)
(4.2,6.5)
(3.2,5.5)
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(double r = 0, double i = 0)
{
real = r;
imag = i;
}
Complex operator++()
{
++real;
++imag;
return *this;
}
friend Complex operator--(Complex &op, int)
{
return Complex(op.real--, op.imag--);
}
void print() { cout << "(" << real << "," << imag << ")" << endl; }
private:
double real, imag;
};
int main()
{
double r, i;
cin >> r >> i;
Complex c1(2.5, 3.7), c2(r, i), c;
c = ++c1;
c.print();
c1.print();
c = c2--;
c.print();
c2.print();
return 0;
}
单目运算符重载与双目运算符重载的区别
友元函数
双目运算符有2个参数,单目运算符有1个参数
成员函数
双目运算符有1个参数,单目运算符没有参数,就自己++或者–就行
单目运算符的重载分前置和后置
后置在后面就加一个假参数int即可
friend Complex operator--(Complex &op,int)
{
return Complex(op.real--,op.imag--);
}
成员函数的返回值
要返回this指针
Complex operator++(){
++real;++imag;
return * this;
习题05(03)抽象类与虚函数
题目描述
编写程序,定义抽象类Container,有double型保护数据成员radius,由它公有派生出3个类:Cube(立方体)、Sphere(球体)、Cylinder(圆柱体),派生类中增加的数据成员也为double型。
抽象类Container的结构如下:
class Container //声明抽象类
{
public:
Container(double radius); //抽象类的构造函数
virtual double calSurfaceArea()=0; //纯虚函数
virtual double calVolume()=0; //纯虚函数
protected:
double radius;
};
主函数中定义抽象类的指针,再定义Cube(立方体)、Sphere(球体)、Cylinder(圆柱体)的对象各一个,各对象的初始值均由键盘输入。应用C++的多态性,将各派生类对象的地址赋给基类指针,通过指针计算各对象的表面积和体积并输出。
π的值:3.1415926
输入描述
立方体的边长
球体的半径
圆柱体的半径和高
输出描述
立方体的表面积和体积
球体的表面积和体积
圆柱体的表面积和体积
输入样例
5
5
5 6
输出样例
立方体的表面积:150,体积:125
球体的表面积:314.159,体积:523.599
圆柱体的表面积:345.575,体积:471.239
#include <iostream>
using namespace std;
const double PI = 3.1415926;
class Container //声明抽象类
{
public:
Container(double radius)
{
this->radius = radius;
} //抽象类的构造函数
virtual double calSurfaceArea() = 0; //纯虚函数
virtual double calVolume() = 0; //纯虚函数
protected:
double radius;
};
class Cube : public Container
{
public:
Cube(double radius) : Container(radius)
{
}
double calSurfaceArea()
{
return radius * radius * 6;
}
double calVolume()
{
return radius * radius * radius;
}
};
class Sphere : public Container
{
public:
Sphere(double radius) : Container(radius)
{
}
double calSurfaceArea()
{
return radius * 4 * PI * radius;
}
double calVolume()
{
return radius * radius * radius * PI * 4 / 3;
}
};
class Cylinder : public Container
{
public:
Cylinder(double radius, double height) : Container(radius)
{
this->height = height;
}
double calSurfaceArea()
{
return PI * 2 * radius * radius + PI * 2 * radius * height;
}
double calVolume()
{
return PI * radius * radius * height;
}
private:
double height;
};
int main()
{
Container *p;
double a, b, c1, c2;
cin >> a >> b >> c1 >> c2;
Cube A(a);
Sphere B(b);
Cylinder C(c1, c2);
p = &A;
cout << "立方体的表面积:" << p->calSurfaceArea() << ",体积:" << p->calVolume() << endl;
p = &B;
cout << "球体的表面积:" << p->calSurfaceArea() << ",体积:" << p->calVolume() << endl;
p = &C;
cout << "圆柱体的表面积:" << p->calSurfaceArea() << ",体积:" << p->calVolume() << endl;
return 0;
}