类的第二个练习例子:
类的声明
#ifndef POINT_H_
#define POINT_H_
#include <string>
using namespace std;
class Point{
private:
int a,b;
public:
void Setab(int m,int n);
void Move(int m,int n);
void Display();
int Geta();
int Getb();
};
#endif
类的定义:
#include <iostream>
#include "point.h"
using namespace std;
void Point::Setab(int m,int n){
a=m;
b=n;
}
void Point::Move(int m,int n)
{
a=a+m;
b=b+n;
}
void Point::Display()
{
cout<<" a "<<a<<" b "<<b<<endl;
}
int Point::Geta(){
return a;
}
int Point::Getb()
{
return b;
}
类的使用:
#include <iostream>
#include "point.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
Point point;
point.Setab(32,55);
point.Display();
cout<<"-------------------"<<endl;
point.Move(33,34);
point.Display();
return 0;
}
本文详细介绍了C++中类的定义、声明、使用及其成员函数Setab、Move和Display的具体实现,通过实例展示了如何创建并操作类对象,深入理解C++面向对象编程的基本概念。
573

被折叠的 条评论
为什么被折叠?



