1.继承的概念及定义
a.概念:
class Person
{
public:
void print()
{
cout << "name: " << _name << endl;
cout << "age: " << _age << endl;
}
protected:
string _name = "perter";
int _age = 18;
};
class studet : public Person
{
public:
void Set(const char* name, int age)
{
子类中能访问,类外不能访问
_name = name;
_age = age;
}
protected:
int _stuid;
};
b.定义格式:
c.子类继承父类成员访问方式变化
class person
{
protected:
string _name;
string _sex;
int _age;
};
注意:必须继承是public才支持,否则编译器会报错
class student : public person
{
public:
int _num;
};
//person和student类型不同,也不是隐式类型转换
//但这是C++编译器的特殊支持,语法天然支持
int main()
{
student s;
person p = s;
person* pp = &s;
person& pq = s;
return 0;
}
3.继承中的作用域
class A
{
public:
void fun()
{
cout << "func()" << endl;
}
};
class B : public A
{
public:
void fun(int i)
{
A::fun();
cout << "func(int i)->" <<i<<endl;
}
};
void Test()
{
B b;
b.fun(10);//父类中的func函数被隐藏,默认调用的是子类中的func函数
//b.A::fun();//可以这样显示调用
};
注意:上述俩个函数并没有构成函数重载,构成函数重载的前提是:在同一个作用域下,
他们构成函数隐藏,只要函数名相同就构成隐藏。
4.派生类的默认成员函数
相比普通的类派生类该如何调用默认成员函数?
a.构造函数:派生类自身的成员,内置类型不处理,自定义类型调用它的默认构造,继承基类的成员,如果继承类有默认构造函数,自动调用,没有默认构造函数和需要显示传参则需要派生类显示调用基类的构造函数完成初始化。
b.拷贝构造:派生类自身的成员,内置类型值拷贝/浅拷贝,自定义类型调用它的拷贝构造,继承基类的成员,如果继承类有默认构造函数,自动调用,基类没有默认拷贝构造或者派生类的成员需要深拷贝则必须调用基类的拷贝构造完成基类的拷贝初始化。
派生类中的成员如何调用基类成员函数
class Person
{
public:
Person(const char* name)
:_name(name)
{
cout << "Person(const char* name)" << endl;
}
Person(const Person& p)
:_name(p._name)
{
cout << "Person(const Person& p)" << endl;
}
Person& operator=(const Person& p)
{
cout << "Person& operator=(const Person& p)" << endl;
if (this != &p)
_name = p._name;
return *this;
}
~Person()
{
cout << "~Person()" << endl;
}
protected:
string _name;
};
class Student : public Person
{
Student(const char* name, int num)
//编译器特殊处理,显式调用
//_name(name)
//不能把父类中的成员单个处理,而是调用父类函数整体处理
//子类默认成员函数相当于一个合成版本
:Person(name)
,_num(num)
{
cout << "Student()" << endl;
}
Student(const Student& s)
//编译器特殊处理,显式调用
:Person(s)
,_num(s._num)
{
cout << "Student(const Student& s)" << endl;
}
Student& operator=(const Student& s)
{
if (this != &s)
{
//构成隐藏,加域限定符调用父类赋值重载
Person::operator=(s);
_num = s._num;
}
cout << "Student& operator=(const Student& s)" << endl;
return *this;
}
//子类的析构函数和父类的析构函数会构成隐藏
//由于多态需要,析构函数名字统一处理成destructor()
//所以调用基类析构函数时,要显示调用时需要加域限定符
~Student()
{
//Person::~person();
cout << "~Student()" << endl;
}
private:
int _num;
};
5.继承和友元
友元关系不能继承,也就是说基类友元不能访问子类私有和保护成员。
class Student;
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;
cout << s._stuNum << endl;
}
void main()
{
Person p;
Student s;
Display(p, s);
}
6.继承与静态成员函数
//求总共创建了多少个对象
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;
}
问题:如何定义一个不能被继承的类?
class Person
{
private:
Person(const string name="张三")
:_name(name)
{}
protected:
string _name;
};
C++11引用关键字final,直接报错
//class Person final
//{
//private:
// Person(const string name = "张三")
// :_name(name)
// {}
//protected:
// string _name;
//};
class Student : public Person
{
private:
int _age;
};
int main()
{
//创建对象才显示出来非法
Student s;
return 0;
}
7.2.多继承:一个子类有两个或以上直接父类时称这个继承关系为多继承
下面有一个和多线程有关的一个小题目:
class Base1{public: int _b1;};
class Base2{public: int _b2;};
class Derive : public Base1, public Base2{public : int _d;};
int main()
{
Derive d;
Base1* p1=&d;
Base2* p2=&d;
Derive* p3=&d;
return 0;
}
选项如下:
A.p1==p2==p3 B.p1<p2<p3 C.p1==p3!=p2 D.p1!=p2!=p3
//正确选项:C
这其实是一个多线程和赋值转换(切割)问题,解题如下:
那么p1,p2,p3他们的指针大小是怎么样的?
根据运算结果可得:p1==p3<p2,那么为什么呢?
我们可以看出Derive对象d中的三个成员变量在内存中的存储顺序,如何理解呢?编译器给对象d在栈帧中划分了一块空间,但是这块空间在内存中是按照继承的先后顺序按照地址从低到高进行对成员变量进行划分。
7.3.菱形继承:菱形继承是多继承的一种特殊情况。
class Person
{
public :
string _name ; // 姓名
};
class Student : public Person
{
protected :
int _num ; //学号
};
class Teacher : public Person
{
protected :
int _id ; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected :
string _majorCourse ; // 主修课程
};
void Test ()
{
// 这样会有二义性无法明确知道访问的是哪一个
Assistant a ;
a._name = "peter";
// 需要显示指定访问哪个父类的成员可以解决二义性问题,但是数据冗余问题无法解决
a.Student::_name = "xxx";
a.Teacher::_name = "yyy";
}
class Person
{
public :
string _name ; // 姓名
};
class Student : virtual public Person
{
protected :
int _num ; //学号
};
class Teacher : virtual public Person
{
protected :
int _id ; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected :
string _majorCourse ; // 主修课程
};
void Test ()
{
Assistant a ;
a._name = "peter";
}
class A
{
public:
int _a;
};
//普通菱形继承
// class B : public A
//虚拟继承
class B : virtual public A
{
public:
int _b;
};
// class C : public A
class C : virtual public A
{
public:
int _c;
};
class D : public B, public C
{
public:
int _d;
};
int main()
{
D d;
d.B::_a = 1;
d.C::_a = 2;
d._b = 3;
d._c = 4;
d._d = 5;
return 0;
}
从内存和监视中可以看出:如果不是虚拟继承,而是普通的菱形继承,会出现代码冗余和二义性的问题,那么虚拟继承如何解决这些问题的呢?
问:如何能找到基类A的成员变量?
D的基类B和C根据指针找到指针所指向的偏移量,当前位置加偏移量就能访问到A中的公共成员变量。