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.

cpp 复制代码
函数类型 函数名(参数类型 参数名)
{
    函数体
}

int add(int a, int b)
{
    return a + b;
}

Then make a class named calculator and a namespace named jisuanqi.

cpp 复制代码
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.

cpp 复制代码
#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

cpp 复制代码
#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.

cpp 复制代码
#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;
        }
};
相关推荐
于是我说32 分钟前
前端JavaScript 项目中 获取当前页面滚动位置
开发语言·前端·javascript
27399202934 分钟前
QT5使用QFtp
开发语言·qt
怪力左手1 小时前
qt qspinbox editingfinished事件问题
开发语言·qt
waper971 小时前
nohup java -jar启动jar包错报错 地址已在使用
java·开发语言·jar
我喜欢就喜欢1 小时前
2025技术成长复盘:解决问题的365天
c++·qt
神仙别闹1 小时前
基于QT(C++)+MySQL实现(窗体)学生信息管理系统
c++·qt·mysql
沐知全栈开发1 小时前
ASP 实例:深入浅出地了解ASP技术
开发语言
待╮續1 小时前
JVMS (JDK Version Manager) 使用教程
java·开发语言
龘龍龙1 小时前
Python基础学习(四)
开发语言·python·学习
U-52184F692 小时前
C++ 实战:构建通用的层次化数据模型 (Hierarchical Data Model)
开发语言·c++