c++构造函数

来源:互联网 发布:python socket通信框架 编辑:程序博客网 时间:2024/06/07 00:13

头文件:

wangkai.h

#ifndef HEADER_PPOINT#define HEADER_PPOINTclass 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 = 100y = 200wangkai@ubuntu:~/Test$ 

原创粉丝点击