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.操作被隐藏的成员变量(子类的成员变量和父类的成员变量重名的说法)和 ...
随机推荐
- SQL语句实用技巧1
--显示行号 select *, ROW_NUMBER() OVER(Order by TYPENAME ) AS RowNumber from ( select distinct TYPENAME ...
- CentOS6下安装zabbix3.4
安装环境 系统 Centos6.7,最小化安装 CPU 2核 内存 2G 硬盘 200G IP 192.168.100.168/24 主机名 zabbix Zabbix版本 3.4 数据库 mysql ...
- CentOS 7上利用systemctl添加自定义系统服务
Centos 7 之 systemctl CentOS 7继承了RHEL 7的新的特性,例如强大的systemctl,而systemctl的使用也使得以往系统服务的/etc/init.d的启动脚本的方 ...
- centos 7 MysSQL 5.6.39 源码安装
MySQL 5.6.39 二进制安装 CentOS 7 将默认数据库MySQL替换成了Mariadb. 这里会从系统的环境准备开始一步一步安装. 环境准备 系统版本 内核版本 IP地址 Centos ...
- 【github repo自荐】码农周刊一周精选分类
以下内容节选自我的github码农周刊整理repo,欢迎大家star. 写在最前面的话 作为最初的一批码农周刊的订阅者,不能说经历了其成长,但是确实见证了他的壮大.码农周刊确实从开始第一期的基本上都是 ...
- num += num 与 num = num+ num
a = 100def test(num): num += num print(num) test(a)print(a) 200100 这里 num += num 与 num = num+ num 不能 ...
- 重新调用 layoutSubview
重新调用 layoutSubview
- Flink应用程序结构开发介绍
Flink程序遵循一定的编程模式.DataStream API 和 DataSet API 基本具有相同的程序结构.以下为一个流式程序的示例代码来对文本文件进行词频统计. package com.re ...
- gitbook 入门教程之增强版 edit-link-plus 编辑此页插件
Gitbook plugin add "edit this page" link, and intelligent recognition to Github or Gitlab ...
- sublime text3安装中文版插件
安装插件前需要先安装 package control 扩展包管理器,安装方法为:进入 https://sublime.wbond.net/Package%20Control.sublime-packa ...