2000行之C++基礎練習

來源:互聯網
上載者:User

標籤:amp   oid   double   poi   min   ast   ret   c++基礎   ==   

#include<iostream>using namespace std;enum GameResult{WIN,LOSE,TIE,CANCEL};int main(){    GameResult result;    enum GameResult omit = CANCEL;    for(int count = WIN;count <= CANCEL;count++){        result = GameResult(count);        if(result = omit)            cout<<"The game was cancelled"<<endl;        else{            cout<<"The game was played"<<endl;            if(result == WIN)                cout<<"and we won!";            if(result == LOSE)                cout<<"and we lost";            cout<<endl;        }    }    return 0;}
#include<iostream>using namespace std;class clock{public:    void setTime(int newH = 0, int newM = 0, int newS = 0);    void showTime();private:    int hour,minute,second;};void clock::setTime(int newH, int newM, int newS){    hour = newH;    minute = newM;    second = newS;}inline void clock::showTime(){    cout<<hour<<":"<<minute<<":"<<second<<endl;}int main(){    clock myclock;    cout<<"First time set and output"<<endl;    myclock.setTime();    myclock.showTime();    return 0;}
#include<iostream>using namespace std;const float PI = 3.141593;const float F = 35;const float C = 20;class circle1{public:    circle1(float r);    float circumference();    float area();private:    float radius;};circle1::circle1(float r){    radius = r;}float circle1::circumference(){    return 2*PI*radius;}float circle1::area(){    return PI*radius*radius;}int main(){    float radius;    cout<<"Enter the radius of the pool:"<<endl;    cin>>radius;    circle1 pool(radius);    circle1 poolR(radius + 3);    float fenceCost = poolR.circumference()*F;    cout<<fenceCost<<endl;    float concreteCost = (poolR.area() - pool.area())*C;    cout<<concreteCost<<endl;    return 0;}
#include<iostream>#include<cmath>using namespace std;class point{public:    point(int xx = 0, int yy = 0){        x = xx;        y = yy;    }    point(point &p);    int getX(){return x;}    int getY(){return y;}private:    int x,y;};point::point(point &p){    x = p.x;    y = p.y;    cout<<"copy constructor of point"<<endl;}class line{public:    line(point xp1, point xp2);    line(line &l);    double getLen(){return len;}private:    point p1,p2;    double len;};line::line(point xp1, point xp2):p1(xp1),p2(xp2){    cout<<"c of line"<<endl;    double x = static_cast<double>(p1.getX() - p2.getX());    double y = static_cast<double>(p1.getY() - p2.getY());    len = sqrt(x*x + y*y);}line::line(line &l):p1(l.p1),p2(l.p2){    cout<<"copy c of line"<<endl;    len = l.len;}int main(){    point myp1(1,1),myp2(4,5);    line line1(myp1,myp2);    line line2(line1);    cout<<line1.getLen()<<line2.getLen()<<endl;    return 0;}
#include<iostream>using namespace std; class point { public:     point (int x = 0, int y = 0):x(x),y(y){        count++;     }     point (point &p){        x = p.x;        y = p.y;        count++;     }     ~point(){count--;}     int getX(){        return x;     }     int getY(){return y;}     void showCount(){cout<<count<<endl;} private:    int x,y;    static int count; }; int point::count = 0; int main(){    point a(4,5);    cout<<a.getX()<<a.getY()<<endl;    a.showCount();    point b(a);    b.showCount();    return 0; }#include<iostream>using namespace std; class point { public:     point (int x = 0, int y = 0):x(x),y(y){        count++;     }     point (point &p){        x = p.x;        y = p.y;        count++;     }     ~point(){count--;}     int getX(){        return x;     }     int getY(){return y;}     static void showCount(){cout<<count<<endl;} private:    int x,y;    static int count; }; int point::count = 0; int main(){    point a(4,5);    cout<<a.getX()<<a.getY()<<endl;    point::showCount();    point b(a);    point::showCount();    return 0; }

 

2000行之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.