c++ cdi+示例

C ++中的本地类 (Local Class in C++)

In C++, generally a class is declared outside of the main() function, which is global for the program and all functions can access that class i.e. the scope of such class is global.

在C ++中,通常在main()函数外部声明一个类,该类对于程序是全局的,并且所有函数都可以访问该类,即此类的范围是全局的。

A local class is declared inside any function (including main() i.e. we can also declare a class within the main() function) and the scope of local class is local to that function only i.e. a local class is accessible within the same function only in which class is declared.

本地类在任何函数内声明(包括main(),即我们也可以在main()函数内声明一个类),并且本地类的范围仅对该函数本地,即本地类只能在同一函数内访问在哪个类中声明。

Example:

例:

Here, we are declaring and defining two classes "Test1" and "Test2", "Test1" is declared inside a user-defined function named testFunction() and "Test2" is declares inside the main() function.

在这里,我们声明并定义了两个类“ Test1”“ Test2” ,在一个名为testFunction()的用户定义函数中声明了“ Test1”,在main()函数中声明了“ Test2”

Since classes "Test1" and "Test2" are declared within the functions, thus, their scope will be local to those functions. Hence, "Test1" and "Test2" are local classes in C++.

由于在函数中声明了“ Test1”“ Test2”类,因此它们的作用域对于这些函数而言是局部的。 因此, “ Test1”“ Test2”C ++中的本地类

Program:

程序:

#include <iostream>
using namespace std;
//A user defined function
void testFunction(void)
{//declaring a local class
//which is accessible within this function only
class Test1
{private:
int num;
public:
void setValue(int n)
{num = n;
}
int getValue(void)
{return num;
}
};
//any message of the function
cout<<"Inside testFunction..."<<endl;
//creating class's object
Test1 T1;
T1.setValue(100);
cout<<"Value of Test1's num: "<<T1.getValue()<<endl;
}
//Main function
int main()
{//declaring a local class
//which is accessible within this function only
class Test2
{private:
int num;
public:
void setValue(int n)
{num = n;
}
int getValue(void)
{return num;
}
};
//calling testFunction
cout<<"Calling testFunction..."<<endl;
testFunction();
//any message of the function
cout<<"Inside main()..."<<endl;
//creating class's object
Test2 T2;
T2.setValue(200);
cout<<"Value of Test2's num: "<<T2.getValue()<<endl;
return 0;
}

Output

输出量

Calling testFunction...
Inside testFunction...
Value of Test1's num: 100
Inside main()...
Value of Test2's num: 200

翻译自: https://www.includehelp.com/cpp-programs/local-class-with-example.aspx

c++ cdi+示例

c++ cdi+示例_C ++中带有示例的本地类相关推荐

  1. c语言 函数的参数传递示例_C ++中带有示例的nearint()函数

    c语言 函数的参数传递示例 C ++附近的int()函数 (C++ nearbyint() function) nearbyint() function is a library function o ...

  2. python 示例_Python中带有示例的关键字除外

    python 示例 Python关键字除外 (Python except keyword) except is a keyword (case-sensitive) in python, it is ...

  3. c语言atoll函数怎么用_C ++中带有示例的atoll()函数

    c语言atoll函数怎么用 C ++ Atoll()函数 (C++ atoll() function) atoll() function is a library function of cstdli ...

  4. nanf flash校验_C ++中带有示例的nanf()函数

    nanf flash校验 C ++ Nanf()函数 (C++ nanf() function) nanf() function is a library function of cmath head ...

  5. c++ abort 函数_C ++中带有示例的abort()函数

    c++ abort 函数 C ++ abort()函数 (C++ abort() function) abort() function is a library function of cstdlib ...

  6. python 示例_Python中带有示例的class关键字

    python 示例 Python类关键字 (Python class keyword) class is a keyword (case-sensitive) in python, it is use ...

  7. ruby hash方法_Ruby中带有示例的Hash.rehash方法

    ruby hash方法 Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Meth ...

  8. java中的类跟结构体_C#中的结构体与类的区别

    经常听到有朋友在讨论C#中的结构与类有什么区别.正好这几日闲来无事,自己总结一下,希望大家指点. 1. 首先是语法定义上的区别啦,这个就不用多说了.定义类使用关键字class 定义结构使用关键字str ...

  9. c语言 函数的参数传递示例_C语言中带有示例的remove()函数

    c语言 函数的参数传递示例 C语言中的remove()函数 (remove() function in C) The remove() function is defined in the <s ...

最新文章

  1. ssh在dhcp自动获取地址的时候使用
  2. 进程的用户栈和内核栈
  3. MS12_020漏洞
  4. 160. 相交链表 golang
  5. Dungeon Master——BFS
  6. weblogic启动服务报错
  7. 微软:中国市场XP主流支持服务结束时间未定
  8. 2天完成17TB数据量迁移,华为云数据库是如何做的?
  9. python 控制语句
  10. SequoiaDB版本升级及导入导出工具说明
  11. zbb20170919 设置eclipse myeclipse 在工程窗口 项目目录 显示代码错误提示的
  12. 三星S7edge刷极光ROM的总结_我是亲民_新浪博客
  13. Verilog语言乒乓球机8段译码器
  14. ORACLE SQL 优化的若干方法(详细)
  15. node-sails后台搭建
  16. python气象卫星云图解析_python下载卫星云图合成gif的方法示例
  17. 旧弹簧(Old spring )
  18. picker使用以及注意事项
  19. 电子秤方案介绍——体脂测量功能
  20. 【JavaSE与数据结构】栈与Stack类

热门文章

  1. Linux中g++与gcc的区别
  2. linux 系统调用时怎么知道当前上下文属于那个进程,linux – 编写系统调用来计算进程的上下文切换...
  3. matlab与maple互联,Matlab,Maple和Mathematica三款主流科学计算软件的互操作
  4. orcle mysql 查询_Oracle与Mysql的高级查询与难点sql
  5. 锁定计算机在哪里设置方法,怎样锁定电脑?如何设置电脑锁屏【步骤详解】
  6. java日历事件处理_日历表的事件处理和管理(刘静)
  7. HTML下拉菜单怎么做成横向,css导航条横向带下拉菜单
  8. 线程停止继续_线程不是你想中断就能中断
  9. Git 的安装、使用、分支、命令 一篇博客全都学会
  10. Linux 修改用户名的主目录 家目录