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 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+示例

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540999.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540999.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540999.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

RabbitMQ安裝|使用|概念|Golang開發

手冊:http://www.rabbitmq.com/getstarted.html 安裝:http://www.rabbitmq.com/download.html 參考&#xff1a;http://blog.csdn.net/whycold/article/details/41119807 一.介紹 AMQP&#xff0c;即Advanced Message Queuing Protocol&#xff0c;高級消息隊列協議&#xff0c…

python中的替換函數_python:替換模塊類中的函數

我試圖替換類中定義的函數,以便在不更改實際代碼的情況下修改其函數(如內部工作)。我以前從來沒有這樣做過,因此在更換它時遇到一些問題。更改代碼會讓我訪問python庫中的包,這不是一個很好的選擇。例如,如果模塊名為testmodclass testMOD(object):def testFunc(self, variable…

諾基亞AirScale支持低頻段和高頻段5G服務 確保運營商投資收入

據悉&#xff0c;諾基亞創新和測試強調了其AirScale無線產品組合的靈活性、可升級性和可擴展性&#xff0c;以適應技術初始應用中實現的5G頻段。 通過證明AirScale能夠支持低頻和高頻&#xff0c;運營商將能夠從5G推出的第一天提供廣泛的覆蓋和室內覆蓋&#xff0c;而無需進行復…

MS的完整形式是什么?

碩士&#xff1a;理學碩士/外科碩士/ MicroSoft (MS: Master of Science / Master of Surgery / MicroSoft) 1)碩士&#xff1a;理學碩士 (1) MS: Master of Science) MS is an abbreviation of Master of Science. It is a masters degree program provided by universities i…

java 線程安全的linkedlist_使ArrayList,LinkedList變成線程安全的

1.使用SynchronizedListSynchronizedList是一個線程安全的包裝類。繼承于SynchronizedCollection&#xff0c;SynchronizedCollection實現了Collection接口&#xff0c;SynchronizedList包含一個List對象&#xff0c;對List的訪問修改方法進行了一些封裝&#xff0c;在封裝的方…

人工智能能夠構建一個自主驅動云嗎?

企業和組織可以從云計算中受益&#xff0c;但許多公司并不希望面對公共云的成本&#xff0c;性能和治理問題&#xff0c;并且認為構建自己的私有云的復雜性和運營開銷并沒有那么困難。 如今&#xff0c;一些云計算供應商正在使用人工智能&#xff08;AI&#xff09;來簡化私有云…

scala中def_def關鍵字以及Scala中的示例

scala中defScala def關鍵字 (Scala def keyword) The def keyword in Scala is used to declare functions and methods in Scala. Scala being ignorant on the data types does the same with the return type of a function. Declaring and defining a function in Scala do…

前端必備的 web 安全知識手記

前言 安全這種東西就是不發生則已&#xff0c;一發生則驚人。作為前端&#xff0c;平時對這方面的知識沒啥研究&#xff0c;最近了解了下&#xff0c;特此沉淀。文章內容包括以下幾個典型的 web 安全知識點&#xff1a;XSS、CSRF、點擊劫持、SQL 注入和上傳問題等&#xff08;…

php腳本開頭注釋_PHP文件注釋標記及規范小結

PHP文件注釋標記及規范小結發布時間&#xff1a;2016-06-17 來源&#xff1a; 點擊:次PHP 注釋標記access使用范圍&#xff1a;class,function,var,define,module該標記用于指明關鍵字的存取權限&#xff1a;private、public或protecedauthor指明作者copyright使用范圍&#xf…

Salesforce宣布5.82億美元收購文件編輯公司Quip

北京時間8月2日消息&#xff0c;據路透社報道&#xff0c;云軟件開發商Salesforce.com周一宣布&#xff0c;已同意以大約5.82億美元收購文件編輯創業公司Quip。 Salesforce此前已經對Quip進行了投資。Quip開發的文字處理平臺供企業員工用于在移動設備、可穿戴設備以及臺式機上編…

Java中的main()方法是強制性的嗎?

The question is that "Is main() method is compulsory in Java?" 問題是“ main()方法在Java中是強制性的嗎&#xff1f;” Yes, we can write a java program without main() method but there is a condition if and only if java JDK version till JDK 5. 是的…

php date( w ),PHP Date()函數詳解

頁面的最上方加上&#xff1a;date_default_timezone_set(PRC); /*把時間調到北京時間,php5默認為格林威治標準時間*/date ()a: "am"或是"pm"A: "AM"或是"PM"d: 幾日&#xff0c;兩位數字&#xff0c;若不足則補零&#xff1b…

10.6-全棧Java筆記:常見流詳解(四)

上節我們講到「Java中常用流&#xff1a;數據流和對象流」&#xff0c;本節我們學習文件字符流和文件緩沖流~文件字符流前面介紹的文件字節流可以處理所有的文件&#xff0c;但是字節流不能很好的處理Unicode字符&#xff0c;經常會出現“亂碼”現象。所以&#xff0c;我們處理…

python 示例_帶有示例的Python File open()方法

python 示例文件open()方法 (File open() Method) open() method is an inbuilt method in Python, it is used to create, open or append a file. open()方法是Python中的內置方法&#xff0c;用于創建&#xff0c;打開或附加文件。 Syntax: 句法&#xff1a; file_object …

php屬于腳本,php是腳本語言嗎

PHP即“超文本預處理器”&#xff0c;是一種通用開源腳本語言。PHP是在服務器端執行的腳本語言&#xff0c;與C語言類似&#xff0c;是常用的網站編程語言。PHP獨特的語法混合了C、Java、Perl以及 PHP 自創的語法。利于學習&#xff0c;使用廣泛&#xff0c;主要適用于Web開發領…

NetMarketShare:本月桌面瀏覽器市場份額幾乎沒有變化

NetMarketShare之前關于臺式機瀏覽器市場份額的報告表示&#xff0c;Google Chrome市場份額正在快速上升&#xff0c;而Edge瀏覽器市場份額正在以蝸牛的速度前進。而該公司的最新統計數據顯示&#xff0c;幾乎所有瀏覽器的市場份額或多或少保持不變。 NetMarketShare的最新統計…

treeset java_Java TreeSet add()方法與示例

treeset javaTreeSet類的add()方法 (TreeSet Class add() method) add() method is available in java.util package. add()方法在java.util包中可用。 add() method is used to add the given object(ob) to this TreeSet when it does not already exist otherwise it ignore…

php fpm www.conf,PHP7中php.ini、php-fpm和www.conf 配置

PHP7中php.ini、php-fpm和www.conf 配置php.ini是php運行核心配置文件,下面是一些常用配置extension_dir""● 設置PHP的擴展庫路徑expose_php Off● 避免PHP信息暴露在http頭中display_errors Off● 避免暴露php調用mysql的錯誤信息log_errors On● 在關閉display…

服務器電流源泉ups電源的三大形式

還記得此前12306官網癱瘓&#xff0c;回家心切急于購票的我們只能感到無比心累。雙十一前夕&#xff0c;守在購物車邊準備瘋狂購物的剁手黨們&#xff0c;遇到一直呈現加載狀態的頁面&#xff0c;不得不感嘆想要做馬云背后的那個人也要大費周折。作為一個資深網民&#xff0c;不…

timer purge_Java Timer purge()方法與示例

timer purge計時器類purge()方法 (Timer Class purge() method) purge() method is available in java.util package. purge()方法在java.util包中可用。 purge() method is used to remove all canceled tasks from the task queue of this Timer. purge()方法用于從此Timer的…