继承的基本语法
语法:class子类:继承方式 父类
父亲:基类
子类:派生类
继承的作用:可以将少重复代码的出现
继承方式
公共继承
父类中 public到子类中 public
父类中protected 到子类中 protectded
父类中private到子类中 访问不到
保护继承
父类中 public到子类中 protectded
父类中protected 到子类中 protectded
父类中private到子类中 访问不到
私有继承
父类中 public到子类中 private
父类中protected 到子类中 private
父类中private到子类中 访问不到
#include<iostream>
using namespace std;
class Base
{
public:
int m_a;
protected:
int m_b;
private:
int m_c;
};
// 公共继承
class Son1 :public Base1
{
public:
void f()
{
m_a = 100; // m_a在Son1中是public权限
m_b = 100; // m_b在Son1中是protected;
m_c = 100; // 父类中的私有属性子类不可访问(不论什么继承方式)
}
};
void test01()
{
Son1 s;
s.m_a = 100; // m_a在Son1中的权限是public 类外是可以访问的
}
// 保护继承
class Son2:protected Base1
{
public:
void f()
{
m_a = 100; // m_a在Son2中是protected;
m_b = 100; // m_b在Son2中是protected;
m_c = 100; // 父类中的私有属性子类不可访问(不论什么继承方式)
}
};
void test02()
{
Son2 s;
s.m_a = 100; // m_a在Son2中的权限是protected 类外不可以访问
}
// 私有继承
class Son3 :private Base
{
public:
void f()
{
m_a = 100;
m_b = 100;
m_c = 100; // 父类中的私有属性子类不可访问(不论什么继承方式)
}
};
void test03()
{
Son3 s;
s.m_a = 100; // m_a在Son2中的权限是protected 类外不可以访问
}
class GrandSon :public Son3
{
public:
void f()
{
m_a = 100; // m_a在Son3中已经变为私有属性,因此访问不到
m_b = 100;
m_c = 100;
}
};
int main()
{
test01();
system("pause");
return 0;
}
继承中的对象模型
**父类中私有属性 子类也是继承下去了,但被编译器隐藏,因此访问不到 **
#include<iostream>
using namespace std;
class Base
{
public:
int m_a;
protected:
int m_b;
private:
int m_c;
};
class Son :public Base
{
public:
// 父类中私有属性 子类也是继承下去了 cl/d1 reportSingleClassLayoutSon 文件名 但被编译器隐藏,因此访问不到
int m_d;
};
// C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\VC
void test01()
{
cout << sizeof(Son) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
继承中的构造和析构
#include<iostream>
using namespace std;
/// 在继承中 先调用父类的构造 再调用子类的构造,析构顺序与构造顺序相反
class Base
{
public:
Base()
{
cout << "Base的构造函数调用" << endl;
}
~Base()
{
cout << "Base的析构函数调用" << endl;
}
};
class Son :public Base
{
public:
Son()
{
cout << "Son的构造函数调用" << endl;
}
~Son()
{
cout << "Son的析构函数调用" << endl;
}
};
class Base2
{
public:
Base2(int a){}
};
// 继承
class Son2:public Base2
{
public:
// 初始化列表
// 可以利用初始化列表的方法 显示指定出调用父类中的哪个构造函数
Son2(int a = 1000):Base2(a) // 函数默认参数 初始化列表 继承
{
}
};
//子类不会继承父亲的构造函数和析构函数,只有父类才知道如何构造和析构自己的属性
void test01()
{
// Base b;
Son s;
}
int main()
{
test01();
system("pause");
return 0;
}
继承中的同名成员处理
#include<iostream>
using namespace std;
class Base
{
public:
Base()
{
this->m_a = 100;
}
void f()
{
cout << "Base中的f调用" << endl;
}
void f(int a)
{
cout << "Base中的f(int a)调用" << endl;
}
int m_a;
};
class Son :public Base
{
public:
Son()
{
this->m_a = 200;
}
void f()
{
cout << "Son中的f调用" << endl;
}
int m_a;
};
void test01()
{
Son s;
cout << s.m_a << endl;
cout << "父类中的m_a = " << s.Base::m_a << endl;
// 同名的成员函数,子类会隐藏掉父类中的所有版本
s.f();
// s.f(10);
s.Base::f(10);
s.Base::f();
}
int main()
{
test01();
system("pause");
return 0;
}
继承中的静态成员处理
#include<iostream>
using namespace std;
class Base
{
public:
static void f()
{
cout << "Base中的f调用" << endl;
}
static void f(int a)
{
cout << "Base中的f(int a)调用" << endl;
}
static int m_a; // 共享数据,编译期间分配内存,类内声明,类外初始化
};
int Base::m_a = 100;
class Son:public Base
{
public:
static void f()
{
cout << "Son中的f调用" << endl;
}
static int m_a;
};
int Son::m_a = 20;
void test01()
{
Son s;
// 通过对象访问
cout << s.m_a << endl;
cout << s.Base::m_a << endl;
// 通过类名访问
cout << Son::m_a << endl;
cout << Son::Base::m_a << endl;
s.f();
s.Base::f();
Son::f();
Son::Base::f();
Son::Base::f(1);
}
int main()
{
test01();
system("pause");
return 0;
}