#include
#include
#include
#include
#include
using namespace std;
///Q1
class Special
{
private:
int x;
public:
Special()
{
cout << "constructor : This is special" << endl;
}
~Special()
{
cout << "~Special" << endl;
}
Special(const Special & special)
{
x = special.x;
cout << "Copy constructor " <= 0);
}
//Q4 :
void test_rand()
{
srand((unsigned)time(0));
for(int n=3; n>0; n--)
{
cout<< rand() << endl;
}
}
//Q5:
void test_pointer_arrary()
{
char *pstr[] = {"welcome", "to", "China"};
printf("%s : size= %d\n", pstr[0], sizeof(pstr)/sizeof(pstr[0]));
printf("%s : size= %d\n", pstr[1], sizeof(pstr[1]));
}
//Q6: test what will be printed
void test_out()
{
int a, b;
unsigned int c,d;
a = -20;
c = 19.5;
d = (a+c) ? c : a;
b = (a+c > 0) ? a : c;
if(a+c > 0)
{
printf("a+c > 0\n");
}
printf("d = %d\n",d);
printf("b = %d\n",b);
printf("a+c = %d\n",a+c);
printf("c = %d\n",c);
/**
Answer
a+c > 0
d = 19
b = -20
a+c = -1
c = 19
**/
}
//Q7: test structure on 32 bits system
void test_struct()
{
struct AAA
{
char a[5];
int b;
short int c;
};
struct BBB
{
char *d;
short int c;
struct AAA f;
char g;
};
printf("sizeof(AAA) = %d\n", sizeof(struct AAA)); //16
printf("sizeof(BBB) = %d\n", sizeof(struct BBB)); //28
}
//Q8: quiz on reference of c++
//what will be printed by this function
void tough_one()
{
int x=0;
int &y = x ;
int z = 3;
++x;
++y;
y=z;
cout <<"y:" << y << ";x:" << x <<";z:" << z << endl;
z += 5; // this operation will not make variable y changed.
cout <<"y:" << y << ";x:" << x <<";z:" << z << endl;
++x;
y=x;
cout <<"y:" << y << ";x:" << x <<";z:" << z << endl;
x += 5;
cout <<"y:" << y << ";x:" << x <<";z:" << z << endl;
++y;
cout <<"y:" << y << ";x:" << x <<";z:" << z << endl;
/**** Answer
y:3;x:3;z:3
y:3;x:3;z:8
y:4;x:4;z:8
y:9;x:9;z:8
y:10;x:10;z:8
***/
}
//Q9: what's the *(*ptr) mean ?
//Q10:
void quiz_ten()
{
int array[10] = {1,2,3,4,5,6,7,8,9,10};
int *m = array;
int *m1 = m;
m1 = m1 + 1;
int *n = &array[5];
printf("n-m = %ld\n", n-m);
printf("m1-m = %ld\n", m1-m);
printf("*m1 = %d\n", *m1);
/****
n-m = 5
m1-m = 1
*m1 = 2
**/
}
//Q11: what's the correct implmetation of the swap function;
void swap(int *p1, int *p2)
{
/* this was not working , only chage the address
int *p;
p = p1;
p1 = p2;
p2 = p;
*/
// this can be working
int p;
p = *p1;
*p1 = *p2;
*p2 = p;
}
//Q12
void quiz_twelve()
{
int a[][3] = {{20,19,18}, {33,34,45}, {66,37,18}, {1,2,3} };
int *p = a[1]+5;
printf("*p = %d\n", *p);
//*p = 18
}
//Q13
void quiz_thirteen()
{
char a[] = "Language", b[] = "Programe";
char *p1, *p2;
p1 = a;
p2 = b;
for (int k = 0 ; k<=7; k++)
{
if (*(p1+k) == *(p2+k))
{
printf("%c",*(p1+k));
}
}
printf("\n");
//gae
}
//Q14 : in small-endian CPU, what's the output.
void quiz_fourteen()
{
typedef struct strBitData
{
unsigned int ulData1:2;
unsigned int ulData2:12;
unsigned int :2;
}strBitData;
strBitData stData;
unsigned int *pOutputData;
memset((void*)&stData, 0, sizeof(strBitData));
stData.ulData1 = 0x7;
stData.ulData2 = 0x010;
pOutputData = (unsigned int *) &stData;
unsigned char *p = (unsigned char *) &stData;
for (int i = 0; i<2; i++)
{
printf("0x%x\n", *(p+i));
}
printf("0x%x\n", *pOutputData);
printf("0x%x\n", stData.ulData1);
printf("0x%x\n", stData.ulData2);
/***
0x43
0x0
0x43
0x3
0x10
**/
}
//Q15:
//how many places with dynamic sapce allocated when the function executed.
static vector g_vector(2);
void quiz_fifteen()
{
string my_string = "This is string";
vector l_vector;
g_vector.push_back(1);
l_vector.push_back(10);
cout << "at(0): " << g_vector.at(0) << "\t max_size :" << g_vector.max_size() << endl;
cout << "global vec.size(): " << g_vector.size() << "\t vec.capacity(): " << g_vector.capacity() << endl;
cout << "local vec.size(): " << l_vector.size() << "\t vec.capacity(): " << l_vector.capacity() << endl;
}
/*
at(0): 0
global vec.size(): 3 vec.capacity(): 4
local vec.size(): 1 vec.capacity(): 1
**/
//Q16:
void quiz_sixteen()
{
vector l_vec_obj;
Special special;
l_vec_obj.push_back(special);
l_vec_obj.pop_back();
/****
constructor : This is special
Copy constructor
~Special
~Special
****/
}
//Q17
class Base
{
public:
virtual void func();
int a;
};
class Base1
{
public:
virtual void func1();
int a;
};
class DeriveA : public Base
{
public:
virtual void func();
};
class DeriveB : public Base, public Base1
{
};
class DeriveC : virtual public Base
{
};
class DeriveD : virtual public Base, virtual public Base1
{
};
//on 64 bits system , tips ,sizeof(pointer) = 8
void quiz_seventeen()
{
cout << "sizeof(Base)" << sizeof(Base) << endl;
cout << "sizeof(DeriveA)" << sizeof(DeriveA) << endl;
cout << "sizeof(DeriveB)" << sizeof(DeriveB) << endl;
cout << "sizeof(DeriveC)" << sizeof(DeriveC) << endl;
cout << "sizeof(DeriveD)" << sizeof(DeriveD) << endl;
/***
sizeof(Base)16
sizeof(DeriveA)16
sizeof(DeriveB)32
sizeof(DeriveC)24
sizeof(DeriveD)40
*****/
}
int main()
{
// 1) test: printf all the string with end char'\0'
const char *p = "abc";
char pAarry[] = {'a','b','c'};
printf("%s, %s\n",p,pAarry);
// 2) test
int *pInt = getNumber();
int a = 77;
printf("%d\n", *pInt);
// 3)
//Special spcail = Special(10);
//spcail.printf_x();
//4)
//test_execute_times();
test_rand();
test_pointer_arrary();
tough_one();
test_out();
test_struct();
quiz_ten();
//Q11:
int t11a = 10;
int t11b = 20;
swap(&t11a, &t11b);
printf("a = %d, b = %d\n", t11a, t11b);
quiz_twelve();
quiz_thirteen();
quiz_fourteen();
quiz_fifteen();
quiz_sixteen();
quiz_seventeen();
return 0;
}
//Q9: what's the *(*ptr) mean ?