創(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>