本篇內(nèi)容主要講解“C++中怎么創(chuàng)建CryptoCurrency類”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“C++中怎么創(chuàng)建CryptoCurrency類”吧!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請域名、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、嵊州網(wǎng)站維護(hù)、網(wǎng)站推廣。
// @url: https://repl.it/@MrToph/CPPBasics-Classes-1 #include <iostream> #include <string> #include <stdlib.h> //自動解析std命名空間,所以我們可以寫字符串而不是std::string using namespace std; //聲明一個類。 //類通常在頭文件(.h或.hpp)中聲明。 class Currency { //默認(rèn)情況下,成員變量和函數(shù)是私有的。 string name; double priceInUSD; //此后的所有成員都是公共的 //直到找到“private:”或“protected:”。 public: //默認(rèn)構(gòu)造函數(shù) Currency(); //另一個帶兩個參數(shù)的構(gòu)造函數(shù) Currency(const string &_name, const double price); //成員函數(shù)聲明(要遵循的實(shí)現(xiàn)) void setName(const string &dogsName); void setPrice(double price); //不修改對象狀態(tài)的函數(shù)應(yīng)該標(biāo)記為const。 //如果給定對象的const引用,則允許你調(diào)用它們。 void print() const; //函數(shù)也可以在類體內(nèi)定義。 //這樣定義的函數(shù)會自動內(nèi)聯(lián)。 void bark() const { cout << name << " barks!\n"; } // C++有析構(gòu)函數(shù)。它們是建造者的標(biāo)識當(dāng)一個對象被刪除或超出范圍時調(diào)用它們。 virtual ~Currency(); }; //分號必須遵循類聲明。 //類成員函數(shù)通常在.cpp文件中實(shí)現(xiàn)。 Currency::Currency() { cout << "A currency has been created\n"; } Currency::Currency(const string &_name, double price) { name = _name; priceInUSD = price; cout << name << " has been created with a price of " << price << "USD\n"; } void Currency::setName(const string ¤cyName) { name = currencyName; } void Currency::setPrice(double price) { priceInUSD = price; } //請注意,僅在聲明中需要“virtual”,而不是定義。 void Currency::print() const { cout << name << " is at a price of " << priceInUSD << "USD\n"; } Currency::~Currency() { cout << name << " has been hard forked!\n"; } //struct與類相同,但它們通常僅用于封裝數(shù)據(jù)很少包含方法,在這些情況下更喜歡類 struct block_header { //默認(rèn)情況下,結(jié)構(gòu)字段是公共的 uint64_t timestamp; uint64_t blockNumber; //指向block_header對象的指針 block_header* prevBlock; }; int main() { //這會運(yùn)行默認(rèn)構(gòu)造函數(shù) Currency bitcoin; bitcoin.setName("Bitcoin"); bitcoin.setPrice(1E5); bitcoin.print(); Currency eos("EOS", 100); eos.print(); block_header genesis; genesis.timestamp = 1528445288; genesis.blockNumber = 0; //沒有用戶定義構(gòu)造函數(shù)的結(jié)構(gòu) //可以通過“aggregate initialization”初始化 block_header second{1528445288, 1, &genesis}; cout << "Timestamp of second block " << second.timestamp << "\n"; //或通過提供struct的字段名稱顯式它們需要與結(jié)構(gòu)中定義的順序相同,但允許你跳過初始化值 block_header third{.blockNumber = 2, .prevBlock = &second}; // third.timestamp初始化為0 cout << "Timestamp of block after block #" << third.prevBlock->blockNumber << ": " << third.timestamp << "\n"; }
如果沒有強(qiáng)制性的Animal
類示例,繼承的簡介會是什么?請注意,C++支持多重繼承,這是一種(有爭議的)功能,其中一個類可以同時從多個類繼承。在開發(fā)智能合約時,你可能永遠(yuǎn)不需要它,所以讓我們看一下從單個類繼承的情況。
#include <iostream> using namespace std; class Animal { string name; int weight; public: //默認(rèn)構(gòu)造函數(shù)將其值“delegates”給其他構(gòu)造函數(shù) Animal() : Animal("Peter", 80){}; //構(gòu)造函數(shù)獲取名稱和權(quán)重并初始化 //具有使用相同名稱的“initializer list”的類成員 Animal(const string &name, int weight) : name(name), weight(weight) { // we already write the function body here cout << name << " was created\n"; }; void setName(const string &dogsName); string getName() const; void setWeight(int weight); //可以覆蓋的函數(shù)必須聲明為_virtual_ virtual void print() const; //函數(shù)也可以在類聲明中定義 //但要小心,因?yàn)樗鼈儠詣觾?nèi)聯(lián)。 void eat() { weight += 5; } //如果要派生類,析構(gòu)函數(shù)應(yīng)該是虛擬的; //如果它不是虛擬的,那么如果通過基類引用或指針銷毀對象,則不會調(diào)用派生類的析構(gòu)函數(shù)。 virtual ~Animal(); }; void Animal::setName(const string &animalName) { name = animalName; } string Animal::getName() const { return name; } void Animal::setWeight(int animalWeight) { weight = animalWeight; } //“virtual”僅在聲明中需要,而不是在定義中。 void Animal::print() const { cout << name << " weighs " << weight << "kg\n"; } Animal::~Animal() { cout << "Animal " << name << " died\n"; } // Dog現(xiàn)在是Animal的子類,并繼承了Animal的成員。 //但是如果沒有g(shù)etter,可能無法直接訪問私有成員或方法。 class Dog : public Animal { string breed; public: Dog(const string &name,int weight,const string &breed):Animal(name,weight),breed(breed) { cout << "Woof\n"; } //被重載的虛擬方法應(yīng)標(biāo)記為重載。 void print() const override; }; void Dog::print() const { //調(diào)用Animal的打印功能 Animal::print(); //無法直接訪問.name,因?yàn)樗撬接械? //需要訪問public getter getName cout << Animal::getName() << " is a " << breed << " dog\n"; } int main() { Dog dog("Carl", 10, "Dackel"); dog.print(); }
到此,相信大家對“C++中怎么創(chuàng)建CryptoCurrency類”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)頁標(biāo)題:C++中怎么創(chuàng)建CryptoCurrency類
文章分享:http://chinadenli.net/article24/giijce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)站營銷、網(wǎng)站導(dǎo)航、網(wǎng)站建設(shè)、電子商務(wù)、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)