当前位置: 首页 > article >正文

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;
        }
};


http://www.kler.cn/a/542480.html

相关文章:

  • HarmonyOS 5.0应用开发——NodeContainer自定义占位节点
  • (定时器,绘制事件,qt简单服务器的搭建)2025.2.11
  • 如何免费白嫖 Deepseek API 接口
  • 修复缺失的tobii.gameintegration.dll文件,让游戏更顺畅
  • 鸿蒙harmony 手势密码
  • 如何把邮件批量导出到本地
  • 机器学习 - 进一步理解最大似然估计和高斯分布的关系
  • kafka服务端之日志磁盘存储
  • 从零开始设计一个完整的网站:HTML、CSS、PHP、MySQL 和 JavaScript 实战教程
  • 如何评估云原生GenAI应用开发中的安全风险(下)
  • MySQL 中可以通过添加主键来节省磁盘空间吗?(译文)
  • jQuery UI 下载指南
  • 腾讯云HAI部署DeepSeek结合Ollama API搭建智能对话系统
  • [QMT量化交易小白入门]-二十二、deepseek+cline+vscode,让小白使用miniQMT量化交易成为可能
  • MR30分布式IO模块:驱动智能制造工厂的工业互联与高效控制新范式
  • React进行路由跳转的方法汇总
  • 在 Qt 开发中,可以将 QML 封装成库
  • 基于Springmvc+MyBatis+Spring+Bootstrap+EasyUI+Mysql的个人博客系统
  • JVM的栈里面存的是栈帧,栈帧里面存的是什么?
  • Unity底层C#处理机制深度解析
  • eBPF入门教程(Ubuntu 24.04)
  • JavaScript设计模式 -- 工厂模式
  • 五、OSG学习笔记-矩阵变换
  • 25考研材料复试面试常见核心问题真题汇总,材料考研复试面试有哪些经典问题?材料考研复试过程最看重什么内容?
  • python C# 内存共享互传 图像 以及字符串
  • 蓝桥杯 Java B 组 - 第 1 周复习总结