04737_C++程序设计_第6章_继承和派生

时间:2023-03-08 22:15:24

例6.1

使用默认内联函数实现单一继承。

 #include<iostream>

 using namespace std;

 class Point
{
private:
int x, y;
public:
Point(int a, int b)
{
x = a;
y = b;
cout << "Point..." << endl;
}
void Showxy()
{
cout << "x=" << x << "y=" << y << endl;
}
~Point()
{
cout << "Delete Point" << endl;
}
}; class Rectangle :public Point
{
private:
int H, W;
public:
Rectangle(int a, int b, int h, int w) :Point(a, b)//构造函数初始化列表
{
H = h;
W = w;
cout << "Rectangle..." << endl;
}
void Show()
{
cout << "H=" << H << ",W" << W << endl;
}
~Rectangle()
{
cout << "Delete Rectangle" << endl;
}
}; void main()
{
Rectangle r1(, , , );
r1.Showxy();
r1.Show(); system("pause");
}

例6.2

演示使用protected成员。

 #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;//基类的Show()函数
}
}; 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;
} void main()
{
Point a(, );
Rectangle r1(, , , );
a.Show();//基类对象调用基类Show()函数
r1.Show();//派生类对象调用派生类Show()函数 system("pause");
}

例6.3

使用Point和Rectangle类演示赋值兼容规则的例子。

 #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;//基类的Show()函数
}
}; 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;
} void main()//演示公有继承的赋值兼容性规则
{
Point a(, );//基类对象a
Rectangle b(, , , );//派生类对象b
a.Show();
b.Show(); Point& ra = b;//派生类对象初始化基类的引用
ra.Show();//实际调用的是基类的Show函数 Point *p = &b;//派生类对象的地址赋给指向基类的指针
p->Show();//实际调用的是基类的Show函数 Rectangle *pb = &b;//派生类指针pb
pb->Show();//调用派生类的Show函数 a = b;//派生类对象的属性值更新基类对象的属性值
a.Show(); system("pause");
}

例6.5

演示多重继承的例子。

 #include <iostream>

 using namespace std;

 class A
{
private:
int a;
public:
void setA(int x)
{
a = x;
}
void showA()
{
cout << "a=" << a << endl;
}
}; class B
{
private:
int b;
public:
void setB(int x)
{
b = x;
}
void showB()
{
cout << "b=" << b << endl;
}
}; class C :public A, public B
{
private:
int c;
public:
void setC(int x, int y)
{
c = x;
setB(y);
}
void showC()
{
showB();
cout << "c=" << c << endl;
}
}; void main()
{
C obj; obj.setA();
obj.showA();//输出a=53
obj.setC(, );
obj.showC();//输出b=58 c=55 system("pause");
}

例6.6

访问具有二义性的例子。

 #include <iostream>

 using namespace std;

 class A
{
public:
void func()
{
cout << "a.func" << endl;
}
}; class B
{
public:
void func()
{
cout << "b.func" << endl;
}
void gunc()
{
cout << "b.gunc" << endl;
}
}; class C :public A, public B
{
public:
void gunc()
{
cout << "c.gunc" << endl;
}
void hun1()
{
A::func();
}
void hun2()
{
B::func();
}
}; void main()
{
C obj; obj.A::func();//输出a.func
obj.B::func();//输出b.func
obj.B::gunc();//输出b.func
obj.C::gunc();//输出c.gunc obj.gunc();//输出c.gunc
obj.hun1();//输出a.func
obj.hun2();//输出b.func system("pause");
}

123