新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > C++中創(chuàng)建頭文件的方法

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

作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
創(chuàng)建頭文件。(不帶形參)

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

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

void eat();
};
#endif

本文引用地址:http://2s4d.com/article/201612/324422.htm

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

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

class animal
{
public:
animal()
{
cout<<"animal construct"<
}
~animal()
{
cout<<"construct animal"<
}
virtual void breath()
{
cout<<"bubble2"<
}
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)鍵詞: C++頭文

評論


技術(shù)專區(qū)

關(guān)閉