WARNING: 这篇文档虽然能用, 但是其实有更好的办法, 就是把.cpp改为.hpp, 然后在.h文件的末尾 # include “.hpp”, 在编译的时候只需要g++ main.cpp就行了
你本来的文件长这样
# include <iostream>
class myclass{
public:
int property;
void say_property(){
std::cout << property << std::endl;
}
};
int main() {
myclass i; //i for instance
i.property = 1;
i.say_property();
return 0;
}
放到另一个文件里
main.cpp内的处理类似把函数放到另一个文件里,也是在main()前面要写上class的声明,
但是另一个文件里就不太一样了,
不能直接在另一个文件里定义class,而是先声明class再针对未定义的部分使用,myclass::func(){}进行定义。
//>>>cat main.cpp
class myclass{
//仅声明
public:
int property;
void say_property();
};
int main() {
...
//>>>cat anotherFile.cpp
# include<iostream>
class myclass{
//仅声明
...
};
void myclass::say_property(){
...
>>>g++ main.cpp anotherFile.cpp
放到另外两个文件里面
思路和把函数放到另外两个文件里相同,只不过是用一个头文件代替class的声明
//>>>cat main.cpp
# include "whatever.hpp"
int main() {
...
//>>>cat whatever.hpp
#pragma once
class myclass{
//仅声明
...
};
//>>>cat anotherFile.cpp
# include "whatever.hpp"
# include <iostream>
void myclass::say_property(){
...
>>>g++ ...
如果你想要更具体的例子, 可以往下继续看
//>>>cat main.cpp
# include <iostream>
# include <string>
# include "Meat.h"
void test(){
Meat undefinedMeat; //good
std::cout << undefinedMeat.get_name() << std::endl; //good
std::cout << undefinedMeat.get_price_per() << std::endl; //good
std::cout << undefinedMeat.get_n_stored() << std::endl; //good
std::cout << undefinedMeat.get_price(10) << std::endl; //good
Meat beef("beef", 300.00, 10); //good
std::cout << beef.get_name() << std::endl; //good
std::cout << beef.get_price_per() << std::endl; //good
std::cout << beef.get_n_stored() << std::endl; //good
std::cout << beef.get_price(10) << std::endl; //good
beef.buy(5);std::cout << beef.get_n_stored() << std::endl; //good
}
int main(){
test();
return 0;
}
//>>>cat Meat.cpp
# include <string>
# include "Meat.h"
std::string Meat::get_name(){
return _name;
}
double Meat::get_price_per(){
return _price_per;
}
double Meat::get_price(int n_required){
return n_required*_price_per;
}
int Meat::get_n_stored(){
return _n_stored;
}
void Meat::buy(int n_required){
//用户输入要买的数量, 然后进行购买, 暂时不考虑交互的问题
int new_n_stored = _n_stored;
new_n_stored = _n_stored - n_required;
set_n_stored(new_n_stored);
}
Meat::Meat():Meat("meat", 30, 10){
}
Meat::Meat(std::string name, double price_per, int n_stored){
set_name(name);
set_price_per(price_per);
set_n_stored(n_stored);
}
void Meat::set_name(std::string name){
_name = name;
}
void Meat::set_price_per(int new_price_per){
_price_per = new_price_per;
}
void Meat::set_n_stored(int n_stored){
_n_stored = n_stored;
}
//>>>cat Meat.h
# pragma once
# include <string>
# include "Meat.h"
class Meat{
public:
std::string get_name();
double get_price_per();
double get_price(int n_required);
int get_n_stored();
void buy(int n_required);
Meat();
Meat(std::string name, double price_per, int n_stored);
private:
std::string _name = "undefined meat";
void set_name(std::string name);
double _price_per = 0;
void set_price_per(int new_price_per);
int _n_stored = 0;
void set_n_stored(int n_stored);
};
>>>g++ main.cpp Meat.cpp
>>>./a.out
meat
30
10
300
beef
300
10
3000
5