第7章继承与派生
约 1449 字大约 5 分钟
2026-11-10
7-1 P257 Rectangle类公有继承Point类
源文件:7_01.cpp
/*
7-1 P257
Rectangle类公有继承Point类
派生类Rectangle就包含了它的全部基类中的所有非静态成员(除了构造和析构函数)。
*/
#include <iostream>
using namespace std;
class Point
{
public:
Point(float xx = 0, float yy = 0) : x(xx), y(yy) {}
void initPoint(float x = 0, float y = 0)
{
this->x = x;
this->y = y;
}
void move(float offX, float offY)
{
x += offX;
y += offY;
}
void show() { cout << "this is Point's Show:" << x << y << endl; }
float getX() const { return x; }
float getY() const { return y; }
private:
float x, y;
};
class Rectangle : public Point
{
public:
void initRectangle(float x = 0, float y = 0, float w = 0, float h = 0)
{
initPoint(x, y);
this->w = w;
this->h = h;
}
void show() { cout << "this is Rectangle's Show:" << w << h << endl; }
float getW() const { return w; }
float getH() const { return h; }
private:
float w, h;
};
int main()
{
Rectangle r1;
r1.initRectangle(1, 1, 3.0, 4.0);
r1.move(2, 1);
cout << r1.getX() << ',' << r1.getY() << ',' << r1.getW() << ',' << r1.getH() << endl;
r1.show();
return 0;
}7-3 P264 类型兼容性规则实例
源文件:7_03.cpp
/*
7-3 P264
类型兼容性规则实例
*/
#include <iostream>
using namespace std;
class Base1 //基类Base1定义
{
public:
void display() const
{
cout << "Base1::display()" << endl;
}
};
class Base2 : public Base1 //公有派生类Base2定义
{
public:
void display() const
{
cout << "Base2::display()" << endl;
}
};
class Derived : public Base2 //公有派生类Derived定义
{
public:
void display() const
{
cout << "Derived::display()" << endl;
}
};
void fun(Base1 *p) //参数为执行基类对象的指针
{
//根据类型兼容性规则。派生类对象的地址可以隐含转换为指向基类的指针
p->display();
}
int main()
{
Base1 b1;
Base2 b2;
Derived d1;
fun(&b1);
fun(&b2);
fun(&d1);
return 0;
}7-4 P266 派生类构造函数举例(多继承,含有内嵌对象)
源文件:7_04.cpp
/*
7-4 P266
派生类构造函数举例(多继承,含有内嵌对象)
*/
#include <iostream>
using namespace std;
class Base1
{
public:
Base1(int i) { cout << "Constructing Base1 " << i << endl; }
};
class Base2
{
public:
Base2(int i) { cout << "Constructing Base2 " << i << endl; }
};
class Base3
{
public:
Base3() { cout << "Constructing Base3 *" << endl; }
};
class Derived : public Base2, public Base1, public Base3
{ //基类构造顺序Base2,Base1,Base3
public:
Derived(int a, int b, int c, int d) : Base1(a), m2(d), m1(c), Base2(b) {}
//基类Base1和Base2调用有参数的构造函数,基类Base3调用无参数的构造函数(默认构造函数)
//对象m1和m2调用有参数的构造函数,对象m3调用无参数的构造函数
private:
//对象构造顺序为m1,m2,m3
Base1 m1;
Base2 m2;
Base3 m3;
};
int main()
{
Derived obj(1, 2, 3, 4);
return 0;
}7-5 P269 派生类析构函数举例(多继承,含有内嵌对象)
源文件:7_05.cpp
/*
7-5 P269
派生类析构函数举例(多继承,含有内嵌对象)
对例7-4进行修改
*/
#include <iostream>
using namespace std;
class Base1
{
public:
Base1(int i) { cout << "Constructing Base1 " << i << endl; }
~Base1() { cout << "Destructing Base1" << endl; };
};
class Base2
{
public:
Base2(int i) { cout << "Constructing Base2 " << i << endl; }
~Base2() { cout << "Destructing Base2" << endl; };
};
class Base3
{
public:
Base3() { cout << "Constructing Base3 *" << endl; }
~Base3() { cout << "Destructing Base3" << endl; };
};
class Derived : public Base2, public Base1, public Base3
{ //基类构造顺序Base2,Base1,Base3
public:
Derived(int a, int b, int c, int d) : Base1(a), m2(d), m1(c), Base2(b) {}
//基类Base1和Base2调用有参数的构造函数,基类Base3调用无参数的构造函数(默认构造函数)
//对象m1和m2调用有参数的构造函数,对象m3调用无参数的构造函数
// Derived类中无析构函数,系统会自动生成默认析构函数
//析构顺序与构造顺序严格相反
private:
//对象构造顺序为m1,m2,m3
Base1 m1;
Base2 m2;
Base3 m3;
};
int main()
{
Derived obj(1, 2, 3, 4);
return 0;
}7-6 P273 多继承同名隐藏规则(1)
源文件:7_06.cpp
/*
7-6 P273
多继承同名隐藏规则(1)
*/
#include <iostream>
using namespace std;
class Base1 //定义基类Base1
{
public:
int var;
void fun() { cout << "Member of Base1:" << var << endl; }
};
class Base2 //定义基类Base2
{
public:
int var;
void fun() { cout << "Member of Base2:" << var << endl; }
};
class Derived : public Base1, public Base2 //定义派生类Derived
{
public:
int var;
void fun() { cout << "Member of Derived:" << var << endl; }
};
int main()
{
Derived d;
Derived *p = &d;
d.var = 1; //访问Derived类成员
d.fun();
d.Base1::var = 2; //访问Base1类成员
d.Base1::fun();
p->Base2::var = 3; //访问Base2类成员
p->Base2::fun();
return 0;
}7-7 P273 多继承同名隐藏规则(2)
源文件:7_07.cpp
/*
7-7 P273
多继承同名隐藏规则(2)
*/
#include <iostream>
using namespace std;
class Base0 //定义基类Base0
{
public:
int var0;
void fun0() { cout << "Member of Base0" << endl; }
};
class Base1 : public Base0 //定义派生类Base1
{
public:
int var1;
};
class Base2 : public Base0 //定义派生类Base2
{
public:
int var2;
};
class Derived : public Base1, public Base2 //定义派生类Derived
{
public:
int var;
void fun() { cout << "Member of Derived:" << var << endl; }
};
int main()
{
Derived d; //定义Derived对象d
//使用直接基类
d.Base1::var0 = 2; // Base1中的var0
d.Base1::fun0(); // Base1中的fun0()
//使用直接基类
d.Base2::var0 = 3; // Base2中的var0
d.Base2::fun0(); // Base3中的fun0()
//直接访问
// d.var0=1;//错误,不知道var0是Base1中的还是Base2中的
return 0;
}7-8 P77 虚基类举例
源文件:7_08.cpp
/*
7-8 P77
虚基类举例
*/
#include <iostream>
using namespace std;
class Base0 //定义基类Base0
{
// Base0是最远虚基类
//虚基类的成员是由最远派生类的构造函数通过调用虚基类的构造函数进行初始化的。
//在建立对象时,只有最远派生类的构造函数调用虚基类的构造函数,其他类对虚基类构造函数的调用被忽略。
public:
int var0;
void fun0() { cout << "Base0 var0=" << var0 << endl; }
};
class Base1 : virtual public Base0 //定义派生类Base1
{
public: //新增外部接口
int var1;
};
class Base2 : virtual public Base0 //定义派生类Base2
{
public: //新增外部接口
int var2;
};
class Derived : public Base1, public Base2 //定义派生类Derived
{ // Derived是最远派生类
public: //新增外部接口
int var;
void fun() { cout << "Derived" << endl; }
};
int main()
{
Derived d;
d.var0 = 2; //直接访问虚基类的数据成员
d.fun0(); //直接访问虚基类的函数成员
return 0;
}