头文件:
wangkai.h
#ifndef HEADER_PPOINT
#define HEADER_PPOINT
class Uestc {
double x, y;
public:
Uestc( double ix, double iy );
void print();
};
#endif
一定要注意类定义后的那个分号。如果没有那个分号,会导致个各种编译错误。
类的实现:uestc.cpp
#include "wangkai.h"
#include <iostream>
//#include <cmath>
using namespace std;
Uestc::Uestc( double ix, double iy )
{
x = ix; y = iy;
}
void Uestc::print()
{
cout << "x = " <<x << endl;
cout << "y = " << y << endl;
}
调用类的应用程序:
#include "wangkai.h"
#include <iostream>
using namespace std;
int main() {
Uestc wangkai( 100, 200 );
wangkai.print();
}
编译及其运行结果:
angkai@ubuntu:~/Test$ g++ uestc.cpp main.cpp
wangkai@ubuntu:~/Test$ ./a.out
x = 100
y = 200
wangkai@ubuntu:~/Test$