1. 程式人生 > >在vs2015 中c++學習筆記(孫鑫視訊2)

在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");
}