代码演示如下
#include
#include
using namespace std;
class Person
{
public:
void Print()
{
cout << "name:" << _name << endl;
cout << "age:" << _age << endl;
}
protected:
string _name = "peter"; // 姓名
int _age = 18;
};
class Student : public Person
{
protected:
int _stuid;
};
int main()
{
Person p;
Student s;
p.Print();
s.Print();
return 0;
}
Person是父类,也称作基类。Student是子类,也称作派生类。
#include
#include
using namespace std;
class Person
{
public:
// 缺省
string _name = "陈志浩"; // 姓名
string _sex = "男"; // 性别
int _age = 25; // 年龄
};
class Student : public Person
{
public:
int _No = 2019124084; // 学号
};
void Test()
{
Person pobj;
Student sobj;
// 修改子类的成员变量方便演示
sobj._name = "chenzhiiao";
sobj._age = 23;
}
int main()
{
Test();
return 0;
}
来上代码
#include
using namespace std;
class A {
public:
void fun()
{
cout << "func()" << endl;
}
};
class B : public A {
public:
void fun(int i)
{
cout << "func(int i)->" << i << endl;
}
};
void Test()
{
// B中的fun和A中的fun不是构成重载,因为不是在同一作用域
// B中的fun和A中的fun构成隐藏,成员函数满足函数名相同就构成隐藏。
B b;
b.fun(10);
b.A::fun();//可以使用 基类::基类成员 显示访问
};
int main()
{
Test();
return 0;
}
派生类的6个默认成员函数,默认就是我们不写但是会自己生成,那么这4(只关注 构造、拷贝构造、析构、赋值重载)个默认成员函数是如何生成的呢?
#include
#include
#include
using namespace std;
class Person
{
public:
friend ostream& operator<<(ostream& out, const Person& p);
Person(const char* name = "peter")
: _name(name)
{
cout << "Person()" << 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; // 姓名
};
ostream& operator<<(ostream& out, const Person& p)
{
out << "name : " << p._name << endl;
return out;
}
class Student : public Person{
public:
friend ostream& operator<<(ostream& out, const Student& s);
Student(const char* name = "chenzhiao", int no = 2019)
:Person(name)
,_no(no)
{
cout << "Student()" << endl;
}
~Student()
{
cout << "~Student()" << endl;
}
protected:
int _no;
};
ostream& operator<<(ostream& out, const Student& s)
{
out << "name : " << s._name << ",no : " << s._no << endl;
return out;
}
void Test()
{
Student s1;
cout << s1;
};
int main()
{
Test();
return 0;
}
Student(const Student& s)
: Person(s), _no(s._no)
{
cout << "Student(const Student& s)" << endl;
}
Student& operator=(const Student& s)
{
if (this != &s)
{
Person::operator=(s);
//这里必须要用类名指定,否则子类的operator=会把父类的
//的operator=隐藏掉,导致无限递归调用子类的operator=导致栈溢出
_no = s._no;
}
return *this;
}
~Student()
{
cout << "~Student()" << endl;
Person::~Person();
//C++的编译器保证了这个代码默认会执行,但是必须要放在最后一行才符合规则
//不能让程序员去手动指定Person::~Person();必须要放在最后一行
//万一程序员不小心放在了第一行,会导致一些不可预料的错误,所以不要显示调用
//这和上面3个默认成员的规则不一样,需要多注意
}
来段代码
#include
#include
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;
Person s5;
cout << " 人数 :" << Person::_count << endl;
Student::_count = 0;
cout << " 人数 :" << Person::_count << endl;
}
int main()
{
TestPerson();
return 0;
}
#include
#include
using namespace std;
class Person{
public:
string _name;
};
class Student : public Person{
protected:
int _stuId;
};
class Teacher : public Person{
protected:
int _teaId;
};
class Assistant : public Student, public Teacher{
protected:
string _majorCourse;
};
void Test()
{
Assistant a;
//a._name = "chenzhiao"; 这样会有二义性无法明确知道访问的是哪一个
// 需要显式指定访问哪个父类的成员可以解决二义性的问题,但是数据冗余的问题还是无法解决
a.Student::_name = "xxx";
a.Teacher::_name = "yyy";
}
int main()
{
Test();
return 0;
}
#include
#include
using namespace std;
class Person{
public:
string _name;
};
class Student : virtual public Person{
protected:
int _stuId;
};
class Teacher : virtual public Person{
protected:
int _teaId;
};
class Assistant : public Student, public Teacher{
protected:
string _majorCourse;
};
void Test()
{
Assistant a;
a._name = "chenzhiao";
}
int main()
{
Test();
return 0;
}
// 在菱形继承的“肩膀处”添加完 virtual 关键字,完成虚拟继承,可以解决数据的二义性和冗余问题。我们来看一下监视窗口
#include
#include
using namespace std;
class A {
public:
int _a;
};
// class B : public A
class B : public A {
public:
int _b;
};
// class C : public A
class C : public A {
public:
int _c;
};
class D : public B, public C {
public:
int _d;
};
void Test()
{
D d;
cout << sizeof(d) << "字节" << endl;
d.B::_a = 1;
d.C::_a = 2;
d._b = 3;
d._c = 4;
d._d = 5;
}
int main()
{
Test();
return 0;
}
下图是菱形继承的内存对象成员模型:这里可以看到数据冗余
下图是菱形虚拟继承的内存对象成员模型