Class 基本语法
header file
#ifndef CAT_H_
#define CAT_H_
class Cat {
public:
void speak();
void jump();
};
#endif
Cat.cpp
#include <iostream>
#include "Cat.h"
void Cat::speak() {
using std::endl;
using std::cout;
cout << "Meouwww!!" << endl;
}
void Cat::jump() {
using std::cout;
using std::endl;
cout << "Jumping to top of bookcase" << endl;
}
main.cpp
#include <iostream>
#include "Cat.h"
int main() {
Cat cat;
cat.speak();
cat.jump();
return 0;
}
执行:
g++ -I Cat.h Cat.cpp main.cpp -o app && ./app