首页 > 代码库 > 2000行之C++基础练习

2000行之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++基础练习