[精讚] [會員登入]
377

【C++】一些好用的C++小功能 —— 貳

承襲上一篇的C++小功能,筆記下來以免自己以後忘記了。

分享此文連結 //n.sfs.tw/14954

分享連結 【C++】一些好用的C++小功能 —— 貳@小編過路君子
(文章歡迎轉載,務必尊重版權註明連結來源)
2021-03-13 16:39:02 最後編修
2021-03-02 23:15:06 By 過路君子
 

哈囉大家好,這裡是筆記了一堆用不到程式的小編過路君子

怕哪天自己要用找不到,特此記錄下來,一些比較稀奇的用法

 

上一篇的筆記在這裡!基本上跟這一篇一樣!

大標題、完整程式碼、如果有輸出會附上輸出,基本上等於0的講解!

希望能在這些非常雜亂的文章裡面找到你所需要的小功能,就算不會用看過至少也有個C++能這樣寫的概念吧~

 

以指標的形式呼叫函數

#include<iostream>

using namespace std;

void out(int);
void add(int, int, void (*op)(int));	//一定要先宣告

int main()
{
	int a = 9, b = 6;
	add(a, b, out);
	
	return 0;
}

void add(int ui, int ux, void (*op)(int br))
{
	ui += ux;
	(*op)(ui);
}

void out(int xc)
{
	cout << xc;
}

輸出

15

 

呼叫被區域變數遮蔽的全域變數

#include<iostream>
#include<string>
using namespace std;

string apple("yummy!\n");

int main()
{
	string apple("yuck!\n");
	cout << "Apple is so " << apple;
	cout << "Umm...no, is so " << ::apple;
	return 0;
}

輸出

Apple is so yuck!
Umm...no, is so yummy!

 

建立區域靜態變數
~再次呼叫副程式時不會被初始化

#include<iostream>
using namespace std;

void call(void);

int main()
{
	for(int ui=0;4>ui;ui++) call();
	return 0;
}

void call(void)
{
	static int a = 0;
	cout << "Now number is " << a << '\n';
	a += 2;
}

輸出

Now number is 0
Now number is 2
Now number is 4
Now number is 6

 

函數重載和呼叫、初始化隱藏於class內成員

#include<iostream>
#include<string>
using namespace std;

class vov
{
private:
	string ui;
	int ux;

public:
	vov(string);
	void out(void);
	void out(int);
};

int main()
{
	vov Alice("Yale");	//函數初始化
	Alice.out();
	Alice.out(9);		//函數重載
	return 0;
}

vov::vov(string init)
{
	this->ui = init;
	this->ux = 6491;
}

void vov::out(void)
{
	cout << this->ui << ' ' << this->ux << '\n';
}

void vov::out(int add)
{
	this->ux += add;
	cout << this->ui << ' ' << this->ux << '\n';
}

輸出

Yale 6491
Yale 6500

 

使所有的class共用某項變數

#include<iostream>
using namespace std;

class test
{
public:
	static int ux;
};

int test::ux;	//一定要宣告!!! 

int main()
{
	test op;
	op.ux = 777;
	
	test some;	//宣告新的test物件
	cout << some.ux << '\n';
	//宣告於class內的static (type)可以想像是存在於這個class內的全域變數
	return 0;
}

輸出

777

 

class物件之間的轉換

#include<iostream>
#include<string>

using namespace std;

class reset
{
private:
	string game;
	int number;
	
public:
	reset(string);
	
	void SetNumber(int);
	
	operator int() const;
	operator string() const;
};

int main()
{
	reset kill("prayer");
	kill.SetNumber(777);
	
	int a = 23;
	a += kill;
	cout << a << '\n';

	string b = kill;
	cout << b << '\n';
	
	return 0;
}

reset::reset(string gameName)
{
	this->game = gameName;
	this->number = 0;
}

void reset::SetNumber(int num)
{
	this->number = num;
}

reset::operator int() const
{
	return this->number;
}

reset::operator string() const
{
	string information = "\'s game";
	return this->game + information;
}

輸出

800
prayer's game

 

建構複製運算子

#include<iostream>
using namespace std;
 
class reset
{
private:
    int *number;
     
public:
    reset(int *);
    reset(const reset *);	//複製建構子

	void out(void);   
};
 
int main()
{
	int *x = new int[5];
	for(int a=0, b=2;5>a;a++,b+=9) x[a] = b;
    reset *kill = new reset(x);
	delete [] x;

	reset pray(kill);
	delete kill;	//證明pray的number不是指向kill的number
	pray.out();
     
    return 0;
}
 
reset::reset(int *nums)
{
    this->number = new int[5];
	for(int a=0;5>a;a++) this->number[a] = nums[a];
}

reset::reset(const reset *oldReset)
{
	this->number = new int[5];
	for(int a=0;5>a;a++) this->number[a] = oldReset->number[a];
}

void reset::out(void)
{
	for(int a=0;5>a;a++) cout << this->number[a] << ' ';
}

輸出

2 11 20 29 38

 

END

你可能感興趣的文章

【Wicket】[Header]如何讀取來自客戶端地檔頭和傳送自訂擋頭至客戶端 當需要設定檔頭或是讀取來自客戶端的檔頭時,這些程式碼就很好用

【C】(%c, %d)解決讀取字元時的緩衝區殘留 不解決就會莫名其妙地冒出一些莫名其妙的東西

【教程】(進階版)如何用Sigil製作一本高質量的epub 下載好了Sigil之後除了直接把文字貼進去以外,還有:變更字型、著色、導入CSS……等等功能,不知道你有沒有發現呢?

【Maven】如何夾帶檔案至Jar內以及其讀取方式 想要讀取一個外部的文字文件?Maven是你的好幫手

【Wicket】[CSP] Content-Security-Policy & Content-Security-Policy-Report-Only Wicket 預設開啟的,所以如果要加載外部資源甚至是同源資源都會被擋下。

【C++】class練習 — 檢測該字串是否為迴圈 第一次的C++結構式寫法,雖然以後應該會見怪不怪,但畢竟是第一次所以還是想保存下來

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

高捷少女:購票大作戰① 「各位乘客,本班機即將降落,感謝各位乘客的搭乘……」隨著空中小姐的廣播音,那架飛機逐漸降落在地面,裡面的乘客們也紛紛開始整理自己的行李。 那個有著歐美人五官的少女抓緊包包,看著外面的小港機場,臉上緩緩

高捷少女:美麗島的守護者⑤  雖然暫時不用怕牠們了,可是一直躲在這裡終究不是辦法,小雅心想。她看看四周,這間更衣室沒有窗戶或後門,她不禁著急起來,不可能一直躲在這裡面,但從門出去只會被群貓圍攻而已。小雅低下頭苦思該怎麼辦,過了不

高捷少女:耐耐的新年驚喜① 耐耐拿出手機。「我回來囉。」她說。幾分鐘後,木門緩緩打開。當它完全開啟的那一刻,小穹手中的包包掉到地上;艾米揉揉雙眼,確定自己看見的景象;婕兒的三魂七魄飛到了九霄雲外。

婕兒──她的青春② 艾米直搖頭。「我真不敢相信,小穹妳都二十幾歲了,為什麼能想出這種故事呀?」「婕兒也是二十幾歲啊,妳想想她現在是什麼樣子?」小穹不滿地指向火車的方向。

婕兒──她的青春④ 「投降吧,耐耐!這回合妳將不會再有獲勝的機會了!哈哈哈哈!」 「妳確定?」耐耐臉上泛起一絲微笑,並將手中的牌展示給婕兒看 婕兒的笑容僵住了。恐懼浮現在她的臉。