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;
        }
};
相关推荐
似璟如你12 分钟前
Java开发八股文之基础篇+spring+集合
java·开发语言·面试
本杰明15214 分钟前
2025/7/14——java学习总结
java·开发语言·学习
2345VOR19 分钟前
【C#地图显示教程:实现鼠标绘制图形操作】
开发语言·c#·计算机外设·地图显示鼠标交互
开开心心_Every25 分钟前
可增添功能的鼠标右键优化工具
开发语言·pdf·c#·计算机外设·电脑·音视频·symfony
洛克希德马丁25 分钟前
QTableView鼠标双击先触发单击信号
c++·qt
星释25 分钟前
优雅的Java:01.数据更新如何更优雅
java·开发语言·spring boot
The_era_achievs_hero26 分钟前
微信131~140
开发语言·javascript·微信
LeonYangRyeon27 分钟前
解锁SQL“密码”:SELECT DISTINCT END AS的深度剖析与实战指南
java·开发语言
ysa05103033 分钟前
竞赛常用加速技巧#模板
c++·笔记·算法
exm-zem1 小时前
多用户图书管理系统
c++