【问题描述】用c++实现求正方形、长方形、圆形的面积和周长,要求使用派生和继承
【编译环境】 ubuntu20.04 g++
【实现思路】使用c++的多态与继承,基类是形状类,重写求周长和面积函数,实现多态。但是要注意到长方形需要两个参数,圆形和正方形一个参数即可。结合虚函数和函数重载实现,代码如下。
#include <iostream>
using namespace std;
#define PI 3.14159
class Shape
{
public:
virtual void area(int w) = 0; //纯虚函数
virtual void grith(int w) = 0; //纯虚函数
virtual void grith(int w, int h) =0; //函数重载
virtual void area(int w, int h) =0; //函数重载
};
class Rectangle : public Shape //长方形
{
public:
void area(int w){};
void grith(int w){};
void grith(int w, int h)
{
cout << "长方形状周长为:" << (w + h) * 2 << endl;
};
void area(int w, int h)
{
cout << "长方形状面积为:" << w * h << endl;
};
};
class Square : public Shape //正方形
{
public:
void area(int w,int h){};
void grith(int w,int h){};
void area(int w)
{
cout << "正方形面积" << w * w << endl;
};
void grith(int w)
{
cout << "正方形周长" << w * 4 << endl;
};
};
class Circular : public Shape //圆形
{
public:
void area(int w,int h){};
void grith(int w,int h){};
void area(int w)
{
cout << "圆形面积:" << w * w * PI << endl;
;
};
void grith(int w)
{
cout << "圆形周长" << w * 2 * PI << endl;
};
};
int main()
{
Shape *shape01;
Shape *shape02;
Shape *shape03;
Rectangle rectangle;
Square square;
Circular circular;
shape01 = &rectangle;
shape01->area(3, 4);
shape01->grith(3, 4);
shape02 = □
shape02->area(2);
shape02->grith(2);
shape03 = &circular;
shape03->area(2);
shape03->grith(2);
return 0;
}
输出结果:
【存在的问题】重载了虚函数,纯虚函数需要在每一个子类中实现,若有一个子类忘记实现,则会编译报错,有点复杂。下面是改进后的方法,在调用子类构造函数时,把长宽传进去,基类只有面积和周长这两个接口,在子类中进行了重载。编程传参一般放在初始化即构造函数初始化(个人理解)
#include <iostream>
using namespace std;
#define PI 3.14159
class Shape
{
public:
virtual void area() = 0; //纯虚函数
virtual void grith() = 0; //纯虚函数
Shape()
{
cout << "Shape构造函数" << endl;
}
virtual ~Shape() { cout << "析构Shape" << endl; }
};
class Rectangle : public Shape //长方形
{
public:
Rectangle(int x, int y)
{
cout << "构造函数"
<< "Rectangle:长 " << x << " 宽 " << y << endl;
w = x;
h = y;
};
~Rectangle() { cout << "析构Rectangle" << endl; }
void grith()
{
cout << "长方形状周长为:" << (w + h) * 2 << endl;
};
void area()
{
cout << "长方形状面积为:" << w * h << endl;
};
private:
int w, h;
};
class Square : public Shape //正方形
{
public:
Square(int x)
{
cout << "构造函数"
<< "Square 宽:" << x << endl;
w = x;
};
~Square() { cout << "析构Square" << endl; }
void area()
{
cout << "正方形面积" << w * w << endl;
};
void grith()
{
cout << "正方形周长" << w * 4 << endl;
};
private:
int w;
};
class Circular : public Shape //圆形
{
public:
Circular(int x)
{
cout << "构造函数"
<< "Circular 半径: " << x << endl;
w = x;
};
~Circular() { cout << "析构Circular" << endl; }
void area()
{
cout << "圆形面积:" << w * w * PI << endl;
;
};
void grith()
{
cout << "圆形周长" << w * 2 * PI << endl;
};
private:
int w;
};
int main()
{
Shape *shape01;
Rectangle rectangle(3, 4);
Square square(4);
Circular circular(4);
shape01 = &rectangle;
shape01->area();
shape01->grith();
shape01 = □
shape01->area();
shape01->grith();
shape01 = &circular;
shape01->area();
shape01->grith();
return 0;
}
【构造与析构顺序】构造时,先调用基类的构造函数,再调用子类的构造函数;析构时相反,先调用子类的析构函数,再调用父类的析构函数
【析构函数写成虚函数】如果 基类的析构函数不写成虚函数,则在函数结束时,只会调用子类的析构函数,不会调用父类的析构函数,在程序运行结束时,编译器会调用父类的析构函数
下面是基类析构函数写成虚函数后的效果,在子类结束时,会自动调用父类的析构函数。这种写法是推荐的!
【隐藏规则】
1、如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏。(此时显示的是基类的值)
2、如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual关键字。此时,基类的函数被隐藏。(此时显示的是基类的值)
【覆盖】:如果派生类的函数与基类的函数同名,并且参数也相同,基类函数有virtual关键字。此时,基类的函数被覆盖。(此时显示的是派生类的值)