C++类的使用2学习笔记

本文详细介绍了C++中类的定义、声明、使用及其成员函数Setab、Move和Display的具体实现,通过实例展示了如何创建并操作类对象,深入理解C++面向对象编程的基本概念。

类的第二个练习例子:

类的声明

#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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值