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;
        }
};
相关推荐
L_09072 分钟前
【C++】高阶数据结构 -- 红黑树
数据结构·c++
lly20240623 分钟前
C 语言中的结构体
开发语言
JAVA+C语言40 分钟前
如何优化 Java 多主机通信的性能?
java·开发语言·php
青岑CTF2 小时前
攻防世界-Ics-05-胎教版wp
开发语言·安全·web安全·网络安全·php
Li emily2 小时前
如何通过外汇API平台快速实现实时数据接入?
开发语言·python·api·fastapi·美股
APIshop2 小时前
Java 实战:调用 item_search_tmall 按关键词搜索天猫商品
java·开发语言·数据库
血小板要健康3 小时前
Java基础常见面试题复习合集1
java·开发语言·经验分享·笔记·面试·学习方法
淼淼7633 小时前
安装jdk1.8
java·开发语言
PfCoder3 小时前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform
Legendary_0083 小时前
Type-C 一拖二快充线:突破单口限制的技术逻辑
c语言·开发语言