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. STL源码分析-AVL树-RB树

    AVL树 不平衡情况 插入节点位于左子节点的左子树(左左) 插入节点位于左子节点的右子树(左右) 插入节点位于右子节点的左子树(右左) 插入节点位于右子节点的右子树(右右) 左左.右右为外侧插入,左右 ...

  2. CSS阻止页面双击选中文本

    转载自:w3cui 在双击左右箭头,快速切换图片滚动时,会选择附近区域的文字,感觉不是很好,今天在同事在分享时,讲到了这个问题, 试了一下,不错,解决了问题IE及Chrome下的方法一样,对相应的元素 ...

  3. SWFUpload使用指南

    SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大. SWFUpload的特点: 1.用flash进行上传,页面无刷新,且可自定义Flash按钮的样式; 2.可以在浏览器 ...

  4. 纯servlet返回xml数据

    ... void doget..... response.setContentType("application/xml");//设置格式  PrintWriter out = r ...

  5. dig out secrets beneath AirSig

    My sister installed AirSig last week. She is so exciting about this new techknology and she won't st ...

  6. leetcode 121

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  7. hdu2602

    01-bag #include <stdio.h> #include <math.h> #include <string.h> int main(){ int t; ...

  8. 基于s5pv210嵌入式linux系统sqlite3数据库移植

    基于s5pv210嵌入式linux系统sqlite3数据库移植 1.下载源码 http://www.sqlite.org/download.html 最新源码为3080100 2.解压 tar xvf ...

  9. mysql中union与union all的区别

    当查询表结构完全相同的多张表的数据时: 1.当查询条件完全相同且不包括主键,此时用union查询会过滤掉查询出的重复的记录,及漏查记录:使用union all进行查询,则会查出所有的符合条件的记录,保 ...

  10. yii2.0 DetailView 自定义样式

    GII 生成如下: <?= DetailView::widget([ 'model' => $model, 'attributes' => [ 'id', ['label'=> ...