C++ 继承

C++——继承


一、基本介绍

img

有些类与类之间存在特殊的关系

有些类之间拥有一样的性质,但是各自又拥有只属于自己的特性。

在这个时候我们可以使用继承,来减少重复代码。

二、继承的基本语法

语法: class 子类 : 继承方式 父类

父类:包含子类要继承的部分

子类中的成员,包含两大部分:

一类是从父类继承来的,一类是自己增加的成员。

从基类继承过来的表现其共性,而新增的成员表现其特性。

代码示例:

class Base
{
public:
int i = 1;
};

class Derive:public Base
{
public:
int b = 2;
};

程序写的很简单,但清楚地表现了继承的基本用法。

三、继承方式

继承方式一共有三种:

1.公共继承(public)

2.保护继承(protected)

3.私有继承(private)

下面我们用例子来了解三种继承方式的不同。

1.公共继承

基类成员访问属性 派生类成员访问属性
private 无法访问
protected protected
public public
class Base
{
public:
    Base(int a = 0, int b = 0, int c = 0)
        : _pub(a)
        , _pro(b)
        , _pri(c)
    {}
    int _pub = 1;
protected:
    int _pro = 2;
private:
    int _pri = 3;
};
class Derive :public Base
{
public:
    void display()
    {
        cout << "_pub" << _pub << endl;
        cout << "_pro" << _pro << endl;
        cout << "_pri" << _pri << endl;
    }
};

在public继承中,派生类访问基类的private成员时就会报错。

2.保护继承

基类成员访问属性 派生类成员访问属性
private 无法访问
protected protected
public protected
class Base
{
public:
    Base(int a = 0, int b = 0, int c = 0)
        : _pub(a)
        , _pro(b)
        , _pri(c)
    {}
    int _pub = 1;
protected:
    int _pro = 2;
private:
    int _pri = 3;
};
class Derive :protected Base
{
public:
    void display()
    {
        _pro = 10;
    }
};
int main()
{
    Derive d1;
    d1._pub = 4;
    d1._pro = 5;
    d1._pri = 6;
    return 0;
}

在protected继承中在派生类中可以访问基类中的protected成员,但是在main函数中pub和_pro都不能被访问了,如果想要成员能在函数内部被访问,在函数外面不能被访问就用protected继承。

3.私有继承

基类成员访问属性 派生类成员访问属性
private 无法访问
protected private
public private
class Base
{
public:
    Base(int a = 0, int b = 0, int c = 0)
        : _pub(a)
        , _pro(b)
        , _pri(c)
    {}
    int _pub = 1;
protected:
    int _pro = 2;
private:
    int _pri = 3;
};
class Derive :private Base
{
public:
    void display()
    {
        _pub = 20;
        _pro = 10;
        _pri = 30;
    }
};
int main()
{
    Derive d1;
    d1._pub = 4;
    d1._pro = 5;
    d1._pri = 6;
    return 0;
}

在私有继承中,从基类继承下来的东西全部就变为了派生类私有的,所以你在外部完全访问不了。

保护继承和私有继承的区别:

如果再次创建一个派生类来访问它的protected成员时是可以访问的,但是private成员就访问不了

四、继承种类

继承种类有两种,单继承和多继承。

1.单继承

img

单继承:一个子类只有一个父类。

示例:

#include<iostream>
using namespace std;
class Base
{
public:
int i = 1;
};
class Derive:public Base
{
public:
int b = 2;
};
int main()
{
Derive d1;
cout << sizeof(d1) << endl;
return 0;
}

单继承的格式:

class 派生类名:继承方式 基类名 
{
派生类成员
};

2.多继承

多继承:一个子类可以有多个父类。

示例:

class Base1
{
public:
    int a = 1;
};
class Base2
{
public:
    int b = 2;
};
class Derive:public Base1, public Base2
{
    int c = 3;
};

多继承的格式:

class 派生类名:继承方式 基类名,继承方式 基类名
{
派生类成员;
};

五、继承中的构造与析构

#include <iostream>
using namespace std;
class Object
{
public:
Object(const  char* s)
{
cout<<"Object()"<<"  "<<s<<endl;
}
~Object()
{
cout<<"~Object()"<<endl;
}
};
class Parent : public Object
{
public:
Parent(const  char* s) : Object(s)
{
cout<<"Parent()"<<"  "<<s<<endl;
}
~Parent()
{
cout<<"~Parent()"<<endl;
}
};
class Child : public Parent
{
protected:
Object  o1;
Object  o2;
public:
Child()  : o2("o2"), o1("o1"), Parent("Parameter from  Child!")
{
cout<<"Child()"<<endl;
}
~Child()
{
cout<<"~Child()"<<endl;
}
};
void run05()
{
Child  child;
}
int main05(int argc, char *argv[])
{
cout<<"demo05_extend_construct_destory.cpp"<<endl;
run05();
system("pause");
return  0;
}

原则:先构造父类,再构造成员变量、最后构造自己

先析构自己,在析构成员变量、最后析构父类

//先构造的对象,后释放

六、继承中的作用域

(1) 在继承体系中基类和派生类都有独立的作用域。
(2) 子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫“隐藏”,也叫“重定义”(在子类成员函数中,可以使用 基类::基类成员 显示访问 )
(3) 需要注意的是如果成员函数的隐藏,只需要函数名相同就构成了隐藏。
(4) 注意:在实际工程中,继承体系里最好不要定义同名成员。

class Person
{
protected:
	string _name = "小李子"; //姓名
	int _num = 111; //身份证号
};

class Student :public Person
{
public:
	void Print(){
		cout << "姓名: " << _name << endl;
		cout << "身份证: " << Person::_num << endl;
		cout << "学号: " << _num << endl;
	}
protected:
	int _num = 999; //学号
};

void Test()
{
	Student s1;
	s1.Print();
}

七、继承与友元

友元关系是不能继承的,也就是说基类友元不能访问子类私有和保护成员。

实例验证:

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

class Person
{
public:
	friend void Display(const Person& p, const Student& s);
protected:
	string _name; //姓名
};

class Student :public Person
{
protected:
	int _stuNum; //学号
};

void Display(const Person& p, const Student& s)
{
	cout << p._name << endl;   //error
	cout << s._stuNum << endl;  //error
}

int main()
{
	Person p;
	Student s;
	Display(p, s);
	return 0;
}

运行这段代码,会报错。

八、继承与静态成员

如果基类定义了static静态成员,则整个继承体系里只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例。

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

class Person
{
public:
	Person(){
		++_count;
	}
protected:
	string _name; //姓名
public:
	static int _count; //统计人数
};
int Person::_count = 0;

class Student :public Person
{
protected:
	int _stuNum; //学号
};

class Graduate : public Student
{
protected:
	string _seminarCourse;  //研究科目
};

void TestPerson()
{
	Student s1;
	Student s2;
	Student s3;
	Graduate s4;
	cout << "人数: " << Person::_count << endl;
	Student::_count = 0;
	cout << "人数: " << Person::_count << endl;
}

int main()
{
	TestPerson();
	system("pause");
	return 0;
}

运行结果:

img

posted @ 2019-10-12 17:45  慕慕慕慕  阅读(194)  评论(0编辑  收藏  举报