网站建设和定位,学做效果图网站,网站权重排行,网站制作前景demo1只有类的情况下调用
下面写一个util.h 文件里面
// 定义宏防止编译器重复编译
#ifndef TEST_H
#define TEST_H
class Test{
public:void sum(int a, int b);int num(int a, int b);bool number();};
#endif // TEST_H
调用的时候首先要引入这个头文件 #include u…demo1只有类的情况下调用
下面写一个util.h 文件里面
// 定义宏防止编译器重复编译
#ifndef TEST_H
#define TEST_H
class Test{
public:void sum(int a, int b);int num(int a, int b);bool number();};
#endif // TEST_H
调用的时候首先要引入这个头文件 #include util.h
cpp 里面实现
#include iostream
#include string
#include util.h
using namespace std;// 实现Test类的sum成员函数
void Test::sum(int a,int b){cout The sum of a and b is: a b std::endl;
}
// 实现Test类的num成员函数
int Test::num(int a, int b) {return a * b;
}
// 实现Test类的number成员函数
bool Test::number() {return true;
}int main() {int a 3;int b 6;Test test;test.sum(a,b);int result test.num(a,b);cout num result result endl;bool isNumber test.number();cout number result isNumber endl;return 0;
}
demo2 有namespace的情况下调用
util.h 文件
// 定义宏防止编译器重复编译
#ifndef TEST_H
#define TEST_H
namespace common::comm::com {
class Test{
public:void sum(int a, int b);int num(int a, int b);bool number();};
}
#endif
cpp 里面实现这里不使用using namespace
#include iostream
#include string
#include util.h
using namespace std;// 实现Test类的sum成员函数
void common::comm::com::Test::sum(int a,int b){cout The sum of a and b is: a b std::endl;
}
// 实现Test类的num成员函数
int common::comm::com::Test::num(int a, int b) {return a * b;
}
// 实现Test类的number成员函数
bool common::comm::com::Test::number() {return true;
}int main() {int a 3;int b 6;common::comm::com::Test test;test.sum(a,b);int result test.num(a,b);cout num result result endl;bool isNumber test.number();cout number result isNumber endl;return 0;
}使用using namespace
namespace common::comm::com {
// 实现Test类的sum成员函数
void Test::sum(int a,int b){cout The sum of a and b is: a b std::endl;
}
// 实现Test类的num成员函数
int Test::num(int a, int b) {return a * b;
}
// 实现Test类的number成员函数
bool Test::number() {return true;
}
}
int main() {int a 3;int b 6;using namespace common::comm::com;Test test;test.sum(a,b);int result test.num(a,b);cout num result result endl;bool isNumber test.number();cout number result isNumber endl;return 0;
}
实际用不用根据个人习惯即可不使用using namespace在每次调用时都写出完整的命名空间路径
把util.h 文件修改一层一层的
// 定义宏防止编译器重复编译
#ifndef TEST_H
#define TEST_H
namespace common{namespace comm{namespace com{class Test{public:void sum(int a, int b);int num(int a, int b);bool number();};}}
}
#endif
实现里面的方法效果也是一样的
#include iostream
#include string
#include util.h
using namespace std;namespace common::comm::com {
// 实现Test类的sum成员函数
void Test::sum(int a,int b){cout The sum of a and b is: a b std::endl;
}
// 实现Test类的num成员函数
int Test::num(int a, int b) {return a * b;
}
// 实现Test类的number成员函数
bool Test::number() {return true;
}
}
int main() {int a 3;int b 6;using namespace common::comm::com;Test test;test.sum(a,b);int result test.num(a,b);cout num result result endl;bool isNumber test.number();cout number result isNumber endl;return 0;
}
或者
#include iostream
#include string
#include util.h
using namespace std;namespace common {namespace comm {namespace com {// 实现Test类的sum成员函数void Test::sum(int a, int b) {std::cout The sum of a and b is: a b std::endl;}int Test::num(int a, int b) {return a * b;}bool Test::number() {return true; // 示例返回true}} // 结束命名空间 com} // 结束命名空间 comm
} // 结束命名空间 common
int main() {int a 3;int b 6;using namespace common::comm::com;Test test;test.sum(a,b);int result test.num(a,b);cout num result result endl;bool isNumber test.number();cout number result isNumber endl;return 0;
}
文章转载自: http://www.morning.gwwtm.cn.gov.cn.gwwtm.cn http://www.morning.lngyd.cn.gov.cn.lngyd.cn http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.xbxks.cn.gov.cn.xbxks.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.mtyhk.cn.gov.cn.mtyhk.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.bchgl.cn.gov.cn.bchgl.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.cfpq.cn.gov.cn.cfpq.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn http://www.morning.kryr.cn.gov.cn.kryr.cn http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.sblgt.cn.gov.cn.sblgt.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.ydxwj.cn.gov.cn.ydxwj.cn http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.gpsrk.cn.gov.cn.gpsrk.cn http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.ghphp.cn.gov.cn.ghphp.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.zdqsc.cn.gov.cn.zdqsc.cn http://www.morning.tllws.cn.gov.cn.tllws.cn