概述
覆盖、保护成员、派生类的构造函数
覆盖
派生类可以定义一个和基类成员同名的成员,这就叫覆盖。在派生类中访问这类成员时,缺省的情况是访问派生类中定义的成员。要在派生类中访问由基类定义的同名成员时,要使用作用域符号::。
//基类和派生类有同名成员的情况:
class base
{
int j;
public:
int i;
void func();
};
class derived:public base
{
public:
int i;
void access();
void func();
};
void derived::access()
{
j = 5;
//错误:j是私有成员
i = 5;
//引用的是派生类的i
base::i = 5;
//引用的是基类的i
func();
//派生类的
base:func();
//基类的
}
derived obj;
obj.i = 1;
obj.base::i = 1;
//一般来说基类与派生类不定义同名成员变量。
//obj占用的存储空间:base::j、base::i、i。
类的保护成员
另一种存取权限说明符:protected
基类的practice成员,可以被下列函数访问:
1、基类的成员函数
2、基类的友元函数
基类的public成员,可以被下列函数访问:
1、基类的成员函数
2、基类的友元函数
3、派生类的成员函数
4、派生类的友元函数
5、其他函数
基类的protected成员,可以被下列函数访问:
1、基类的成员函数
2、基类的友元函数
3、派生类的成员函数可以访问当前对象的基类的保护成员
class Father
{
private:
int nPrivate;
//私有成员
public:
int nPublic;
//公有成员
protected:
int nProtected; //保护成员
};
class Son :public Father
{
void AccessFather()
{
nPublic = 1;
//正确
nPrivate = 1;
//错误
nProtected = 1;
//正确,访问从基类继承的protected成员
Son f;
f.nProtected = 1;
//错误,f不是当前对象
}
};
int main()
{
Father f;
Son s;
f.nPublic = 1;
//正确
s.nPublic = 1;
//正确
f.nProtected = 1;
//错误
f.nPrivate = 1;
//错误
s.nProtected = 1;
//错误
s.nPrivate = 1;
//错误
return 0;
}
派生类的构造函数
class Bug
{
private:
int nLegs;
int nColor;
public:
int nType;
Bug(int legs, int color);
void PrintBug() {}
};
class FlyBug :public Bug
{
private:
int nWings;
public:
FlyBug(int legs, int color, int wings);
};
Bug::Bug(int legs, int color)
{
nLegs = legs;
nColor = color;
}
/*
//错误的FlyBug构造函数
FlyBug::FlyBug(int legs, int color, int wings)
{
nLegs = legs;
//不能访问
nColor = color; //不能访问
nType = 1;
//正确
nWings = wings;
}
*/
//正确的FlyBug构造函数
FlyBug::FlyBug(int legs, int color, int wings):Bug(legs, color)
{
nWings = wings;
}
/*
也可以这么写
FlyBug::FlyBug(int legs, int color, int wings):Bug(legs, color), nWings(wings){}
*/
int main()
{
FlyBug fb(2, 3, 4);
fb.PrintBug();
fb.nType = 1;
fb.nLegs = 2;
//错误:nLegs是private成员
return 0;
}
在创建派生类的对象时,需要调用基类的构造函数:初始化派生类对象中从基类继承的成员。在执行一个派生类的构造函数之前,总是先执行基类的构造函数。
调用基类构造函数的两种方式:
1、显示方式:在派生类的构造函数中,为基类的构造函数提供参数。
derived::dervied(arg_dervied-list):base(arg_base-list)
2、隐式方式:在派生类的构造函数中,省略基类构造函数时,派生类的构造函数则自动调用基类的默认构造函数。
派生类的析构函数被执行时,执行完派生类的析构函数之后,自动调用基类的析构函数。
#include <iostream>
using namespace std;
class Base
{
public:
int n;
Base(int i):n(i)
{
cout << "Base " << n << " constructed" << endl;
}
~Base()
{
cout << "Base " << n << " destructed" << endl;
}
};
class Derived:public Base
{
public:
Derived(int i):Base(i)
//此处必须要用显示方式(加上初始化列表)(因为基类没有无参构造函数)
{
cout << "Derived constructed" << endl;
}
~Derived()
{
cout << "Derived destructed" << endl;
}
};
int main()
{
Derived obj(3);
return 0;
}
/*
输出:
Base 3 constructed
Derived constructed
Derived destructed
Base 3 destructed
*/
包含成员对象的派生类的构造函数写法
class Bug
{
private:
int nLegs;
int nColor;
public:
int nType;
Bug(int legs, int color);
void PrintBug() {}
};
class Skill
{
public:
Skill(int n) {}
};
class FlyBug:public Bug
{
int nWings;
Skill sk1, sk2;
public:
FlyBug(int legs, int color, int wings);
};
FlyBug::FlyBug(int legs, int color, int wings):Bug(legs, color), sk1(5), sk2(color), nWings(wings) {}
在创建派生类的对象时:
1、先执行基类的构造函数,用以初始化派生类对象中从基类继承的成员。
2、再执行成员对象类的构造函数,用以初始化派生类对象中成员对象。
3、最后执行派生类自己的构造函数。
在派生类对象消亡时:
1、先执行派生类自己的析构函数。
2、再依次执行各成员对象类的析构函数。
3、最后执行基类的析构函数。
析构函数的调用顺序与构造函数的调用顺序相反。
public继承的赋值兼容规则
class base{};
class derived:public base{};
base b;
derived d;
//1)派生类的对象可以赋值给基类对象
b = d;
//反过来不行
//2)派生类对象可以初始化基类引用
base &br = d;
//3)派生类对象的地址可以赋值给基类指针
base *pb = &d;
//⚠️注意:如果派生方式是private或protected,则上述三条不可行。
直接基类与间接基类
1、类A派生类B,类B派生类C,类C派生类D,…
-类A是类B的直接基类
-类B是类C的直接基类,类A是类C的间接基类
-类C是类D的直接基类,类A、B是类D间接基类
2、声明类时,只需要列出它的直接基类
派生类沿着类的层次自动向上继承它的间接基类
派生类的成员包括:派生类自己定义的成员、直接基类中的所有成员、所有间接基类的全部成员
#include <iostream>
using namespace std;
class Base
{
public:
int n;
Base(int i):n(i)
{
cout << "Base " << n << " constructed" << endl;
}
~Base()
{
cout << "Base " << n << " destructed" << endl;
}
};
class Derived:public Base
{
public:
Derived(int i):Base(i)
//此处必须要用显示方式(加上初始化列表)(因为基类没有无参构造函数)
{
cout << "Derived constructed" << endl;
}
~Derived()
{
cout << "Derived destructed" << endl;
}
};
class MoreDerived:public Derived
{
public:
MoreDerived():Derived(4)
//此处必须要用显示方式(加上初始化列表)(因为基类没有无参构造函数)
{
cout << "MoreDerived constructed" << endl;
}
~MoreDerived()
{
cout << "MoreDerived destructed" << endl;
}
};
int main()
{
MoreDerived obj;
return 0;
}
/*
输出:
Base 4 constructed
Derived constructed
MoreDerived constructed
MoreDerived destructed
Derived destructed
Base 4 destructed
*/
最后
以上就是能干小刺猬为你收集整理的覆盖、保护成员、派生类的构造函数、public继承的赋值兼容规则、直接基类与间接基类的全部内容,希望文章能够帮你解决覆盖、保护成员、派生类的构造函数、public继承的赋值兼容规则、直接基类与间接基类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复