因为最近写cocos2dx游戏,发现c++基本还给老师了,所以有必要再复习一下,温故希望能知新。废话不多说,直接码起来
#include <iostream>
typedef struct{
int red;
int blue;
}Color;
int getMaxOrMin(int*arr,int count,bool isMax);
void swapFun(int* a, int* b);
void swapFun2(int& a, int& b);
void func(const int &a, const int &b);
void func2(int i,int j=5,int k=10);
void fun3(int x,int y=10,int z=30);
void fun3(double x,double y);
int main(int argc, const char * argv[]) {
#pragma mark 基础
#if 0
std::cout << "Hello, World!\n";
bool flag = 0;
if (flag) {
std::cout<<"真"<<std::endl;
}else{
std::cout<<"假"<<std::endl;
}
int x= 1024;
int y(1024);
bool z=0;
std::cin>>z;
std::cout<<z<<std::endl;
std::cout<<std::oct<<z<<std::endl;
std::cout<<std::dec<<z<<std::endl;
std::cout<<std::hex<<z<<std::endl;
int a[7] = {1,2,3,0,3,2,9};
int result = getMaxOrMin(a, 7, true);
std::cout<<"result:"<<result<<std::endl;
#endif
#pragma mark 重点
#if 0
int b = 3;
int &c = b;
c = 10;
std::cout<<b<<std::endl;
Color c1;
Color &c2 = c1;
c1.red = 100;
c2.blue = 200;
std::cout<<c1.red<<":"<<c2.blue<<std::endl;
int e = 10;
int * p = &e;
int *&q = p;
*q = 20;
std::cout<<e<<std::endl;
int x = 10, y = 20;
swapFun(&x, &y);
std::cout<<x<<":"<<y<<std::endl;
swapFun2(x,y);
std::cout<<x<<":"<<y<<std::endl;
const int x=3;
const int* p1 = NULL;
int const * p2 = NULL;
int * const p3 = NULL;
const int * const p4 = NULL;
int const * const p5 = NULL;
const int &y = x;
const int x2 = 3;
int x3 = 3;
const int * y2 = &x3;
int a = 30,b=20;
std::cout<<a<<":"<<b<<std::endl;
func(a, b);
std::cout<<a<<":"<<b<<std::endl;
func2(10);
func2(20, 30);
func2(40, 50, 60);
fun3(1.1,1.2);
fun3(1,1);
#endif
int * p = new int;
if (NULL == p) {
system("pause");
return 0;
}
delete p;
p = NULL;
int * arr = new int[10];
delete []arr;
arr = NULL;
return 0;
}
int getMaxOrMin(int*arr,int count,bool isMax) {
int temp;
if (count>0) {
temp = arr[0];
for (int i=1; i<count; i++) {
if (isMax) {
if (temp<arr[i]) {
temp = arr[i];
}
}else{
if (temp>arr[i]) {
temp = arr[i];
}
}
}
}
return temp;
}
void swapFun(int* a, int* b) {
int temp = 0;
temp = *a;
*a = *b;
*b = temp;
}
void swapFun2(int& a, int& b) {
int temp = 0;
temp = a;
a = b;
b = temp;
}
void func(const int &a, const int &b) {
std::cout<<a<<":"<<b<<std::endl;
}
void func2(int i,int j,int k) {
std::cout<<i <<j <<k <<std::endl;
}
void fun3(int x,int y,int z)
{
std::cout<<x << y <<z <<std::endl;
}
void fun3(double x,double y)
{
std::cout<<x << y <<std::endl;
}