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或其他进程创建的线程,回调函数需要在动态链接库中声 ...
随机推荐
- css的border效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【MySQL】查询使用临时表
MySQL查询产生临时表的分析 官网说明的地址:http://dev.mysql.com/doc/refman/5.5/en/internal-temporary-tables.html 参考:htt ...
- 织梦DedeCMS列表摘要 description 长度控制方法
[field:description /]标签如何限制字数? [field:description function='cn_substr(@me,80)'/] DedeCMS 里的所有标记都支持这样 ...
- SQL笔记-第二章,数据表的创建和管理
数据类型 分5类:整数.数值.字符相关.日期时间以及二进制 1.整数 数据库系统 类型 说明 MYSQL tinyint [unsigned] 一个很小的整数.有符号的范围是-128 到127,无符号 ...
- 使用buildroot编译bind DNS服务器
用buildroot来制作文件系统很方便,编译出来的文件系统是直接可用的,不用添加脚本等麻烦的工作,很多的库和app都可以直接添加到文件系统里边,如常用的udhcpc,tftp,apache,ligh ...
- 为什么for in循环不适合用于数组
首先一点无关的,使用(var i in a) 而不是( i in a),除非你想创建全局变量. 第二点,for in 循环会忽略空的数组 var a = []; a[5] = 5; // Perfec ...
- POJ C++程序设计 编程题#1 编程作业—运算符重载
编程题 #2 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的MyIn ...
- MongoDB数据库的主从配对与迁移示例
数据中心在运行中有可能遇到各种硬件.电力.网络故障等问题,需要设计良好的系统来隔离,尽量减少对上层应用的影响,持续对外提供服务:一旦发生业务中断,也应尽快恢复.通过主从备份设计,在主应用系统发生故障时 ...
- zookeeper典型应用场景之一:master选举
对于zookeeper这种东西,仅仅知道怎么安装是远远不够的,至少要对其几个典型的应用场景进行了解,才能比较全面的知道zk究竟能干啥,怎么玩儿,以后的日子里才能知道这货如何能为我所用.于是,有了如下的 ...
- 百度搜索API v3版本与soap
技术文档请参考http://dev2.baidu.com/docs.do?product=2#page=File,和http://dev2.baidu.com/docs.do?product=2#pa ...