目录
一,继承概念及定义
定义格式
继承基类成员访问方式
二,基类和派生类对象赋值转换
三,继承中的作用域
四,派生类的默认成员函数
五,继承与友元
六,继承与静态成员
七,复杂的菱形继承及菱形虚拟继承
菱形继承
虚拟继承
虚拟继承原理
八,附
一,继承概念及定义
class Person
{
protected:
string _name = "peter";
int _age = 20;
};
class Teacher :public Person
{
public:
void print()
{
cout << "child teacher class" << endl;
cout << "name:" << _name << endl;
cout << "age:" << _age << endl;
cout << "jobid:" << _jobid << endl;
}
private:
int _jobid = 123;
};
class Stu :public Person
{
public:
void print()
{
cout << "child stu class" << endl;
cout << "name:" << _name << endl;
cout << "age:" << _age << endl;
cout << "jobid:" << _stuid << endl;
}
private:
int _stuid = 456;
};
int main()
{
Teacher teacher;
teacher.print();
Stu stu;
stu.print();
return 0;
}
定义格式
//继承方式:public、private、protected,不写默认为private;
class 派生名:[继承方式] 基类名
{
派生类新增加成员;
}
继承基类成员访问方式
二,基类和派生类对象赋值转换
class Person
{
protected:
string _name = "peter";
int _age = 20;
};
class Teacher :public Person
{
public:
Teacher()
{
_name = "zhansan";
_age = 18;
}
public:
int _jobid = 123;
};
int main()
{
Teacher teacher;
//派生类可赋值给基类对象、指针、引用
Person person = teacher;
Person* pperson = &teacher;
Person& rperson = teacher;
//基类对象不可赋值给派生类
//teacher = person;
//基类的指针可通过强制类型转换赋值给派生类指针
Teacher* pteacher = (Teacher*)pperson; //pperson指向的是派生类对象
pteacher->_jobid = 100;
Person person1;
Teacher* pteacher1 = (Teacher*)&person1; //存在越界访问
pteacher1->_jobid = 100;
return 0;
}
三,继承中的作用域
//子类成员_name构成隐藏
class Person
{
protected:
string _name = "peter";
int _age = 20;
};
class Teacher :public Person
{
public:
void print(){cout << Person::_name << endl;} //显式访问基类成员
public:
string _name = "zhansan"; //与基类成员同名,基类成员被屏蔽
int _jobid = 123;
};
//father中的fun和child中的fun不构成重载,因为不在一个作用域
//father中的fun和child中的fun构成隐藏,成员函数满足函数名相同就构成隐藏
class father
{
public:
void fun(){cout << "fun()" << endl;}
protected:
int _a = 1;
};
class child :public father
{
public:
//与父类同名函数构成隐藏
void fun(int i = 0){cout << "fun(i)" << endl;}
public:
int _b = 10;
};
四,派生类的默认成员函数
//基类默认成员函数
class Person
{
public:
Person(const char* name = "peter") //构造函数
:_name(name)
{cout << "Person()" << endl;}
Person(const Person& person) //拷贝构造函数
:_name(person._name)
{cout << "Person(const Person& person)" << endl;}
Person& operator=(const Person& person) //赋值运算符重载
{
cout << "operator=(const Person& person)" << endl;
if (this != &person)
_name = person._name;
return *this;
}
~Person() //析构函数
{
cout << "~Person()" << endl;
}
protected:
string _name;
};
//派生类默认成员函数
class Stu :public Person
{
public:
Stu(const char* name, int stuid) //构造函数
:Person(name) //调用基类构造函数初始化基类成员
,_stuid(stuid)
{cout << "Stu()" << endl;}
Stu(const Stu& stu) //拷贝构造函数
:Person(stu) //调用基类拷贝构造函数初始化基类成员
,_stuid(stu._stuid)
{cout << "Stu(const Stu& stu)" << endl;}
Stu& operator=(const Stu& stu) //赋值运算符重载
{
cout << "operator=(const Stu& stu)" << endl;
if (this != &stu)
{
Person::operator=(stu); //调用基类operator=
_stuid = stu._stuid;
}
return *this;
}
~Stu() //析构函数
{
cout << "~Stu()" << endl;
}
protected:
int _stuid;
};
int main()
{
Person person("zhansan");
Person person1(person);
Person person2;
person2 = person;
Stu stu("lisi", 123);
Stu stu1(stu);
Stu stu2("wanwu", 456);;
stu2 = stu;
return 0;
}
五,继承与友元
//基类友元
class Stu;
class Person
{
friend void print(const Person& person, const Stu& stu);
protected:
string _name = "peter";
};
class Stu :public Person
{
//friend void print(const Person& person, const Stu& stu);
protected:
int _stuid = 123;
};
void print(const Person& person, const Stu& stu)
{
cout << person._name << endl;
cout << stu._stuid<< endl; //报错,无法访问_stuid
}
int main()
{
Person person;
Stu stu;
print(person, stu);
return 0;
}
六,继承与静态成员
class Person
{
public:
Person(){++_count;}
protected:
string _name;
public:
static int _count;
};
//static成员
int Person::_count = 0;
class Teacher :public Person
{
protected:
int _jobid = 1234;
};
class Stu :public Person
{
protected:
int _stuid = 123;
};
int main()
{
Teacher teacher1;
Teacher teacher2;
Stu stu1;
Stu stu2;
cout << Person::_count << endl;
Person::_count = 0;
cout << Person::_count << endl;
return 0;
}
七,复杂的菱形继承及菱形虚拟继承
菱形继承
//菱形继承
class Person
{
public:
string _name;
};
class Teacher :public Person
{
protected:
int _jobid;
};
class Stu :public Person
{
protected:
int _stuid;
};
class Assistant :public Teacher, public Stu
{
protected:
string _majorCourse;
};
int main()
{
Assistant a;
//报错,无法明确访问哪个,有二义性
//a._name = "peter";
//需明确指定访问哪个父类,但会有数据冗余
a.Teacher::_name = "zhansan";
a.Stu::_name = "lisi";
return 0;
}
虚拟继承
//虚拟继承
class Person
{
public:
string _name;
};
class Teacher :virtual public Person
{
protected:
int _jobid;
};
class Stu :virtual public Person
{
protected:
int _stuid;
};
class Assistant :public Teacher, public Stu
{
protected:
string _majorCourse;
};
int main()
{
Assistant a;
a._name = "peter";
return 0;
}
虚拟继承原理
八,附
继承和组合
//Car和BMW/Benz构成is-a的关系
class Car
{
protected:
string _color = "white";
string _num = "GB12334";
};
class BMW :public Car
{
public:
void Drive()
{cout << "good drive" << endl;}
};
class Benz :public Car
{
public:
void Drive()
{cout << "good sit" << endl;}
};
//Tire和Car构成has-a的关系
class Tire
{
protected:
string _brand = "Michelin";
size_t _size = 17;
};
class Car
{
protected:
string _color = "white";
string _num = "GB12334";
Tire _t;
};