dev c++工程在c++main函数里调用另一个文件里的自定义的类

引论

一个五个文件, 两个自定义类

main 函数文件

#include"widget.h"

int main(){
	widget w;
	int a=1;
	int b=2;
	cout<<w.calculate(a,b)<<endl;
	system("pause");
}

widget.h文件

///widget.h文件:
#include<iostream>
using namespace std;

class widget{
	public:int calculate(int a,int b);
};

widget.cpp文件

/////widget.cpp文件:
#include"widget.h"
#include"map.h"

int widget::calculate(int a,int b){
	map m;//实现了map实例对象m;
	return m.count(a,b);//调用map类中count函数计算a+b的和
}

map.h文件:

////map.h文件:
#include<iostream>
using namespace std;

class map{
	public: int count(int a,int b);//map类方法(函数)
};

map.cpp文件:

/////map.cpp文件:
#include"map.h"

int map::count(int a,int b){
	return a+b;
}

运行结果

发布了19 篇原创文章 · 获赞 2 · 访问量 338

猜你喜欢

转载自blog.csdn.net/qq_43008718/article/details/104188702
Dev