在vs2015 中c++学习笔记(孙鑫视频2)

//#include<cstdlib>
#include<iostream>
using namespace std;
//父类
class Animal
{
public://访问方式三种:public、private、protected
Animal(int height,int wigth)
{
// cout << "animal construct" << endl;
}
~Animal()
{
// cout << "animal deconstruct" << endl;
}
void eat();
/* {
cout << "animal eat" << endl;
}*/
//protected:则下面的成员只可以子类调用外部,不可以调用
//private:子类都不可以调用
void sleep()
{
cout << "animal sleep" << endl;
}
// virtual void breathe() = 0;//纯虚函数,无具体实现。有名字无内容,让派生类在具体继承的时候再给出定义
virtual void breathe()
    {
cout << "animal breathe" << endl;
}
};
//子类
class Fish :public Animal
{
/*void test()
{
sleep();
breath();
}*///子类调用
public:
Fish():Animal(400,300),a(1)//子类向基类传递参数
{
// cout << "Fish construct" << endl;
}
~Fish() 
{
// cout << "Fish deconstruct" << endl;
}
void breathe()
{
Animal::breathe();//"::"作用率标识符,表示属于哪一类
cout << "fish bubale" << endl;
}
private:
const int a;//定义常量
};
void Animal::eat()//把函数的实现放到了类外面
{
}
void fn(Animal *pan)
{
pan->breathe();
}
//外部函数调用
void main()
{
//Animal an;
//an.eat();
Fish fh;
Animal *pan;
pan = &fh;
fn(pan);
//fh.breathe;
//fh.sleep();
//引用,变量的别名
int a = 6;
int &b = a;//引用
b = 5;


system("pause");
}

猜你喜欢

转载自blog.csdn.net/qq_26572229/article/details/69828505
今日推荐