2.2孙鑫C++
1、继承
动物有 吃 睡 呼吸的方法 当然 鱼也有 不用重复再定义
1)public 那里都可以访问
#include <iostream.h>
class Animal //类 基类
{
public:
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
void breathe()//呼吸方法
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// //子类
{ }; void main() {
Animal an;
an.eat();
Fish fh;
fh.sleep();
}
2)protected 只有子类能访问 外部其他的都不能被访问
#include <iostream.h>
class Animal //类 基类
{
public:
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
void breathe()//呼吸方法
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// //子类
{
void test()
{
sleep();
breathe();
} }; void main() {
Animal an;
an.eat();
Fish fh;
fh.sleep();
}
3)private 只有自己能访问 ,子类 外部都不能访问
#include <iostream.h>
class Animal //类 基类
{
public:
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
private:
void breathe()//呼吸方法
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// //子类
{
void test()
{
sleep();
breathe();
} }; void main() {
Animal an;
an.eat();
Fish fh;
// fh.sleep();
}
4)访问特性
最多为public继承
2、继承的先后性
没有父亲 就没有孩子
1)基类 子类 构造函数的调用顺序
动物先
#include <iostream.h>
class Animal //类 基类
{
public: Animal()
{
cout<<"Animal construct "<<endl;
}
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
//protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
//private:
void breathe()//呼吸方法
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// 继承特性 //子类
{
public:
Fish()
{
cout<<"Fish construct "<<endl;
} }; void main() {
// Animal an;
// an.eat();
Fish fh;
// fh.sleep();
}
2)基类 子类 析构函数的调用顺序
鱼先
#include <iostream.h>
class Animal //类 基类
{
public: Animal()
{
cout<<"Animal construct "<<endl;
}
~Animal()
{
cout<<"Animal deconstruct "<<endl;
}
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
//protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
//private:
void breathe()//呼吸方法
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// 继承特性 //子类
{
public:
Fish()
{
cout<<"Fish construct "<<endl;
}
~Fish()
{
cout<<"Fish deconstruct "<<endl;
}
}; void main() {
// Animal an;
// an.eat();
Fish fh;
// fh.sleep();
}
3、继承出错:没有对象的基类 子类初始化 基类没有初始化 且基类有参数
子类只能初始化无 参数的 基类
错误:无 缺省的 基类可条用
#include <iostream.h>
class Animal //类 基类
{
public: Animal(int height , int weight)
{
cout<<"Animal construct "<<endl;
}
~Animal()
{
cout<<"Animal deconstruct "<<endl;
}
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
//protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
//private:
void breathe()//呼吸方法
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// 继承特性 //子类
{
public:
Fish()
{
cout<<"Fish construct "<<endl;
}
~Fish()
{
cout<<"Fish deconstruct "<<endl;
}
}; void main() {
// Animal an;
// an.eat();
Fish fh;
// fh.sleep();
}
解决:象基类中传递参数 Fish():Animal(400,300)
#include <iostream.h>
class Animal //类 基类
{
public: Animal(int height , int weight)
{
cout<<"Animal construct "<<endl;
}
~Animal()
{
cout<<"Animal deconstruct "<<endl;
}
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
//protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
//private:
void breathe()//呼吸方法
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// 继承特性 //子类
{
public:
Fish():Animal(,)
{
cout<<"Fish construct "<<endl;
}
~Fish()
{
cout<<"Fish deconstruct "<<endl;
}
}; void main() {
// Animal an;
// an.eat();
Fish fh;
// fh.sleep();
}
4)常量初始化
class Fish: public Animal// 继承特性 //子类
{
public:
Fish():Animal(,),a()
{
// cout<<"Fish construct "<<endl;
}
~Fish()
{
// cout<<"Fish deconstruct "<<endl;
}
private :
const int a;
};
4、函数的覆盖
1)不要基类的
父亲结婚:花轿
儿子击婚:轿车
#include <iostream.h>
class Animal //类 基类
{
public: Animal(int height , int weight)
{
// cout<<"Animal construct "<<endl;
}
~Animal()
{
// cout<<"Animal deconstruct "<<endl;
}
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
//protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
//private:
void breathe()//呼吸方法
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// 继承特性 //子类
{
public:
Fish():Animal(,),a()
{
// cout<<"Fish construct "<<endl;
}
~Fish()
{
// cout<<"Fish deconstruct "<<endl;
}
void sleep()
{
cout<<"jjjjjjjjjj"<<endl;
}
private :
const int a;
}; void main() {
// Animal an;
// an.eat();
Fish fh;
fh.sleep();
}
1)两个都要
父亲结婚:花轿
儿子击婚:轿车 花轿
#include <iostream.h>
class Animal //类 基类
{
public: Animal(int height , int weight)
{
// cout<<"Animal construct "<<endl;
}
~Animal()
{
// cout<<"Animal deconstruct "<<endl;
}
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
//protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
//private:
void breathe()//呼吸方法
{ cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// 继承特性 //子类
{
public:
Fish():Animal(,),a()
{
// cout<<"Fish construct "<<endl;
}
~Fish()
{
// cout<<"Fish deconstruct "<<endl;
}
void sleep()
{
Animal::sleep();
cout<<"jjjjjjjjjj"<<endl;
}
private :
const int a;
}; void main() {
// Animal an;
// an.eat();
Fish fh;
fh.sleep();
}
5、对象转换
类型转换
int--char 丢失精度 截掉了三个字节
char--int ok 允许
对象转换:
内存模型相同才能转换
输出的是 动物的 而不是 鱼的
#include <iostream.h>
class Animal //类 基类
{
public: Animal(int height , int weight)
{
// cout<<"Animal construct "<<endl;
}
~Animal()
{
// cout<<"Animal deconstruct "<<endl;
}
void eat()//吃方法
{
cout<<"animal eat "<<endl; }
//protected:
void sleep()//睡觉方法
{
cout<<"animal sleep"<<endl;
}
//private:
void breathe()//呼吸方法
{ cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal// 继承特性 //子类
{
public:
Fish():Animal(,),a()
{
// cout<<"Fish construct "<<endl;
}
~Fish()
{
// cout<<"Fish deconstruct "<<endl;
}
void sleep()
{
Animal::sleep();//作用域标示符
cout<<"jjjjjjjjjj"<<endl;
}
private :
const int a;
};
void fn(Animal *pAn)
{
pAn->sleep();
}
void main() {
// Animal an;
// an.eat();
Fish fh;
Animal *pAn;
pAn=&fh;
fn(pAn);
// fh.sleep();
}
2.2孙鑫C++的更多相关文章
- 孙鑫VC学习笔记:多线程编程
孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010 HQU Email:zgzhaobo@gmail.com QQ:452728574 Latest Modified ...
- 孙鑫视频学习:VS2010中找不到【Tab order】菜单项
在学习孙鑫视频中,修改Tab顺序时,找不到VC6.0中提到的[Layout]->[Tab order]菜单项,但VC2010中可以用Ctrl+D调出来Tab顺序,或者[格式]->[Tab键 ...
- 孙鑫视频学习:改变窗口过程函数中出现error C2440错误的解决方法
在Visual Studio 2010中,即使代码是完完全全按照孙鑫视频中的敲,也会在出现error C2440,这是因为开发平台由VC6.0升级至VS2010,需要将原有的项目迁移.VS2010对消 ...
- 孙鑫视频学习:关于Visual Studio 2010中MFC工程的消息WM_INITDIALOG找不到的问题
学习孙鑫的那个深入详解C++视频时,有一处给编辑框空间改写窗口过程函数的例子,其中需要添加一个WM_INITDIALOG消息响应函数,但在类向导的消息栏下找不到WM_INITDIALOG消息.解决方法 ...
- 孙鑫VC++视频教程笔记
写在前面的话:在学习孙鑫老师的VC++视频时,为了加深自己对知识的深入理解,就做了下面的笔记. 第一讲: 第二讲: 第三讲: 第四讲: 第五讲: 第六讲: 第七讲: 第八讲: 第九讲: 第十讲: 第十 ...
- 孙鑫视频VC++深入详解学习笔记
孙鑫视频VC++深入详解学习笔记 VC++深入详解学习笔记 Lesson1: Windows程序运行原理及程序编写流程 Lesson2: 掌握C++基本语法 Lesson3: MFC框架程序剖析 Le ...
- 孙鑫VC++视频教程(1-20课全)
孙鑫VC++视频教程(1-20课全)PPT讲义和源代码 http://down.51cto.com/data/467760 孙鑫VC++从入门到精通开发详解视频教程[20讲] http://down. ...
- 孙鑫VC学习系列教程
教程简介 1.循序渐进 从Win32SDK编程开始讲解,帮助大家理解掌握Windows编程的核心 -- 消息循环机制. 2.通俗易懂 编程语言枯燥难懂,然而通过孙鑫老师形象化的讲解,Windows和M ...
- 孙鑫C++教程留下来的作业--如何让工具栏在原来隐藏的位置出现
--加油,不仅仅是口号! BEGIN---------------------------------- 将工具栏进行停靠.当隐藏后再次点击出现的时候它出现在工具栏顶部了,并没有停靠在原来的位置,如何 ...
- 孙鑫MFC学习笔记20:Hook编程
1.HOOK拦截消息,设置越后的钩子优先级越高(钩子队列)2.SetWindowHookEx设置钩子 如果thread identifier为0或其他进程创建的线程,回调函数需要在动态链接库中声 ...
随机推荐
- ansible 访问内网服务器
ssh https://medium.com/@paulskarseth/ansible-bastion-host-proxycommand-e6946c945d30#.rauzlfv0z http: ...
- PHP 生成excel|好用强大的php excel类库
做Magento的订单导出Excel功能,找了这个php的excel类 :PHPExcel. PHPExcel是强大的 MS Office Excel 文档生成类库,基于Microsoft's Ope ...
- Android IOS WebRTC 音视频开发总结(三三)-- Periscope介绍
本文主要介绍Periscope,文章来自博客园RTC.Blacker,支持原创,转载请说明出处. 可能国内很多人没听说过Periscope,这可是现在Twitter上很火的一个APP,先看看人家自己是 ...
- ngrok逆向代理服务器搭建微信公众号本地开发环境
一条命令解决的外网访问内网问题 本地WEB外网访问.本地开发微信.TCP端口转发 平台登陆地址:http://www.ngrok.cc/login 新版本上线启动方式更简单使用视频教程 在路由器上面的 ...
- Ajax+Asp.Net无刷新分页
1.新建解决方案,并建立四个项目BLL,DAL,Model,PagerTest,如图所示: 2.Model代码 using System; using System.Collections.Gener ...
- js中forEach无法跳出循环?
1. forEach() forEach() 方法从头至尾遍历数组,为每个元素调用指定的函数.如上所述,传递的函数作为forEach()的第一个参数.然后forEach()使用三个参数调用该 函数:数 ...
- Win7、win2008中让IIS7支持asp的方法
Win7或Windows server 2008中IIS7支持ASP+Access解决方法. 1. 让IIS7支持ASP Win7或Windows server 2008中IIS7是默认不安装的, ...
- highcharts与highstock实例
highcharts实例代码 <head> <title>highcharts报表示例</title> <meta http-equiv="Cont ...
- Head First-策略模式
策略模式,什么是策略模式,定义了算法族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户. 下面我们就用鸭子来诠释一下策略模式,鸭子有两种行为呱呱叫和飞,但是并不是所有的鸭 ...
- Android Socket通信
1.TCP: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...