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;
        }
};
相关推荐
T - mars几秒前
python爬虫:喜马拉雅登录案例
开发语言·python
末央&14 分钟前
【C++】list底层封装和实现
c++·windows·list
Zhichao_9716 分钟前
【UE5 C++课程系列笔记】35——HTTP基础——HTTP客户端异步请求API接口并解析响应的JSON
c++·ue5
看海的四叔1 小时前
【Python】Python 100题 分类入门练习题 - 新手友好
开发语言·python·分类·数据分析·python学习
阿猿收手吧!1 小时前
【QT】QPixmap QImage QBitmap QPicture
开发语言·c++·qt
攻城狮7号1 小时前
【第39节】windows编程:打造MFC版本任务管理器
c++·windows·mfc·任务管理器
搞程序的心海1 小时前
Scala语言基础:开启你的 Flink 之旅
大数据·开发语言·scala
落笔映浮华丶2 小时前
C++(进阶) 第11智能指针
开发语言·c++
ephemerals__2 小时前
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
开发语言·c++
froginwe112 小时前
Scala Iterator(迭代器)
开发语言