//Point.h
#ifndef POINT_H
#define POINT_H
class Point{
public:
void initPoint(float x=0,float y=0)
{
this->x=x;
this->y=y;
}
void move(float offX,float offY)
{
x+=offX;
y+=offY;
}
float getX()const {return x;}
float getY()const {return y;}
private:
float x,y;
};
#endif
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "Point.h"
class Rectangle:public Point{
public:
void initRectangle(float x,float y,float w,float h){
initPoint(x,y);
this->w=w;
this->h=h;
}
float getH() const{return h;}
float getW() const{return w;}
private:
float w,h;
};
#endif
//主函数
#include
#include
#include "Rectangle.h"
using namespace std;
int main()
{
Rectangle rect;
rect.initRectangle(2,3,20,10);
rect.move(3,2);
cout<<"The data of rect(x,y,w,h):"<<endl;
cout<<rect.getX()<<","<<rect.getY()<<","<<rect.getW()<<","<<rect.getH()<<endl;
return 0;
}
//运行结果
[Error] #endif without #if
试过使用#if!defined()替代#ifndef