物件導向學習【c++】

來源:互聯網
上載者:User

標籤:c++   代碼   

c++太弱了(其實是一點都不會!)
挖個坑來學習c++!
不間斷更新!

代碼1:

#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace std;/*    類編譯時間 首先編譯成員 再編譯函數體    所以 成員函數可以直接使用類中的其他成員 無須在意出現的次序    成員函數在類內部聲明  內部或外部定義*/class Sale_data {public :    Sale_data() = default;//預設建構函式    Sale_data(const string &s ,double p) ://建構函式        bookNo(s) ,revenue(p) { }    double avg_price() const;    string isbn() const    {        return this->bookNo;//this 為 常量指標    }    Sale_data& combine(const Sale_data&);    friend bool comare(Sale_data &is)//友元函數  可訪問私人成員    {        return is.is_ok;    }    string bookNo = "you are a pig";    unsigned units_sold;    double revenue;private:    bool is_ok;};double Sale_data::avg_price() const//在外部定義函數體{    if (units_sold)        return revenue / units_sold;    else        return 0;}Sale_data& Sale_data ::combine(const Sale_data &rhs){    units_sold += rhs.units_sold;    revenue += rhs.revenue;    return *this;//返回調用該函數的對象}////////////聲明函數Sale_data add(Sale_data&,const Sale_data&);std::ostream &print(std::ostream&, const Sale_data&);std::istream &read(std::istream&, Sale_data&);////////////定義 read print 函數istream &read(istream& is, Sale_data& item){    double price = 0;    is >> item.bookNo >> item.units_sold >> price;    item.revenue = price * item.units_sold;    return is;}ostream &print(std::ostream& os, const Sale_data& item){    os << item.isbn() << " " << item.units_sold << " "        << item.revenue << " " << item.avg_price() << endl;    return os;}////////////定義 add 函數Sale_data add(Sale_data& rhs, const Sale_data& lhs){    Sale_data sum = lhs;// 存放和    sum.combine(rhs);    return sum;}//////////// 類外部定義建構函式/*Sale_data::Sale_data(std::istream &is){    read (is, *this);//read 函數從 is 讀取資料存入this對象中}*/////////////Sale_data total;Sale_data trans;/////////// 主函數int main(){    cout << total.isbn() << endl;    total.combine(trans);    ///////// 類的拷貝    total = trans;    //等價於    total.bookNo = trans.bookNo; //bookNo等成員。。。    return 0;}

代碼2:

#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace std;class Screen{public:    typedef string::size_type pos;//定義類型的成員必須先定義 後使用    //using pos = string::size_type;    Screen() = default;    Screen(pos ht) :hight(ht) { }    string get() const    {        return contents;    }    Screen &move(pos r, pos c);    Screen &set(char);private:    pos cursor = 0;    pos hight = 0, width = 0;    string contents;};inline Screen&  Screen::move(pos r,pos c) //inline用於定義內嵌函式{    pos row = hight * width;    cursor = hight + 1;    return *this;}//函數重載: 函數名形同 參數個數 類型 不同的函數inline Screen &Screen::set(char c){    contents[cursor] = c;    return *this;}Screen myscreen;int main(){    myscreen.set(‘#‘);    return 0;}

代碼3:

class Screen{public:    friend class Window_mgr;// 友元類 友元關係不傳遞    friend void Window_mgr::clear(); // 其他類的函數的友元private:};class  Window_mgr{public:    void  clear();private:};Screen myscreen;int main(){    return 0;}

物件導向學習【c++】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.