C++中創(chuàng)建頭文件的方法
cpp通過類名+::+函數(shù)名被頭文件鏈接。注意一定是類名+::!
函數(shù)代碼放在cpp下的相應的函數(shù)名里,而頭文件中的只是函數(shù)名,只負責提供映射。
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
-------------------------------------------------------------------------------------
如果不創(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ù)類型,屬于那個類。把一個函數(shù)的實現(xiàn)放到類之外。
{
}
void main()
{
}
關鍵詞:
C++頭文
評論