C++中函数的调用

devtools/2025/2/23 3:11:05/

*************

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.ppmy.cn/devtools/159117.html

相关文章

vivo手机和Windows电脑连接同一个WiFi即可投屏!

虽然现在很多人喜欢刷手机&#xff0c;但是对于长时间需要使用手机办公的人来说&#xff0c;手机屏幕还是太小了&#xff0c;当人一天二十四小时中要花费近十个小时摆弄手机&#xff0c;就会渴望手机屏幕能够大一点&#xff0c;至少看的时候&#xff0c;眼睛舒服一点。 因为嫌弃…

域名劫持原理与实践

了解域名及域名劫持 由于点分十进制的IP地址难于记忆&#xff0c;便出现了域名。由于网络传输中最终还是基于IP&#xff0c;所以必须通过一种机制将IP和域名一一对应起来&#xff0c;这便是DNS。全球总共有13台根域名服务器。 域名劫持是互联网攻击中常见的一种攻击方式&…

【Linux】进程间关系与守护进程

文章目录 1. 进程组2. 会话2.1 什么是会话2.2 如何创建会话2.3 守护进程 3. 作业控制 1. 进程组 我们运行下面的命令 sleep 10000 | sleep 20000 | sleep 30000然后查看进程的信息&#xff1a; 可以看到&#xff0c;其实每一个进程除了有进程PID、PPID之外&#xff0c;还属于…

【做一个微信小程序】校园地图页面实现

前言 上一个教程我们实现了小程序的一些的功能&#xff0c;有背景渐变色&#xff0c;发布功能有的呢&#xff0c;已支持图片上传功能&#xff0c;表情和投票功能开发中&#xff08;请期待&#xff09;。下面是一个更高级的微信小程序实现&#xff0c;包含以下功能&#xff1a;…

6.appender

文章目录 一、前言二、源码解析AppenderUnsynchronizedAppenderBaseOutputStreamAppenderConsoleAppenderFileAppenderRollingFileAppenderFileNamePattern 三、总结 一、前言 前一篇文章介绍了appender、conversionRule、root和logger节点的解析, 为的是为本篇详细介绍它们的…

Spring 项目接入 DeepSeek,分享两种超简单的方式!

⭐自荐一个非常不错的开源 Java 面试指南&#xff1a;JavaGuide &#xff08;Github 收获148k Star&#xff09;。这是我在大三开始准备秋招面试的时候创建的&#xff0c;目前已经持续维护 6 年多了&#xff0c;累计提交了 5600 commit &#xff0c;共有 550 多位贡献者共同参与…

ubuntu和windows编译godot

官方文档-编译教程 https://docs.godotengine.org/zh-cn/4.x/contributing/development/compiling/compiling_for_windows.html源码地址 本示例使用4.3-stable版本 https://github.com/godotengine/godot/archive/refs/heads/master.zippython; nasm; mingw下载安装包地址 h…

DeepSeek本地化部署【window下安装】【linux下安装】

一、window 本地安装指导 1.1、下载window安装包 https://ollama.com/download/OllamaSetup.exe 1.2、点击下载好的安装包进行安装 检测安装是否成功&#xff1a; C:\Users\admin>ollama -v ollama version is 0.5.7有上面的输出&#xff0c;则证明已经安装成功。 配置…