c++跨平台Windows编程

枚举类型的声明形式:  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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值