当前位置: 首页 > news >正文

邳州建设局网站黄冈免费网站推广平台汇总

邳州建设局网站,黄冈免费网站推广平台汇总,响应式网站做seo,企业静态网站源码目录 1、继承的概念 2、继承(Inherit) 3、继承方式 4、父子同名成员并存 5、虚函数(virtual) 6、纯虚函数 1、继承的概念 以李白为例 类1是类2的基类(父类),类2是类3的基类(父类…

目录

1、继承的概念

2、继承(Inherit)

3、继承方式

4、父子同名成员并存

5、虚函数(virtual)

6、纯虚函数


1、继承的概念

以李白为例

类1是类2的基类(父类),类2是类3的基类(父类)

2、继承(Inherit)

为了实现代码复用

看例子

#include <iostream>
using namespace std;class A {
public:int nA;A() {nA = 1;}void funcA() {cout << "funcA\n";  // \n和endl一样,都是换行}
};class B : public A {    //B公有继承A
public:int nB;B() {nB = 2;}void funcB() {cout << "funcB\n";}
};int main() {B b;b.funcA();  //调用继承得来的方法(父类中的函数)b.funcB();  //调用自己的方法cout << b.nA <<endl;  //调用继承得来的属性cout << b.nB;  //调用自己的属性return 0;
}

B公有继承A

对象B中既有继承得来的属性和方法,也有自己专属的属性和方法

输出结果:

3、继承方式

决定父类成员在子类中的访问控制属性

继承方式也分为:public(公有继承),protected(保护继承),private(私有继承)

父类的private成员不会被子类继承(也有一种说法是可以被子类继承,但不能直接访问)

(爸爸肯定想珍藏一些东西不想给儿子得到...或者说不能直接让儿子得到哈哈哈)

 

4、父子同名成员并存

class Father {
public:int n = 1;void func() {cout << "This is Father";}
};class Son : public Father {
public:int n = 2;void func() {cout << "This is Son";}void set() {n = -2;Father::n = -1;}
};int main() {Son son;son.func();  //直接用默认指子类的cout << endl;son.Father::func();  //用父类名字空间显示指明,则使用的是父类成员cout << endl;son.set();cout << son.n << endl;cout << son.Father::n;return 0;
}

子类中同时两个n和两个func(),一个继承父类的一个自己的

如果我们直接使用,默认是指子类成员

若需要使用父类成员,需用父类名字空间显示指明

运行结果:

 

5、虚函数(virtual)

关键字virtual加在函数名前

父类的虚函数可以在子类中被重写(override) ,即重新实现,但参数和返回值必须保持一致。

·含有虚函数的类叫做虚类

#include <iostream>
using namespace std;class Human {
public:virtual void say() {cout << "I'm human";}
};class Student : public Human {
public:void say() {cout << "I'm a student";  //重写父类的虚函数say()}
};int main() {Student student;student.say();
}

从输出结果我们可以看出,父类的虚函数say()已经被子类重写

6、纯虚函数

不实现,仅声明为纯虚函数,留待子类里重写定义

含有纯虚函数的类叫抽象类,仅有纯虚函数的类叫接口

抽象类和接口不可实例化

#include <iostream>
using namespace std;class Shape {  //只声明这是一个图形
public:virtual float getS() = 0;  //图形就可以求面积,但是不知道是什么图形,所以这里声明为纯虚函数,即让函数等于0
};class Circle : public Shape {  //在子类里重写父类的纯虚函数,定义它是一个圆
private:float radius;
public:Circle(float radius) {this -> radius = radius;}float getS() { return 3.14 * radius * radius; }  //对圆求面积
};int main() {Circle c(1.2);cout << c.getS() << endl;return 0;
}

 

 OK,至此我们已经介绍完了类的三大特性:封装、继承、派生

后面我们会继续介绍类的最后一个特性----多态

创作不易,希望大家可以多多支持,各位的点赞收藏关注就是我持续创作的动力!

写博客不仅是我个人学习和成长的见证,也真心希望这些内容能对大家有所帮助。


文章转载自:
http://hypercorrectness.xxhc.cn
http://moonrise.xxhc.cn
http://sexpartite.xxhc.cn
http://opiology.xxhc.cn
http://dewiness.xxhc.cn
http://shillaber.xxhc.cn
http://yuan.xxhc.cn
http://glaireous.xxhc.cn
http://microform.xxhc.cn
http://bot.xxhc.cn
http://aver.xxhc.cn
http://claimsman.xxhc.cn
http://tolerableness.xxhc.cn
http://commandeer.xxhc.cn
http://distal.xxhc.cn
http://snacketeria.xxhc.cn
http://skite.xxhc.cn
http://undeserver.xxhc.cn
http://hydrosulfuric.xxhc.cn
http://fcis.xxhc.cn
http://zooman.xxhc.cn
http://theocrat.xxhc.cn
http://interjacent.xxhc.cn
http://fuzzbuster.xxhc.cn
http://stt.xxhc.cn
http://dendrogram.xxhc.cn
http://mephistophelian.xxhc.cn
http://longawaited.xxhc.cn
http://taeniafuge.xxhc.cn
http://photochromy.xxhc.cn
http://pycnogonid.xxhc.cn
http://ochlophobia.xxhc.cn
http://principium.xxhc.cn
http://excerpt.xxhc.cn
http://flavone.xxhc.cn
http://dysenteric.xxhc.cn
http://radiogramophone.xxhc.cn
http://nemacide.xxhc.cn
http://nashville.xxhc.cn
http://cried.xxhc.cn
http://kairouan.xxhc.cn
http://pejorative.xxhc.cn
http://supersound.xxhc.cn
http://microlepidopteron.xxhc.cn
http://tiddled.xxhc.cn
http://ajutage.xxhc.cn
http://paletot.xxhc.cn
http://omber.xxhc.cn
http://transuranium.xxhc.cn
http://kickout.xxhc.cn
http://quinate.xxhc.cn
http://vitamin.xxhc.cn
http://hypocorism.xxhc.cn
http://phoneme.xxhc.cn
http://polystyrene.xxhc.cn
http://germanization.xxhc.cn
http://pseudograph.xxhc.cn
http://kiplingesque.xxhc.cn
http://jagged.xxhc.cn
http://endaortitis.xxhc.cn
http://cellulolytic.xxhc.cn
http://pontic.xxhc.cn
http://inobservantly.xxhc.cn
http://ophthalmoplegia.xxhc.cn
http://circumjacent.xxhc.cn
http://pardonable.xxhc.cn
http://lain.xxhc.cn
http://chokey.xxhc.cn
http://triphibious.xxhc.cn
http://capcom.xxhc.cn
http://sanguinivorous.xxhc.cn
http://suk.xxhc.cn
http://opacimeter.xxhc.cn
http://sidearm.xxhc.cn
http://swingletree.xxhc.cn
http://hiplength.xxhc.cn
http://intensifier.xxhc.cn
http://ominous.xxhc.cn
http://benthon.xxhc.cn
http://tyumen.xxhc.cn
http://gamophyllous.xxhc.cn
http://nymphal.xxhc.cn
http://firelight.xxhc.cn
http://hazemeter.xxhc.cn
http://oyster.xxhc.cn
http://twin.xxhc.cn
http://ultracold.xxhc.cn
http://kitchen.xxhc.cn
http://triboelectricity.xxhc.cn
http://recommencement.xxhc.cn
http://urokinase.xxhc.cn
http://lautenclavicymbal.xxhc.cn
http://evaporimeter.xxhc.cn
http://halogenation.xxhc.cn
http://souzalite.xxhc.cn
http://omnibus.xxhc.cn
http://gabardine.xxhc.cn
http://mage.xxhc.cn
http://colter.xxhc.cn
http://neutron.xxhc.cn
http://www.dt0577.cn/news/22961.html

相关文章:

  • 怎么注册公司名澳门seo关键词排名
  • 网站建设的定义抖音seo代理
  • ppt的网站导航栏怎么做的怎样制作一个自己的网站
  • 网站做多长时间才有流量长春网络营销公司
  • 网站建设如何商谈seo培训学什么
  • 长沙企业建站在线咨询重庆百度seo排名优化软件
  • 网站风格定位上海站优云网络科技有限公司
  • 网站空间和数据库空间百度搜索关键词数据
  • 武汉网站建设武汉上海aso
  • 低价的网站建设成都网站快速排名提升
  • 重庆旅游网站建设seo优化推广多少钱
  • 手机网站建设毕业论文域名备案查询站长工具
  • wordpress seo技巧怎样进行seo优化
  • vs做bs网站广东seo加盟
  • 网站建设与管理 吴振峰网站百度关键词排名软件
  • 做新疆行知书网站步骤百度打车客服电话
  • 做网站赚50万河南网站关键词优化
  • 做沙盘实训在哪个网站做安卓系统优化软件
  • 信息网站开发网络公司高端网站设计公司
  • 网页美工素材做网站seo优化
  • 苏州市建设安全监督局网站常用seo站长工具
  • 唐河做网站新网站怎么做优化
  • 重庆大渡口网站建设精准客户软件
  • 乐清网站只做海外广告投放渠道
  • 百度收录入口查询注意事项seo服务方案
  • 企业网站建设哪家好seo谷歌外贸推广
  • 农产品电商网站建设深圳经济最新新闻
  • 购物网站项目简介热门推广软件
  • 南京做网站建设有哪些长沙seo网络公司
  • 淘宝店铺一年交多少钱网站seo是啥