#include <iostream.h>
class CPoint
{
public:
CPoint(int m,int n)
{
x=m;
y=n;
}
Print()
{
cout<<"x="<<x<<" y="<<y<<endl;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
private:
int x,y;
};
class Jx:private CPoint
{
public:
Jx(int m,int n,int x=0,int y=0):CPoint(m,n)
{
a=x;
b=y;
}
print()
{
int m,n;
m=Getx();
n=Gety();
cout<<"a="<<a<<" b="<<b<<" m="<<m<<" n="<<n<<endl;
cout<<"矩形的面积是:"<<(a-m)*(b-n)<<endl;
cout<<"矩形的周长是:"<<2*((a-m)+(b-n))<<endl;
}
private:
int a,b;
};
class line:private CPoint
{
public:
line(int m,int n,int x=0):CPoint(m,n)
{
c=x;
}
print()
{
int a;
a=Getx();
cout<<"a="<<a<<" c="<<c<<endl;
cout<<"直线的距离是:"<<c-a<<endl;
}
private:
int c;
};
void main()
{
CPoint A(1,1);
A.Print();
line B(3,4,5);
B.print();
Jx C(0,0,2,4);
C.print();
}
本文通过C++代码示例介绍了类的继承与封装概念。主要展示了一个基类CPoint及其两个派生类Jx(用于计算矩形属性)和line(用于计算直线距离),演示了如何在派生类中使用基类的方法。
6434

被折叠的 条评论
为什么被折叠?



