c++跨平台Windows编程
来源:互联网 发布:2016欧洲杯c罗数据 编辑:程序博客网 时间:2024/05/08 17:36
枚举类型的声明形式: 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
- QT跨平台windows编程
- c++跨平台Windows编程
- VC++ Windows平台字符透明编程大总结 - [C/C++]
- 跨平台编程(C++)
- Windows平台下多线程编程基础(C++)
- 【Socket编程】套接字Windows平台C语言实现
- Windows平台多线程编程
- Windows平台网络编程
- 跨平台编程资源,c & c++开发
- 跨平台编程资源,c & c++开发
- Windows平台下搭建Obcject-C平台
- 基于qt的c++跨平台windows编程技术
- C 文件夹遍历(Windows + Linux)跨平台
- c#windows编程视频教程
- C语言windows编程
- windows 编程,c,c++,console
- C语言网络编程和跨平台代码安全性
- Windows平台下的多线程编程
- 易错JavaScript编程题汇总(3)
- 乘法
- java注解学习笔记
- int av_packet_ref(AVPacket *dst, const AVPacket *src)源码分析
- Java Socket编程----通信是这样炼成的
- c++跨平台Windows编程
- Python爬虫数据处理
- ZigZag Conversion
- Android 如何完整的获取到用户已安装应用列表
- Linux下USB摄像头采集图像,液晶framebuffer显示
- 【003】平方求和
- 信源编码第五次作业
- kinetis FTM详解
- 光纤陀螺简介