日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ cdi+示例_C ++中带有示例的本地类

發布時間:2025/3/11 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ cdi+示例_C ++中带有示例的本地类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 onlyclass Test1{private:int num;public:void setValue(int n){num = n;}int getValue(void){return num;}};//any message of the functioncout<<"Inside testFunction..."<<endl;//creating class's objectTest1 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 onlyclass Test2{private:int num;public:void setValue(int n){num = n;}int getValue(void){return num;}};//calling testFunctioncout<<"Calling testFunction..."<<endl;testFunction();//any message of the functioncout<<"Inside main()..."<<endl;//creating class's objectTest2 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 ++中带有示例的本地类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。