第7章 类的继承
1.类的继承与派生的描述

2.继承与派生的目的

3.派生类的构成

4.不同继承方式及类成员的访问控制

5.公有继承

举例:
#ifndef_POINT_H
#define_POINT_H
class Point {
public:
void initPoint(float x=0,float y=0)
{ this-> = x; this-> = 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//_POINT_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//_RECTANGLE_H
#include<iostream>
#include<cmath>
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;
}
6.保护继承(protected)
protected 成员的特点与作用

错误:
class A {
protect:
int x;
};
int main() {
A a;
a.x = 5;
}
正确:
class A {
protect:
int x;
};
class B :public A {
public:
void function();
};
void B::function() {
x = 5;
}
7.私有继承

8.向上转型

注意:绝对不要重新定义继承而来的非虚函数
9.继承时的构造函数

10.单一继承时构造函数的定义

多继承时构造函数的定义

多继承且有对象成员时的构造函数

11.派生类与基类的构造函数

12.复制构造函数

析构函数:

13.作用域限定

14.二义性问题;


Derived类对象d的存储结构示意图:

15.虚基类的语法和用途