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;
        }
};
相关推荐
全栈软件开发1 分钟前
PHP域名授权系统网站源码_授权管理工单系统_精美UI_附教程
开发语言·ui·php·php域名授权·授权系统网站源码
誰能久伴不乏6 分钟前
Qt 动态属性(Dynamic Property)详解
开发语言·qt
枫叶丹41 小时前
【Qt开发】常用控件(四)
开发语言·qt
草莓熊Lotso1 小时前
《吃透 C++ 类和对象(中):const 成员函数与取地址运算符重载解析》
c语言·开发语言·c++·笔记·其他
weixin_307779131 小时前
VS Code配置MinGW64编译Ipopt库
开发语言·c++·vscode·算法
Tadas-Gao1 小时前
Java设计模式全景解析:从演进历程到创新实践
java·开发语言·微服务·设计模式·云原生·架构·系统架构
anlogic2 小时前
Java基础 8.16
java·开发语言
蚰蜒螟3 小时前
Netty 的 Select/Poll 机制核心实现主要在 NioEventLoop 的事件循环
java·开发语言
野生的编程萌新3 小时前
从冒泡到快速排序:探索经典排序算法的奥秘(二)
c语言·开发语言·数据结构·c++·算法·排序算法
Full Stack Developme3 小时前
Java后台生成多个Excel并用Zip打包下载
java·开发语言·excel