windows编程之C++的复习!

枚举类型的声明形式:  enum  枚举类型名 {变量制值列表}

值得注意的是:枚举元素按常量处理,不能赋值;枚举元素具有默认的值,依次为:0,1,2,3,...

例如: enum Gameresult{win,lose,tie,cancel};

而代码:enum Gameresult omit = cancel;  则是定义了一个枚举型的变量omit并给其赋值为cancel!

例如以下代码:

#include <iostream>

using namespace std;

enum Gameresult{win,lose,tie,cancel};

int main(int argc, char *argv[])
{
    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 lose.";
            cout<<endl;
        }
    }
    return 0;
}

运行结果:

The game was played and we won!

The game was played and we lose.

The game was played

The game was cancelled

然后是类与对象的分析:从最开始的抽象与封装,到类和类的定义以及其中的成员函数和数据函数包括内联函数,析构和构造以及复制构造函数等!

运行了一个简单的时钟类:

 #include <iostream>

using namespace std;

class Clock{
public:
    //Clock(int newH,int newM,int newS);
    void setTime(int newH=0,int nemM=0,int newS=0);
    void showTime();\
private:
    int hour,minute,second;
};

/*Clock::Clock(int newH,int newM,int newS){
    hour=newH;
    minute=newM;
    second=newS;
}*/

void Clock::setTime(int newH,int newM,int newS){
    hour=newH;
    minute=newM;
    second=newS;
}
void Clock::showTime(){
    cout<<hour<<":"<<minute<<":"<<second<<endl;
}

int main(int argc, char *argv[])
{
    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

3种情况下复制构造函数的调用:

1)当用类的一个对象去初始化该类的别一个对象时;2)如果函数的形参是类的对象,调用函数时,进行形参和实参结合时;3)如果函数的返回值是类的对象,函数执行完成返回调用者时!

一个运行的例子:

#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){
    cout<<p.getx()<<endl;
}

point fun2(){
    point a(1,2);
    return a;
}

int main(int argc, char *argv[])
{
    point a(4,5);
    point b=a;
    cout<<b.getx()<<endl;
    fun1(b);
    b=fun2();
    cout<<b.getx()<<endl;
    return 0;
}

运行结果:

Calling the copy constructor

4

Calling the copy constructor

4

1

类的组合:例子线段类:

代码:

#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(int argc, char *argv[])
{
    Point myp1(1,1),myp2(4,5);
    Line line(myp1,myp2);
    Line line2(line);
    cout<<"The length of the line is: ";
    cout<<line.getLen()<<endl;
    cout<<"The length of the line2 is: ";
    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


猜你喜欢

转载自blog.csdn.net/xihairanfeng/article/details/62887644