搜索
您的当前位置:首页正文

关于vs2017中将类分解成.h和.cpp文件的写法

来源:好走旅游网

关于vs2017中将类分解成.h和.cpp文件的写法

以Circle做例子:

main函数的.cpp文件中要包含的内容如下:

#include"pch.h"
#include<iostream>
#include<string>
#include<opencv2/opencv.hpp>
#include<vector>
#include<algorithm>
#include"Point.h"                         **//在main函数中要包含所定义类的声明文件,即.h文件**
using namespace std;
using namespace cv;

void test01()
{
	Circle c;
	c.setX(10);
	c.setY(20);
	c.getX();
	c.getY();
	cout << "m_X = " << c.getX() << endl;
	cout << "m_Y = " << c.getY() << endl;
}
int main()
{
	test01();
	system("pause");
}

将Point类分解为.cpp文件中所包含的内容

#include"pch.h"
#include"Point.h" //在类的.cpp文件中也要包含类的声明文件,即.h文件
#include<iostream>

using namespace std;
void Circle::setX(int x)              //定义类的构造函数时要添加所定义的类及类作用域符号
{
	m_X = x;
}

 int Circle::getX()
{
	return  m_X;
}
 void Circle::setY(int y)
{
	m_Y = y;
}
 int Circle::getY()
{
	return  m_Y ;
}

将Point类分解成.h文件所包含的内容

#pragma once  //避免类的重复定义
class Circle   //类的声明
{
public:
	void setX(int x);                    //将类的所有声明都写入.h文件中
	int getX();   
	void setY(int y);
	int getY();
private:
	int m_Y;
	int m_X;
};

因篇幅问题不能全部显示,请点此查看更多更全内容

Top