c++类封装

c++类封装,第1张

直接在使用的文件中写
class Point
{
public:
	void setX(int a) {
		x = a;
	}
	void setY(int a) {
		y = a;
	}
private:
	int x;
	int y;

};
分开在头文件和源文件中写

头文件中

#pragma once //放置头文件重复包含
#include 
using namespace std;

//此处成员函数声明,成员变量声明
class Point
{
public:
	void setX(int a);
	void setY(int a);
private:
	int x;
	int y;

};

源文件中

#include "point.h"

//此处成员函数都写全如:
void Point::setX(int a) {
	x = a;
}
void Point::setY(int a) {
	y = a;
}
使用时

一个类在是另一个类的成员时,在大类的头文件里面引入小类的头文件,使用是引入所有类的头文件

#include 
using namespace std;
#include "point.h"

int main(){
	Point p;
	p.setX(2);
	p.setY(3);
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/3002887.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存