C++中函数的调用
*************
C++
topic:call functions
*************
Have some busy works recently. I have a doubts the call functions.
first take add two integers function as an example. The function is written as the form.
函数类型 函数名(参数类型 参数名)
{
函数体
}
int add(int a, int b)
{
return a + b;
}
Then make a class named calculator and a namespace named jisuanqi.
namespace jisuanqi
{
class Calculator
{
int add(int a, int b)
{
return a + b;
}
};
}
There is a member function, named anotherFunction, in the same calculator class.
#include <iostream>
namespace jisuanqi
{
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
void anotherFunction(void)
{
int result = add(13, 38);
cout << "another function" << endl;
}
};
If use add function in the other class, need do something in the otherfunction. Make an object of the calculator class. then the object call the add cunction. Pay special attention to
#include <iostream>
namespace jisuanqi
{
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
void anotherFunction(void)
{
int result = add(13, 38);
cout << "another function" << endl;
}
};
class Calculator2
{
public:
void useAddFunction(void)
{
Calculator simpleObject;
int result = simpleObject.add(13, 38);
cout << "the result of add is" << result << endl;
}
};
}
If the calculator2 class in the outside of the namespace, add the spacename is fine.
#include <iostream>
namespace jisuanqi
{
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
void anotherFunction(void)
{
int result = add(13, 38);
cout << "another function" << endl;
}
};
class Calculator2
{
public:
void useAddFunction(void)
{
Calculator simpleObject;
int result = simpleObject.add(13, 38);
cout << "the result of add is" << result << endl;
}
};
}
class outsideNamespace
{
public:
void useAddFunction(void)
{
jisuanqi::Calculator simpleObject;
int result = simpleObject.add(13, 38);
cout << "the result of add is" << result << endl;
}
};