---
title: "VC++ 基础（一）"
author: "holoblog"
date: "2011-11-20T13:01:00Z"
tags: []
slug: "vc"
draft: false
lastmod: "2011-11-20T20:23:07Z"
id: 1268835
url: "https://holoblog.iteye.com/blog/1268835"
---

#include&lt;iostream.h&gt;

class Animal{  
public://任何地方都可以访问  
//构造方法：1、与类名相同。2、没有返回值。3、可以带参数  
Animal(){  
//cout&lt;&lt;"Animal construct"&lt;&lt;endl;  
}  
Animal(int height,int weight){  
//cout&lt;&lt;"Animal construct"&lt;&lt;endl;  
}  
//析构方法：1、不允许有返回值。2、不允许带参数。用于内存的释放  
~Animal(){  
//cout&lt;&lt;"Animal deconstruct"&lt;&lt;endl;  
}  
void eat(){  
cout&lt;&lt;"animal eat"&lt;&lt;endl;  
}  
//protected://只在自己、子类中可以访问  
void sleep(){  
cout&lt;&lt;"animal sleep"&lt;&lt;endl;  
}  
//private://只有 自己 可以访问  
/*  
会采用迟绑定（late binding）的技术  
如果子类没有breathe函数就调用基类的，如果有就调用子类的  
\*/  
//virtual 虚函数  
virtual void breathe(){  
cout&lt;&lt;"animal breathe"&lt;&lt;endl;  
}  
/*  
virtual void breathe()=0;  
纯虚函数  
含有纯虚函数的类叫抽象类  
只要子类实现了纯虚函数时，才能实例化。否则不能实例化子类  
\*/  
void get();  
};  
void Animal::get(){  
cout&lt;&lt;"animal get"&lt;&lt;endl;  
}  
class Fish : public Animal{//继承  
public:  
//向基类传参数。可以省去:Animal(175,120)就不传参数。本身的常量a必须要初始化  
Fish():Animal(175,120),a(1)  
{  
//cout&lt;&lt;"Fish construct"&lt;&lt;endl;  
}  
~Fish(){  
//cout&lt;&lt;"Fish deconstruct"&lt;&lt;endl;  
}  
//方法的覆盖。覆盖基类中的方法。  
void breathe(){//与父类的 虚函数进行区别  
//Animal::breathe();//声明该方法是基类中的breathe方法。::作用域标识符  
cout&lt;&lt;"Fish bubble"&lt;&lt;endl;  
}  
private:  
const int a;  
};  
void fn(Animal \*pAn){  
pAn-&gt;breathe();  
}  
void main(){  
Fish fh;  
//fh.breathe();  
Animal \*pAn;//声明一个指针  
pAn=&fh;//把鱼的地址赋给这个指针、隐式转换  
fn(pAn);//输出是Animal对象：animal breathe

int a=6;  
int &b=a;  
/*  
b是a的一个引用，b是a的一个别名。  
b指向a的地址。  
b维系在特定的目标(a)上面  
引用(&b)在定义的时候必须初始化。  
引用一般用在传参。语义更清晰  
change(x,y);  
change(&a,&b){};  
\*/  
b=6;//a的值也会改变  
}