下面是定义一个point类,在main()中实现:
#include "stdafx.h"
#include <iostream.h>
 
//平面上的点类
class point   
{
private:
double x,y;   //点坐标成员变量,其属性为私有public:
    void setx(double tx);  //设置X,Y坐标值
void sety(double ty);
double getx();   //获取X,Y坐标值
double gety();
}void point::setx(double tx)  //通过该函数设置point类的私有成员变量(点坐标x)
{
x=tx;
}
double point::getx(void)    //通过该函数获取point类的私有成员变量(点坐标x)
{
return x;
}
void point::sety(double ty)   //通过该函数设置point类的私有成员变量(点坐标y)
{
x=ty;
}
double point::gety(void)  //通过该函数获取point类的私有成员变量(点坐标y)
{
return y;
}
int main(int argc, char* argv[])
{
point p1;                  //声明类的对象
point* ptr=new point;   //声明类的对象指针
    double x=0.0;
double y=0.0;
cout<<"请输入第一点的X坐标(双精度)"<<"\n";
cin>>x;
cout<<"请输入第一点的Y坐标(双精度)"<<"\n";
cin>>y;
p1.setx(x);
p1.sety(y);
cout<<"请输入第二点的X坐标(双精度)"<<"\n";
cin>>x;
cout<<"请输入第二点的Y坐标(双精度)"<<"\n";
cin>>y;
ptr->setx(x);
ptr->sety(y);
cout<<"p1(x,y)=("<<p1.getx()<<","<<p1.gety()<<")\n";
cout<<"ptr(x,y)=("<<ptr->getx()<<","<<ptr->gety()<<")\n";
delete ptr; return 0;
}
调试时提示出错如下:
Compiling...
SdkTest1.cpp
C:\Documents and Settings\Administrator\My Documents\TEXT\SdkTest1\SdkTest1.cpp(20) : error C2628: 'point' followed by 'void' is illegal (did you forget a ';'?)
C:\Documents and Settings\Administrator\My Documents\TEXT\SdkTest1\SdkTest1.cpp(21) : error C2556: 'class point __thiscall point::setx(double)' : overloaded function differs only by return type from 'void __thiscall point::setx(double)'
        C:\Documents and Settings\Administrator\My Documents\TEXT\SdkTest1\SdkTest1.cpp(14) : see declaration of 'setx'
C:\Documents and Settings\Administrator\My Documents\TEXT\SdkTest1\SdkTest1.cpp(21) : error C2371: 'setx' : redefinition; different basic types
        C:\Documents and Settings\Administrator\My Documents\TEXT\SdkTest1\SdkTest1.cpp(14) : see declaration of 'setx'
C:\Documents and Settings\Administrator\My Documents\TEXT\SdkTest1\SdkTest1.cpp(48) : error C2264: 'setx' : error in function definition or declaration; function not called
C:\Documents and Settings\Administrator\My Documents\TEXT\SdkTest1\SdkTest1.cpp(54) : error C2264: 'setx' : error in function definition or declaration; function not called
Error executing cl.exe.SdkTest1.exe - 5 error(s), 0 warning(s)