c++基类和派生类的框架
源程序:
#include <iostream>
using namespace std;
class Point //基类,也叫父类
{
protected:
int x,y;
public:
Point(int a,int b)
{
x=a;
y=b;
}
void show()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
};
class Rectangle:public Point //派生类继承基类,派生类也叫子类
{
private:
int H,W;
public:
Rectangle(int ,int ,int ,int); //子类的构造函数没有定义
void show()
{
cout<<"x="<<x<<",y="<<y<<",H="<<H<<",W="<<W<<endl;
}
};
Rectangle::Rectangle(int a,int b,int h,int w):Point(a,b) //构造函数的定义,与基类建立关系
{
H=h;
W=w;
}
int main()
{
Point a(3,4);
Rectangle r1(3,4,5,6);
a.show();
r1.show();
return 1;
}