类的继承

面向对象的设计思想,类对数据做了封装,并可以加入访问权限,类的继承是面向对象思想的精髓。类的继承可以让新类从以有的类中获得已有的特征。原有类称为基类或父类,新类称为派生类或子类。

语法:

1
2
3
4
class  子类类名:[继承方式]  父类类名
{

} ;

继承的方式有三种:

1.共有继承:(使用最广泛)

在派生类中原来的公有成员,保护成员身份不变。原私有成员仍不可访问。

2.私有继承:

在派生类中原来的公有成员,保护成员都称为了保护成员。原私有成员仍不可访问。

3.保护继承:

在派生类中原来的公有成员,保护成员都称为了私有成员。原私有成员仍不可访问。

派生类的构造函数和析构函数

派生类继承基类过程中,基类的构造函数和析构是不能继承下来的。所以,派生类必须设置自己的构造函数和析构函数。

派生类构造函数语法:

1
2
3
4
派生类名::派生类名(参数):基类名(参数),派生类新成员()
{

}

构造调用顺序:

基类构造--->派生类构造

析构函数调用顺序

派生类析构--->基类析构

代码演示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>

using namespace std;

class Man
{
public:
Man(string sn, int i):name(sn),age(i)
{
cout << "Man construct" << endl;
}

~Man()
{
cout << "Man destruct" << endl;
}

void dis()
{
cout << "ame:" << name << endl;
cout << "age:" << age << endl;
}
private:
string name;
int age;
};

class Birthday
{
public:
Birthday(int y, int m):year(y), month(m)
{
cout << "Birthday construct " << endl;
}

~Birthday()
{
cout << "Birthday destruct " << endl;
}
private:
int year;
int month;
};

class Student:public Man
{
public:
Student(string name, int age, float fs):Man(name, age),bday(10, 10),_score(fs)
{
cout << "Student construct" << endl;
}

~Student()
{
cout << "Student destruct" << endl;
}
private:
float _score;
Birthday bday;
};

int main()
{
Student stu("wpf", 18, 100);
stu.dis();
}

运行结果:

同名隐藏

子类中定义了与父类同名的方法(不管参数),子类的该方法将会隐藏掉所有的父类的同名方法;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
class Father
{
public:
void show()
{
cout << "Father show()" << endl;
}

void show(int a)
{
cout << "Father show(int a)" << endl;
}
};

class Son : public Father
{
public:
void show()
{
cout << "Son show()" << endl;
}
};

int main()
{
Son son;
son.show(); //调用子类中的方法
son.show(1); //父类中的同名方法,void show(int a)被隐藏,无法调用
getchar();
}

多继承

多继承是的新建的获得多个类(>=2)中获得已有的特征, 并非所有面向对象的语言都有这种语法,如Java,C#取消了多继承,多继承可能引起多种问题,如二义性等。

多继承语法:

1
2
3
4
派生类名:public 基类名1,public 基类名2
{

};

代码演示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>

using namespace std;

//基类X
class X
{
public:
X(int a):i(a)
{
cout << "construct X" << endl;
}

void displayX()
{
cout << "X:" << i << endl;
}
private:
int i;
};

//基类Y
class Y
{
public:
Y(int a):i(a)
{
cout << "construct Y" << endl;
}

void displayY()
{
cout << "Y:" << i << endl;
}
private:
int i;
};

//派生类Z
class Z: public X, public Y
{
public:
Z(int a, int b, int c):X(a), Y(b), i(c)
{
cout << "construct Z" << endl;
}

void displayZ()
{
cout << "Z:" << i << endl;
}
private:
int i;
};

int main()
{
Z z(1,2,3);
z.displayX();
z.displayY();
z.displayZ();
}

运行结果:

# 继承

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×