*************
C++
topic: head file
*************
I have some arguments about the head file and call functions. Just take add function as an exaple. I want to program a tool to add two integers.
so the basic add function Structure is here.
函数类型 函数名 (参数类型 参数名, 参数类型 参数名)
{
函数definition
}
Just write the code. The function name can be named as you wish.
int add(int a, int b)
{
return a + b;
}
In a big project, many functions are used. Use Namesapce to avois naming conflicts. Like a school name, namespace limit the function. Juliet and Juliet share the same name. But they go to different school. Namespace is the school name. Juliet is the function name. Overload function breaks the same name limit. So the same function name is legal. So I got two add functions, one is adding inters and the other is adding floats.'
namespace Plus
{
class adder
{
public:
int add(int a, int b)
{
return a + b;
}
float add(float a, float b)
{
return a + b;
}
};
}
call functions come after definiting functions.
if you call add function in adder class, call add function directly.
namespace Plus
{
class adder
{
public:
int add(int a, int b)
{
return a + b;
}
float add(float a, float b)
{
return a + b;
}
// 在类中调用类的成员函数,不需要加上类的命名空间。
void calculate(int a, int b)
{
int result = add(a, b);
cout << "the integer result of add is" << result << endl;
}
void calculate(float a, float b)
{
float result = add(a, b);
cout << "the float result of add is" << result << endl;
}
};
}
more often, the functions are called out of the class. There is still a method to call the functions. Create an object of the class, and then the object call the functions.
call calculate function first:
#include <iostream>
using namespace std;
namespace Plus
{
class adder
{
public:
int add(int a, int b)
{
return a + b;
}
float add(float a, float b)
{
return a + b;
}
// 在类中调用类的成员函数,不需要加上类的命名空间。
void calculate(int a, int b)
{
int result = add(a, b);
cout << "the integer result of add is " << result << endl;
}
void calculate(float a, float b)
{
float result = add(a, b);
cout << "the float result of add is " << result << endl;
}
};
}
int main()
{
Plus::adder adderObject;
adderObject.calculate(13, 38);
adderObject.calculate(13.8f, 83.1f);
return 0;
}
return is here.
call the add functions.
#include <iostream>
using namespace std;
namespace Plus
{
class adder
{
public:
int add(int a, int b)
{
return a + b;
}
float add(float a, float b)
{
return a + b;
}
// 在类中调用类的成员函数,不需要加上类的命名空间。
void calculate(int a, int b)
{
int result = add(a, b);
cout << "the integer result of add is " << result << endl;
}
void calculate(float a, float b)
{
float result = add(a, b);
cout << "the float result of add is " << result << endl;
}
};
}
int main()
{
// 使用calculate函数时,需要加上类的命名空间。
Plus::adder adderObject;
adderObject.calculate(13, 38);
adderObject.calculate(13.8f, 83.1f);
// 使用add函数时,也需要加上类的命名空间。
Plus::adder adderObject2;
int result = adderObject2.add(13, 38);
cout << "the integer result of add is " << result << endl;
float result2 = adderObject2.add(13.8f, 83.1f);
cout << "the float result of add is " << result2 << endl;
return 0;
}
and the return is here.
head file
下面的文字牵扯到很多专业名词,所以使用中文。
假设,你使用商K这段函数。
头文件 就是 服务菜单。菜单中包含列出了所有可用的女公关(函数或类)以及她们能提供的服务(函数接口或类方法)。客户(主函数)通过这个菜单来选择需要的服务。
妈妈桑 就是主函数或者函数的调用者,负责 call 女公关 过来哦。
实现细节 女公关具体如何提供服务(即实现细节)是在幕后进行的,客户不需要知道。同样,在程序中,函数或类的具体实现是在源文件中进行的,头文件只提供声明。
adder.h
#ifndef ADDER_H
#define ADDER_H
#include <iostream>
using namespace std;
namespace Plus {
class adder {
public:
int add(int a, int b);
float add(float a, float b);
void calculate(int a, int b);
void calculate(float a, float b);
};
}
#endif // ADDER_H
main.cpp
#include "Adder.h"
namespace Plus {
int adder::add(int a, int b) {
return a + b;
}
float adder::add(float a, float b) {
return a + b;
}
void adder::calculate(int a, int b) {
int result = add(a, b);
cout << "the integer result of add is " << result << endl;
}
void adder::calculate(float a, float b) {
float result = add(a, b);
cout << "the float result of add is " << result << endl;
}
}
int main() {
Plus::adder adderObject;
adderObject.calculate(13, 38);
adderObject.calculate(13.8f, 83.1f);
Plus::adder adderObject2;
int result = adderObject2.add(13, 38);
cout << "the integer result of add is " << result << endl;
float result2 = adderObject2.add(13.8f, 83.1f);
cout << "the float result of add is " << result2 << endl;
return 0;
}
编译后,输出
the integer result of add is 51
the float result of add is 96.9
the integer result of add is 51
the float result of add is 96.9