windows实验.2——c++

来源:互联网 发布:软件清理不干净 编辑:程序博客网 时间:2024/05/06 15:33

1、枚举类型的使用:

 例:

#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 ";
            if(result==WIN)
                cout<<"and we won!";
            if(result==LOSE)
                cout<<"and we lost!";
            cout<<endl;
        }
    }

    cout << "Hello World!" << endl;
    return 0;
}

结果:

The game was played and we won!

The game was played and we lost!

The game was played

The game was cancelled

Hello World!

总结:

枚举元素具有默认值,也可在声明时定义枚举元素的值。

将整数值赋值给枚举变量时需要进行强制类型转换。

将枚举类型变量转换为整型时进行的是隐含的类型转换。


2、类与对象

例1:成员函数、成员变量

#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();
    cout<<"Second time set and output:"<<endl;
    myClock.setTime(8,30,30);
    myClock.showTime();
    return 0;
}
结果:

First time set and output:

0:0:0

Second time set and output:

8:30:30


例2:构造函数、复制构造函数

#include <iostream>

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<<"Calling the copy constructor"<<endl;
}

void fun1(Point p){ //形参为Point类对象的函数
    cout<<p.getx()<<endl;
}

Point fun2(){ //返回值为Point类对象的函数
    Point a(1,2);
    return a;
}

int main()
{
    Point a(4,5);
    Point b=a; //用a初始化b,调用复制构造函数
    cout<<b.getx()<<endl;
    fun1(b); //对象b为函数fun1的实参,调用复制构造函数
    b=fun2(); //函数的返回值为类对象,函数返回时,调用复制构造函数
    cout<<b.getx()<<endl;
    return 0;
}

结果:

Calling the copy constructor
4
Calling the copy constructor
4

Calling the copy constructor

1


例3:类的组合

#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<<"Calling the 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<<"Calling constructor 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<<"Calling the copy constructor of Line"<<endl;
    len=l.len;
}

int main()
{
    Point myp1(1,1),myp2(4,5);
    Line line(myp1,myp2);
    Line line2(line); //利用复制构造函数建立一个新的对象
    cout<<"The length of the line is:"<<endl;
    cout<<line.getLen()<<endl;
    cout<<"The length of the line2 is:"<<endl;
    cout<<line2.getLen()<<endl;
    return 0;
}

结果:

Calling the copy constructor of Point

Calling the copy constructor of Point

Calling the copy constructor of Point

Calling the copy constructor of Point

Calling constructor of Line

Calling the copy constructor of Point

Calling the copy constructor of Point

Calling the copy constructor of Line

The length of the line is:

5

The length of the line2 is:

5


0 0
原创粉丝点击