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;
        }
};
相关推荐
fox_lht10 分钟前
8.3.使用if let和let else实现简明的程序流控制
开发语言·后端·算法·rust
AI玫瑰助手16 分钟前
Python基础:列表的定义、增删改查核心操作
android·开发语言·python
mOok ONSC17 分钟前
对基因列表中批量的基因进行GO和KEGG注释
开发语言·数据库·golang
磊 子25 分钟前
类模板与派生1
java·开发语言·c++
:12128 分钟前
java面试基础2
java·开发语言·面试
我是无敌小恐龙1 小时前
Java SE 零基础入门Day03 数组核心详解(定义+内存+遍历+算法+实战案例)
java·开发语言·数据结构·人工智能·算法·aigc·动态规划
甘露寺1 小时前
深入理解并发模型:从 Python 的 async/await 到 Java 的虚拟线程与线程池机制
java·开发语言·网络
郝学胜-神的一滴1 小时前
深入理解 epoll_wait:高性能 IO 多路复用核心解密
linux·服务器·开发语言·c++·网络协议
HAWK eoni1 小时前
java进阶1——JVM
java·开发语言·jvm
c++之路1 小时前
C++ 面向对象编程(OOP)
开发语言·c++