[精讚] [會員登入]
711

【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

你可能感興趣的文章

【JDA/discord bot】如何獲得訊息中的圖片和影片並儲存或轉傳 如何將訊息中的圖片或影片之類的多媒體提取出來之後,再進行相關的處理

【Discord bot】[botton]按鈕的使用、響應和關閉 Discord的botton通常都要和View配合使用。

【Wildfly/jBoss】[Linux](Connection Datasource)如何與MySQL資料庫建立連線 網頁瀏覽器和資料庫的關係密不可分,而通常會將兩者分開架設在不同的伺服器上面來提供服務,這時要如何進行連線呢?

【MySQL】每個類別中取前三高 一個由多層次所組成的SELECT述句

【Maven】如何創建一個簡單可執行的JAR檔 滿重要的一個小功能,畢竟有時候是要傳遞的是JAR檔,而不是直接透過Maven直接部屬之類的

【Wildfly/jBoss】[Linux] 如何從無到有建立Wildfly網頁伺服器 包含一切所需的指令

隨機好文

高捷少女:地下城的探險少女④ 耐耐突然抖了一下。「妳們聽到了嗎?」她說。「聽到什麼?」婕兒問。「那個腳步聲啊!」耐耐嚥了一下喉嚨,覺得有些害怕。「有一陣腳步聲經過,很小聲,但我還是聽到了。』「妳聽錯了吧……等等!」婕兒使終維持著將

【數學】徐氏數學簡明講義(三) 第二章 直線與園 P2.1-17 Q26 26.三角形的兩邊分別在二直線 x-3y+10=0 , 2x-y-8=0 上,且知第三邊中點為(3 , 2),求第三邊所

【2018回顧】過路君子之家所經歷的點點滴滴 紀錄過路君子之家在2018年所有的活動紀錄!

【英翻中歌詞】(二創歌)少女秘封俱樂部 Out there past the stars... that world is ours, once lost to history.  (Don’t you know?~) 在星輝之外...那屬

【英翻中歌詞】(二創歌)Reverse Ideology ˙˙˙punoɹɐ ןןɐ ʇı uɹnʇ ‘punoɹɐ ןןɐ ʇı uɹnʇ ...吧切一轉翻,吧切一