c++-继承的学习
继承的基本概念
继承和派生
- 继承概念
- 派生类的访问控制(继承三种方式、类三种访问控制、三看原则)综合训练
- 继承中的构造和析构
- 类型兼容性原则
- 继承中的构造和析构
- 继承中同名成员函数、成员变量处理方法
- 继承中的static关键字
继承概念
派生类的访问控制(继承三种方式、类三种访问控制、三看原则)综合训练
继承中的构造和析构
- 类型兼容性原则
- 继承中的构造和析构
- 继承中同名成员函数、成员变量处理方法
- 继承中的static关键字
多继承概念
多继承
- 多继承概念
- 二义性
- 虚继承基本实验原理
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student()
{
}
Student(int id, string name)
{
this->id = id;
this->name = name;
}
void printS() {
cout << "id = " << this->id << ", name = " << this->name << endl;
}
int id;
string name;
};
//创建一个新的学生类,增加score、功能
class Student2
{
public:
Student2(int id, string name, int score)
{
this->id = id;
this->name = name;
this->score = score;
}
void printS() {
cout << "id = " << this->id << ", name = " << this->name << endl;
cout << "score = " << this->score << endl;
}
private:
int id;
string name;
//add
int score;
};
//通过继承创建一个新的学生类
class Student3 :public Student
{
public:
Student3(int id, string name, int score) :Student(id, name)
{
this->score = score;
}
void printS() {
Student::printS();
cout << "score = " << this->score << endl;
}
private:
int score;
};
int main(void)
{
Student3 s3(1, "zhang3", 80);
s3.printS();
return 0;
}
继承的方式
规则1, 只要是父类中的private成员,不管是什么继承方式,儿子都访问不了
规则2, 如果是公有(public)继承, 儿子中的访问控制权限保持不变。
规则3, 如果是保护(protected)继承, 儿子中父亲中除了private成员,其余在儿子中都是protected
规则4, 如果是私有(private)继承, 儿子中的父亲的除了private成员,其余在二中都是private成员。
三看原则:
- 看调用的成员变量是在类的内部还是类的外部
- 看儿子继承方式,
- 当前变量在儿子中的变量在父亲中的访问控制权限
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <memory>
using namespace std;
class Parent
{
public:
int pub; //在类的内部 和 外部 都能访问。
protected:
int pro; //在类的内部可以访问, 在类的外部不可以访问
private:
int pri; //在类的内部可以访问, 在类的外部不可以访问
};
//公有继承
class Child :public Parent
{
public:
void func()
{
cout << pub << endl; //pub父类的public成员变量,在public继承 类的 【内部 外部】可以访问。
cout << pro << endl;//pro 是父类protected成员变量 在public继承类的 【内部】可以访问。外部访问不了
//此时的pro在孙子能够访问,说此时pro不是private成员,而是protected成员
//cout << pri << endl; //pri 是父类private成员变量 在public继承类的 【内部,外部】[不]可以访问。
}
};
//孙子类
class SubChild : public Child
{
void sub_func()
{
cout << pro << endl;
}
};
//保护继承
class Child2 :protected Parent
{
public:
void func2() {
pub;//此时pub通过protected继承 能够在类的内部访问。
//pub 在类的内部可以访问, 类的外部访问不了, 类的儿子可以访问
//pub 就是protected成员
pro;//pro 根pub 是一样的性质,pro也是protected成员
//pri;
}
};
class Sub_child2:public Child2
{
public:
void sub_func2() {
pub;
pro;
}
};
//私有继承
class Child3 :private Parent
{
public:
void func3()
{
pub;//pub 在类的内部可以访问。在类的内部可以访问,类的外部不能访问。
//pub 在儿子中访问不了,说明pub在Child3中是 私有成员
pro;//pro 根pub的性质是一样, 也是私有成员。
pri;
}
};
class Sub_Child3 :public Child3
{
public:
void sub_fun3()
{
pub;
pro;
}
};
int main(void)
{
Child c1;
c1.func();
c1.pub;
//c1.pri;
//Child2 c2;
//c2.pub;
//c2.pro;
Child3 c3;
//c3.pub;
//c3.pro;
Child2 c2;
c2.pub;
c2.pro;
c1.pub;
return 0;
}
继承方式的练习
#include <iostream>
using namespace std;
class A
{
private:
int a;
protected:
int b;
public:
int c;
A()
{
a = 0;
b = 0;
c = 0;
}
void set(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
};
class B : public A
{
public:
void print()
{
//cout << "a = " << a; //a是父类的私有成员访问不了
cout << "b = " << b; //b 此时是保护成员,类的内部可以访问
cout << "c = "<<c << endl; //c 此时是公有成员,类的内部可以访问
}
};
class C : protected A
{
public:
void print()
{
//cout << "a = " << a; //a是父类的私有成员访问不了
cout << "b = " << b; //b 在子类中是protected权限,类的内部可以访问。
cout << "c = " <<c << endl; //c 子类的protected成员,类的内部可以访问。
}
};
class D : private A
{
public:
void print()
{
//cout << "a = " << a; //a是父类的私有成员访问不了
cout << "b = " << b << endl; //b 此时是private成员,类的内部可以访问。
cout << "c = " << c << endl; //c 此时是private成员,类的内部可以访问。
}
};
int main(void)
{
A aa;
B bb;
C cc;
D dd;
aa.c = 100; //c 是公有 ,类的外部可以访问。
bb.c = 100; //Bpublic 继承与A ,保持权限不变,c 是公有, 类的外部可以访问
//cc.c = 100; //C protected 继承与A, c 在此类中是protected成员,类的外部不能访问。
//dd.c = 100; //D private 继承与A, c在此类中private成员,类的外部不能访问。
aa.set(1, 2, 3); //能否访问???
bb.set(10, 20, 30); //能否访问???
//cc.set(40, 50, 60); //能否访问???
//dd.set(70, 80, 90); //能否访问???
bb.print(); //print 是定义在B类 public成员函数,在类的外部可以访问。
cc.print(); //print 是定义在C类 public成员函数,在类的外部可以访问。
dd.print(); //print 是定义在D类 public成员函数,在类的外部可以访问。
return 0;
}
类的兼容性复制原则
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
/*
子类对象可以当作父类对象使用。
子类对象可以直接赋值给父类对象。
子类对象可以直接初始化父类对象.
****父类指针可以直接指向子类对象***
父类引用可以直接引用子类对象
*/
class Parent
{
public:
void printP() {
cout << "a " << this->a << endl;
}
int a=333;
};
class Child :public Parent
{
public:
void printC()
{
cout << "b = " << this->b << endl;
}
int b;
};
void myPrint(Parent *pp)
{
pp->printP();
}
int main(void)
{
// Parent p;
// Child c = p; //p对象填充不满c对象空间,
// Child c;
// Parent p = c;//c 对象所占用的内存空间 >= p对象占用空间 能够填充满p对象所需要空间。
// p = c;
// c.printP(); //c 能够当做父类 p 来使用。
Parent *pp = NULL;//父类指针
Child *cp = NULL;//子类指针
Parent p;//父类对象
Child c; //子类对象
pp = &c;//c 内存布局能够满足父类指针的全部需求, 可以用一个儿子的对象地址给父类指针赋值。
myPrint(&p);
myPrint(&c);
return 0;
}
子类的构造和析构
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Parent
{
public:
Parent()
{
cout << "Parent().." << endl;
a = 0;
}
Parent(int a) {
cout << "Parent(int)..." << endl;
this->a = a;
}
~Parent(){
cout << "~Parent" << endl;
}
int a;
};
class Child :public Parent
{
public:
//在调用子类的构造函数时候,一定会调用父类的构造函数
// 父类先构造,子类后构造。
Child(int a, int b) :Parent(a)
{
cout << "Child(int, int)..." << endl;
this->b = b;
}
void printC() {
cout << "b = " << b << endl;
}
~Child(){
cout << "~Child()..." << endl;
}
int b;
};
int main(void)
{
Child c(10, 20);
c.printC();
return 0;
}
子类与父类的变量名相同
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Parent
{
public:
Parent(int a) {
this->a = a;
}
int a;
};
class Child :public Parent
{
public:
Child(int p_a, int c_a) :Parent(p_a)
{
this->a = c_a;
}
void print()
{
cout << Parent::a << endl;
cout << this->a << endl;//child's a
}
int a;
};
int main(void)
{
Child c(10, 100);
c.print();
return 0;
}
继承中的static
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class A
{
public:
static int a;
private:
};
class B :public A
{
public:
private:
};
int A::a = 100;//静态成员变量 初始化
int main(void)
{
A a1;
A a2;
cout << a1.a << endl;//100
cout << a2.a << endl;//100
A::a = 300;
cout << a1.a << endl;//300
cout << a2.a << endl;//300
B b1;
B b2;
A::a = 400;
cout << "------" << endl;
cout << b1.a << endl;//400
cout << b2.a << endl;//400
cout << a1.a << endl;//400
cout << a2.a << endl;//400
return 0;
}
多继承
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//家具类
class Furniture
{
public:
int m; //材质
};
//将父亲类继承爷爷类 改成虚继承, 防止儿子在多继承我的时候,出现爷爷中的变量会拷贝多份。
class Bed:virtual public Furniture
{
public:
void sleep() {
cout << "在床上睡觉" << endl;
}
};
class Sofa:virtual public Furniture
{
public:
void sit() {
cout << "在沙发上休息" << endl;
}
};
//沙发床
class SofaBed :public Bed, public Sofa
{
public:
void SleepAndSit() {
sleep();
sit();
}
};
int main(void)
{
Bed b;
b.sleep();
Sofa s;
s.sit();
cout << " ------ " << endl;
SofaBed sb;
sb.SleepAndSit();
sb.m = 100;//此时只有一个m
// sb.Bed::m = 100;
// sb.Sofa::m = 200;
return 0;
}
c++-继承的学习的更多相关文章
- js类式继承模式学习心得
最近在学习<JavaScript模式>,感觉里面的5种继承模式写的很好,值得和大家分享. 类式继承模式#1--原型继承 方法 让子函数的原型来继承父函数实例出来的对象 <script ...
- javascript继承之学习笔记
今天记录一下学习javascript的继承. 继承基本上是基于“类”来说的,而javascript中并不存在真正的类,所以就出现了各种模拟“类”的行为,然后就堂而皇之的使用起了类的概念.这里不谈“类” ...
- coffeeScript中类的继承[学习篇]
只是在看深入浅出coffeescript中感觉真的很好,不光是coffe写法简单,生成的js也值得学习,废话不多说了,直接抄个书上的例子 class Pet constructor: -> @i ...
- JavaScript 对象 之继承对象 学习笔记
假设,我们有个这样的需求: 两个种族,每个种族都有 名字.血量(默认200).行为(行为有 跳跃.移动速度 这些属性)等共有属性. 人族能量值比兽人多10%,兽人血量比人族多10%. 职业有战士和法师 ...
- ES6 class的继承-学习笔记
1.简介 Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链实现继承,要清晰和方便很多. 子类必须在constructor方法中调用super方法,否则新建实例时会报错. ...
- 继承ViewGroup学习onMeasure()和onLayout()方法
在继承ViewGroup类时,需要重写两个方法,分别是onMeasure和onLayout. 1,在方法onMeasure中调用setMeasuredDimension方法void android.v ...
- 8-C++远征之继承篇-学习笔记
C++远征之继承篇 开篇介绍 整个C++远征计划: 起航->离港->封装->继承 为什么要用继承? 为什么要有继承? 如何来定义基类 <----> 派生类? 基类到派生类 ...
- java中继承关系学习小结
继承:把多个类中同样的内容提取出来.定义到一个类中,其它类仅仅须要继承该类.就能够使用该类公开的属性和公开的方法. 继承的优点:提高代码的复用性.提高代码的可维护性.让类与类之间产生关系,是多态存 ...
- Java 多态 接口继承等学习笔记
Super关键字 1.子类可以调用父类声明的构造方法 : 语法:在子类的构造方法中使用super关键字 super(参数列表) 2.操作被隐藏的成员变量(子类的成员变量和父类的成员变量重名的说法)和 ...
随机推荐
- 2019-9-11:渗透测试,基础学习,vim编辑器,笔记
Linux快捷路径符号说明. 代表当前目录.. 上级目录- 代表前一个工作目录~ 表示当前用户的家目录 vmware tools 用来虚拟机和宿主机之间移动数据 vim/vi编辑器vim编辑器三种模式 ...
- 【2018寒假集训Day 7】【最短路径】三种算法的模板
Luogu单源最短路径模版题 dijkstra #include<cstdio> #include<vector> using namespace std; const int ...
- SpringBoot第一次案例
一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...
- kubeadm join 超时报错 error execution phase kubelet-start: error uploading crisocket: timed out waiting for the condition
解决: swapoff -a kubeadm reset systemctl daemon-reload systemctl restart kubelet iptables -F && ...
- 使用 colgroup 和 col 实现响应式表格
Table 在项目使用中十分频繁,特别是在后台管理系统中,table 无疑是数据展示的第一公民,在早些年的网页中,table 也是网页布局的第一选择,然后使用好 table 并不容易,其它有很多子元素 ...
- Spring面试题集锦(精选)
以下来自网络收集,找不到原文出处.此次主要为了面试收集,希望对大家有所帮助~~~~ 1.什么是Spring? Spring是一个开源的Java EE开发框架.Spring框架的核心功能可以应用在任何J ...
- 鲲鹏性能优化十板斧——鲲鹏处理器NUMA简介与性能调优五步法
TaiShan特战队六月底成立,至今百日有余,恰逢1024程序员节,遂整理此文,献礼致敬!希望能为广大在鲲鹏处理器上开发软件.性能调优的程序员们,提供一点帮助.从今天开始,将陆续推出性能调优专题文章. ...
- RDS关系型数据库 入门 01 创建关系型数据库实例【华为云分享】
[摘要] 关系型数据库(Relational Database Service,简称RDS)是一种基于云计算平台的即开即用.稳定可靠.弹性伸缩.便捷管理的在线关系型数据库服务.RDS具有完善的性能监控 ...
- std::unique_ptr的用法
std::ofstream("demo.txt") << 'x'; // 准备要读的文件 { std::unique_ptr<std::FILE, decltyp ...
- 手撕 JVM 垃圾收集日志
下图是本篇的写作大纲,将从以下四个方面介绍怎么样处理 JVM 日志. 有准备才能不慌 想要分析日志,首先你得有日志呀,对不对.凡是未雨绸蒙总是没错的.所谓有日志的意思,你要把 JVM 参数配置好,日志 ...