把程序的定义与实现部分用头文件与源文件分开

把程序的定义与实现部分用头文件与源文件分开

把不同类分别放到不同的头文件与源文件当中,以构造良好的程序的设计风格。

下边以一个简单的例子MainTest将不同类分别放到不同的头文件与源文件中。

在VC中建一个"MainTest工程",再新建添加如以下所需要的头文件及源文件;

201102191927185428.png

Animal.h:

//使用预编译指令避免头文件被重复定义

#ifndef ANIMAL_H_H

#define ANIMAL_H_H

class Animal

{

public:

    Animal(int height,int weight);

    void eat();

//protected:

    void sleep();

//private:

    virtual void breathe();

};

#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<<"animal breathe"<<endl;

}

Fish.h:

#include "Animal.h"

//使用预编译指令避免头文件被重复定义

#ifndef FISH_H_H

#define FISH_H_H

class Fish :public Animal

{

public:

    Fish();

    void breathe();

    void test();

private:

    const int a;

};

#endif

Fish.cpp:

#include "Fish.h"

#include <iostream.h>

Fish::Fish():Animal(400,300),a(1)

{

}

void Fish::breathe()

{

    cout<<"fish bubble"<<endl;

}

void Fish::test()

{

    sleep();

    eat();

    breathe();

}

Main.cpp:

#include<iostream.h>

//#include <stdlib.h>

#include<cstdlib>

#include "Animal.h"

#include "Fish.h"

//#include <stdio.h>

void fn(Animal *pAn)

{

    pAn->breathe();

}

void main()

{

//    Animal an;

//    an.eat();

    Fish fh;

    Animal *pAn;

    pAn=&fh;

    fn(pAn);

    //fh.breahte();

    fh.test();

//    fh.sleep();

}

//所有的类的声明与实现部分都写在一个源文件当中的情况

#include<iostream.h>

//#include <stdlib.h>

#include<cstdlib>

class Animal

{

public:

    Animal(int height,int weight)

    {

//        cout<<"Animal construct"<<endl;

    }

    ~Animal()

    {

//        cout<<"Animal deconstruct"<<endl;

    }

    void eat();

//protected:

    void sleep()

    {

        cout<<"animal sleep!"<<endl;

    }

//private:

    virtual void breathe()//=0;

    {

        cout<<"animal breathe!"<<endl;

    }

};

void Animal::eat()

{

    cout<<"animal eat!"<<endl;

}

class Fish :public Animal

{

public:

    Fish():Animal(400,300),a(1)

    {

//        cout<<"Fish construct"<<endl;

    }

    ~Fish()

    {

//        cout<<"Fish deconstruct"<<endl;

    }

    void breathe()

    {

        //Animal::breathe();

        cout<<"fish bubble"<<endl;

    }

    void test()

    {

        sleep();

    //    breathe();

    }

private:

    const int a;

};

void fn(Animal *pAn)

{

    pAn->breathe();

}

void main()

{

//    Animal an;

//    an.eat();

    Fish fh;

    Animal *pAn;

    pAn=&fh;

    fn(pAn);

    //fh.breahte();

    fh.test();

//    fh.sleep();

//补充=======================================================

    //引用,引用不需要内存地址,这里的b就相当于变量a的一个别名,它们所代表的是同一个内存地址的内容;

    //而指针变量本身则需要给其分配内存地址,只是指针是一种特殊的变量,即用来存放内存地址的变量。

    int a=6;

    int &b=a;

    b=5;

    int c=7;

    b=c;

    cout<<a<<endl<<b<<endl<<c<<endl;

    system("pause");

//在前面加个头文件 #include <stdlib.h> //标准库头文件

//或者在前面加#include<cstdlib>

}

转载于:https://www.cnblogs.com/luowei010101/archive/2011/02/19/1958641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值