定义类的头文件
Shape.h
#ifndef __SHAPE_H
#define __SHAPE_H
#include <iostream>
using namespace std;
class Shape
{
public:
Shape();
~Shape();
virtual float area() = 0;
virtual float perimeter() = 0;
};
class Square :public Shape
{
private:
float width;
public:
Square();
Square(float b);
~Square();
float area();
float perimeter();
void setWidth(float b);
void print();
};
class Rect : public Shape
{
public:
Rect();
Rect(float x, float y);
~Rect();
float area();
float perimeter();
void setWidth(float x, float y);
void print();
private:
float width;
float height;
};
#endif // !__SHAPE_H
相关函数的定义以及使用
Shape.cpp
#include "Shape.h"
Shape::Shape()
{
cout << "基类构造函数" << endl;
}
Shape::~Shape()
{
cout << "基类析构函数" << endl;
}
Square::Square()
{
width = 0;
}
Square::Square(float b)
{
width = b;
}