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++的更多相关文章

  1. 孙鑫VC学习笔记:多线程编程

    孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010  HQU Email:zgzhaobo@gmail.com    QQ:452728574 Latest Modified ...

  2. 孙鑫视频学习:VS2010中找不到【Tab order】菜单项

    在学习孙鑫视频中,修改Tab顺序时,找不到VC6.0中提到的[Layout]->[Tab order]菜单项,但VC2010中可以用Ctrl+D调出来Tab顺序,或者[格式]->[Tab键 ...

  3. 孙鑫视频学习:改变窗口过程函数中出现error C2440错误的解决方法

    在Visual Studio 2010中,即使代码是完完全全按照孙鑫视频中的敲,也会在出现error C2440,这是因为开发平台由VC6.0升级至VS2010,需要将原有的项目迁移.VS2010对消 ...

  4. 孙鑫视频学习:关于Visual Studio 2010中MFC工程的消息WM_INITDIALOG找不到的问题

    学习孙鑫的那个深入详解C++视频时,有一处给编辑框空间改写窗口过程函数的例子,其中需要添加一个WM_INITDIALOG消息响应函数,但在类向导的消息栏下找不到WM_INITDIALOG消息.解决方法 ...

  5. 孙鑫VC++视频教程笔记

    写在前面的话:在学习孙鑫老师的VC++视频时,为了加深自己对知识的深入理解,就做了下面的笔记. 第一讲: 第二讲: 第三讲: 第四讲: 第五讲: 第六讲: 第七讲: 第八讲: 第九讲: 第十讲: 第十 ...

  6. 孙鑫视频VC++深入详解学习笔记

    孙鑫视频VC++深入详解学习笔记 VC++深入详解学习笔记 Lesson1: Windows程序运行原理及程序编写流程 Lesson2: 掌握C++基本语法 Lesson3: MFC框架程序剖析 Le ...

  7. 孙鑫VC++视频教程(1-20课全)

    孙鑫VC++视频教程(1-20课全)PPT讲义和源代码 http://down.51cto.com/data/467760 孙鑫VC++从入门到精通开发详解视频教程[20讲] http://down. ...

  8. 孙鑫VC学习系列教程

    教程简介 1.循序渐进 从Win32SDK编程开始讲解,帮助大家理解掌握Windows编程的核心 -- 消息循环机制. 2.通俗易懂 编程语言枯燥难懂,然而通过孙鑫老师形象化的讲解,Windows和M ...

  9. 孙鑫C++教程留下来的作业--如何让工具栏在原来隐藏的位置出现

    --加油,不仅仅是口号! BEGIN---------------------------------- 将工具栏进行停靠.当隐藏后再次点击出现的时候它出现在工具栏顶部了,并没有停靠在原来的位置,如何 ...

  10. 孙鑫MFC学习笔记20:Hook编程

    1.HOOK拦截消息,设置越后的钩子优先级越高(钩子队列)2.SetWindowHookEx设置钩子    如果thread identifier为0或其他进程创建的线程,回调函数需要在动态链接库中声 ...

随机推荐

  1. 【MySQL】查询使用临时表

    MySQL查询产生临时表的分析 官网说明的地址:http://dev.mysql.com/doc/refman/5.5/en/internal-temporary-tables.html 参考:htt ...

  2. Server2003安装SP2补丁提示密钥无效的解决方法

    机器一直没有打SP2的补丁.几天要安装一个程序,还非要SP2不可.没办法.居然打补丁的时候出现了这样的错误信息: --------------------------- Service Pack 2 ...

  3. Android IOS WebRTC 音视频开发总结(十)-- webrtc入门002

    继续上一篇中未翻译完成的部分,主要包括下面三个部分: 1,扩展:WebRTC多方通话. 2,MCU Multipoint Control Unit. 2, 扩展:VOIP,电话,消息通讯. 注意:翻译 ...

  4. 第四章_PHP基本语法(2)

    1.常量的声明 在PHP中,定义常量使用define()函数来实现 2.魔术常量 名称 作用 __LINE__ 返回文件中的当前行号 __FILE__ 返回该文件的完整路径和文件名 __DIR__ 返 ...

  5. 《Linux运维趋势》2010-2013年全部期刊下载

    <Linux运维趋势>2010.rar <Linux运维趋势>2011_上.rar <Linux运维趋势>2011_下.rar <Linux运维趋势>2 ...

  6. ubuntu 字体 android stuido 汉字 显示 方块

    Ubuntu 12.04 LTS 中安装 windows 字体   ubuntu 中的中文字体看着总觉的有点不爽,于是百度了下,这里记录下怎么在 ubuntu 12.04 中安装 windows 字体 ...

  7. xode View 的封装

    1.Xcode自带头文件的路径 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Develo ...

  8. 在peopletools里面测试文件上传

    Using the PeopleTools Test Utilities Page Select selectPeopleTools, then selectUtilities, then selec ...

  9. 请注意designer中的NAME field

    If you are trying to set the value of a field through PeopleCode and that field happens to be NAME t ...

  10. centos内核优化--转至网络

    A.关闭selinux(可以预防nginx修改根目录后访问出现404或者403) vi /etc/selinux/config SELINUX=disabled B.修改打开最大文件数句柄也就是 so ...