專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

C++中創(chuàng)建頭文件的方法

作者:黃波海   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月07日   【字體:

 創(chuàng)建頭文件。(不帶形參)
cpp通過類名+::+函數(shù)名被頭文件鏈接。注意一定是類名+::!
函數(shù)代碼放在cpp下的相應(yīng)的函數(shù)名里,而頭文件中的只是函數(shù)名,只負(fù)責(zé)提供映射。
animal.cpp
#include "animal.h"
#include <iostream.h>
animal::animal()
{
   cout<<"hello"<<endl;
}
void animal::eat ()
{
   cout<<"shift"<<endl;
}


animal.h
//頭文件只寫函數(shù)名,提供鏈接地址。
#ifndef ANIMAL_H_H
#define ANIMAL_H_H
class animal
{
  public:
  animal();

void eat();
};
#endif

-------------------------------------------------------------------------------------

如果不創(chuàng)建頭文件就要寫這么長的代碼 可讀性很差#include<iostream.h>

class animal
{
public:
animal()
{
cout<<"animal construct"<<endl;
}
~animal()
{
cout<<"construct animal"<<endl;
}
virtual void breath()  
{
cout<<"bubble2"<<endl;
}
void eat();//把主函數(shù)放在類外的方法
};
class fish:public animal 
{
public:
fish()
{
 
}
~fish()
 
}
void breath()
{   
}
};
void animal::eat()//函數(shù)類型,屬于那個(gè)類。把一個(gè)函數(shù)的實(shí)現(xiàn)放到類之外。
{
 
}
 
void main()
{
 
}
關(guān)閉窗口

相關(guān)文章