C++基础语法

Animal.h

#ifndef ANIMAL_H_H
#define ANIMAL_H_H
class Animal
{
public:
 Animal(int height,int weight);


 void eat();


 void sleep();
 
 virtual void breathe();//=0;
};

#endif

Animal.cpp

#include "Animal.h"
#include <iostream.h>

Animal::Animal(int height,int weight)
{
}

void Animal::eat()
{
 cout<<"animal eat"<<endl;
}

void Animal::sleep()
{
 cout<<"animal sleep"<<endl;
}

void Animal::breathe()
{
 cout<<"aniaml breathe"<<endl;
}

Fish.h

#include "Animal.h"

#ifndef FISH_H_H
#define FISH_H_H
class Fish : public Animal
{
public:
 Fish();

 void breathe();
};
#endif

#Fish.cpp

#include "Fish.h"
#include <iostream.h>
Fish::Fish():Animal(300,400)
{
}

void Fish::breathe()
{
 cout<<"fish bubble"<<endl;
}

Main.cpp

#include <iostream.h>

#include "Animal.h"
#include "Fish.h"

void fn(Animal *pAn)
{
 pAn->breathe();
}


void main()
{
 //Animal an;
 //an.eat();

// an.breathe();
 Fish fh;
 Animal *pAn;
 pAn=&fh;
 fn(pAn);

}

原文地址:https://www.cnblogs.com/jcomet/p/1412987.html