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或其他进程创建的线程,回调函数需要在动态链接库中声 ...
随机推荐
- python线程使用场景 多线程下载
http://blog.xiayf.cn/2015/09/11/parallelism-in-one-line http://python.jobbole.com/84327/ http://www. ...
- 法线贴图——Normal Mapping
对于不曾学过.用过法线贴图的人来说,提到法线贴图,经常会提到的问题是什么是法线贴图?法线贴图用于解决什么问题?法线贴图的原理是什么?本文将就这三个问题阐述本人的一些见解,各位不喜勿喷!!! 谈到法线贴 ...
- word文档中的字号和磅的对应关系
字号 磅 初号 42 小初 36 一号 26 小一 24 二号 22 小二 18 三号 16 小三 15 四号 14 小四 12 五号 10.5 小五 9 六号 7.5 小六 6.5 七号 5.5
- EasyUI-Combox
Combox的数据格式和默认选中项设置 [{ "id":1, "text":"text1" },{ "id":2, &q ...
- C#处理Excel
C#处理Excel C#处理Excel 前言 OleDb 具体操作 NPOI 具体操作 Excel C# NPOI OleDb 前言 最近需要对Excel进行加密解密操作,本身是一个简单的事情,通过 ...
- s3c6410_uart初始化及读写
参考: 1)<USER'S MANUAL-S3C6410X>第31章 UART 2)u-boot uart初始化及读写:u-boot-x.x.x/board/samsumg/smdk641 ...
- jquery.qrcode.js生成二维码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- subpage和secondary page的区别
Hi, Can you tell me the differences between subpage and secondary page and when to use which ? I n ...
- PHP常用函数和常见疑难问题解答
PHP常用库函数介绍 一.PHP字符串操作常用函数 1.确定字符串长度 int strlen(string str) 2.比较两个字符串 a. strcmp函数对两个字符串进行二进制安全的比较,并区 ...
- Mysql常用数据类型详细说明及实例说明(学习笔记一)
1.Mysql 在windows下 Net start mysql[启动] Net stop mysql[停止] Quit[退出mysql命令行] \c[取消输入的命令] Select version ...