学习C++对于初学者的第一挑战是前面的类和对象(封装),而第二挑战则是继承与多态:
目录
封装
1数据和方法放到一起,把想给访问的定义成共有,不想给你访问的定义成私有和保护
2一个类型放到另一个类型中,通过typedef成员函数调整,封装一个全新的类型:如迭代器:
而对于封装的具体学习在:类和对象中进行体现,这里就不详细叙述了,有兴趣就跳转我写的类和对象:http://t.csdnimg.cn/GpVAt
继承
如果我们想创建以下几个类:
它们的成员变量都要有:name,age,address;如果按照通常的思路:要创建出四个类,每个类中写上一遍相同的成员变量;这不免会有些冗余:那有什么更好的方法吗?
1概念
而上面我们所说的解决方法用代码可以写成:
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; // 学号
};
class Teacher : public Person
{
protected:
int _jobid; // 工号
};
int main()
{
Student s;
Teacher t;
s.Print();
t.Print();
return 0;
}
下面我们看到Person是父类,也称作基类。Student是子类,也称作派生类:
2继承方式与访问限定符
3访问权限
父类的成员,继承到派生类以后,访问权限怎么规定?
总结:
4基类与派生类的赋值
class Person
{
protected:
string _name; // 姓名
string _sex; // 性别
int _age; // 年龄
};
class Student : public Person
{
public:
int _No; // 学号
};
int main()
{
Student st;
// 1.子类对象可以赋值给父类对象/指针/引用
Person p = st;
Person& ref = st;
Person* ptf= &st;
return 0;
}
对应关系如下:
父类对象能用指针指向子类当中父类继承的那部分,那我应该能对子类对象的继承成员进行修改:
5继承中的作用域
// B中的fun和A中的fun构成隐藏,成员函数满足函数名相同就构成隐藏。
class A
{
public:
void fun()
{
cout << "func()" << endl;
}
};
class B : public A
{
public:
void fun(int i)
{
//A::fun();显示调用A的fun()
cout << "func(int i)->" << i << endl;
}
};
int main()
{
B b;
b.fun(10);
return 0;
};
6派生类的默认成员函数
先来回忆回忆六大成员函数:
派生类中的成员函数中会有哪些变化呢?
class Person
{
public:
Person(const char* name)
: _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; // 姓名
};
class Student : public Person
{
public:
// 父类+自己,父类的调用父类构造函数初始化(复用)
Student(int num, const char* str, const char* name)
:Person(name)//要显示构造
, _num(num)
, _str(str)
{
cout << "Student()" << endl;
}
// s2(s1)
Student(const Student& s)
:Person(s)
, _num(s._num)
, _str(s._str)
{}
Student& operator=(const Student& s)
{
if (this != &s)
{
Person::operator=(s);//显示写
_num = s._num;
_str = s._str;
}
return *this;
}
// 子类的析构也会隐藏父类
// 因为后续多态的需要,析构函数名字会被统一处理成destructor
~Student()
{
// 显示写无法先子后父
//Person::~Person();
cout << _name << endl;
cout << "~Student()" << endl;
// 注意,为了析构顺序是先子后父,子类析构函数结束后会自动调用父类析构
}
protected:
int _num; //学号
string _str;
// 父类成员,当成一个整体的一个自定义类型成员
// 子类的成员(跟以前一样)
// a、内置类型
// b、自定义类型
};
//与BB对象进行类比理解
class BB
{
public:
BB(int num, const char* str, const char* name)
:_p(name)
,_num(num)
,_str(str)
{}
private:
Person _p;
int _num;
string _str;
};
int main()
{
//类比构造
//BB bb(1, "xxxx", "张三");
//构造
Student s1(1, "xxxx","张三");
//拷贝
Student s2(s1);
//赋值
Student s3(2, "yyy", "李四");
s1 = s3;
//析构
Person p("李四");
p.~Person();
return 0;
}
6.1构造函数
6.2拷贝函数
6.3赋值重载/析构函数
总结:
7继承与友元
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;//不可访问
}
int main()
{
Person p;
Student s;
Display(p, s);
}
8继承与静态成员
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; // 研究科目
};
int main()
{
Student s1;
Student s2;
Student s3;
Graduate s4;
cout << "人数:" << Person::_count << endl;//几个子类就是几个count
Student::_count = 0;
cout << "人数:" << Person::_count << endl;
}
9菱形继承与菱形虚拟继承
C++对复用的方式体现在:1函数逻辑的复用;2模板;3继承
那继承有没有可能会出现继承多个父类来达到double继承?? 有的:多继承~
而我们在上面介绍的继承方式全都是单继承:毕竟比较常用~
9.1单继承
9.2多继承
比如:西红柿既可以是水果,有可以是蔬菜
9.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; // 主修课程
};
int main()
{
Assistant a;
a._name = "peter";//Teacher和stduent都有变量name
// 需要显示指定访问哪个父类的成员可以解决二义性问题,但是数据冗余问题无法解决
a.Student::_name = "xxx";
a.Teacher::_name = "yyy";
}
9.4菱形虚拟继承
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; // 主修课程
};
int main()
{
Assistant a;
a._name = "peter";
cout << a._name << endl;
return 0;
}
9.5分析菱形虚拟继承模型
class A
{
public:
int _a;
};
//class B : public A
class B : virtual public A
{
public:
int _b;
int _b1;
int _b2;
};
//class C : public A
class C : virtual public A
{
public:
int _c;
};
class D : public C, public B
{
public:
int _d;
};
int main()
{
D d;
d.B::_a = 1;
d.C::_a = 2;
d._a = 0;
d._b = 3;
d._c = 4;
d._d = 5;
return 0;
}
在STL库中也有对菱形虚拟继承的应用:
更复杂的菱形虚拟继承还有:
10继承的总结与反思
// Car和BMW Car和Benz构成is-a的关系
class Car {
protected:
string _colour = "白色"; // 颜色
};
class BMW : public Car {
public:
void Drive() { cout << "好开-操控" << endl; }
};
class Benz : public Car {
public:
void Drive() { cout << "好坐-舒适" << endl; }
};
// Tire和Car构成has-a的关系
class Tire {
protected:
string _brand = "Michelin"; // 品牌
size_t _size = 17; // 尺寸
};
class Car {
protected:
string _colour = "白色"; // 颜色
string _num = "陕ABIT00"; // 车牌号
Tire _t; // 轮胎
};
以上便是继承相关的知识:有错误欢迎在评论区指正,谢谢!!