设计一个程序,求正方形和长方形的周长,具体要求如下:
(1) 定义正方形类Square作为基类,包含数据成员边长,以及构造函数、求正方形周长的虚函数、输出函数。
(2) 定义类Square的共有派生类Rectangle,新增一条边长,与派生成员共同作为长方形边长,以及求长方形周长和输出数据成员的函数。
(3) 在主函数中定义的类进行测试,用基类的指针实现动态联编。
#include <iostream>
#include <string>
using namespace std;
class Square
{
protected :
int length;
public:
Square(int l)
{
length = l;
}
virtual int getperimeter(