c++复习1练习题极其代码

来源:互联网 发布:房产税 知乎 编辑:程序博客网 时间:2024/06/07 01:26
#define _CRT_SECURE_NO_WARNINGS 1


//#include<iostream>
//#include<stdlib.h>
//
//using namespace std;
//int Add1(int a = 0, int b = 0)
//{
// return a + b;
//}
//
//int Add2(int a, int b = 0)
//{
// return a + b;
//}
//
//void Test()
//{
// int a = Add1();
// int b = Add1(1);
// int c = Add1(1, 1);
// int d = Add2(2);
// int e = Add2(2, 2);
// cout << a << endl;
// cout << b << endl;
// cout << c << endl;
// cout << e << endl;
// cout << e << endl;
//}
//
//int main()
//{
// Test();
// system("pause");
// return 0;
//}


//#include<iostream>
//#include<stdlib.h>
//#include<windows.h>
//using namespace std;


////引用
//void TestReference1()
//{
// int a = 1;
// int &b = a;
// cout << "a:address->" << &a << endl;
// cout << "b:address->" << &b << endl;
// cout << endl;
// cout << endl;
//
// a = 2;
// b = 3;
// int &c = b;
// c = 4;
// cout << a << endl;
// cout << b << endl;
// cout << b << endl;
// cout << "a:address->" << &a << endl;
// cout << "b:address->" << &b << endl;
// cout << "c:address->" << &c << endl;
//}


////const引用
//void TestReference2()
//{
// int d1 = 4;
// const int &d2 = d1;
// d1 = 5;//d1改变d2的值也会改变。
// //d2 = 6; //const不能被修改的值
//
// int d3 = 1;
// const int &d4 = d3;
// int &d5 = d3;
// const int &d6 = 5;//只有常引用可以引用常量
// double d7 = 1.1;
// //int &d8 = d7;//d7是double类型,d8是int,d7赋值给d8时要生成一个临时变量
// //也就是说d8引用的是这个带有常性的临时变量,所以不能赋值
// const int &d9 = d7;
// cout << d1 << endl;
// cout << d2 << endl;
// cout << d3 << endl;
// cout << d4 << endl;
// cout << d5 << endl;
// cout << d6 << endl;
// cout << d7 << endl;
// //cout << d8 << endl;
// cout << d9 << endl;
// cout << "d1:address->" << &d1 << endl;
// cout << "d2:address->" << &d2 << endl;
// cout << "d3:address->" << &d3 << endl;
// cout << "d4:address->" << &d4 << endl;
// cout << "d5:address->" << &d5 << endl;
// cout << "d6:address->" << &d6 << endl;
// cout << "d7:address->" << &d7 << endl;
// //cout << "d8:address->" << &d8 << endl;
// cout << "d9:address->" << &d9 << endl;
//}


//引用作为参数
//1,值传递
//void Swap(int a, int b)//不能交换
//{
// int temp = a;
// a = b;
// b = temp;
//}


////2,引用传递
//void Swap(int &a, int &b)//能交换
//{
// int temp = a;
// a = b;
// b = temp;
//}


////3,指针传递
//void Swap(int *a, int *b)//能交换
//{
// int temp = *a;
// *a = *b;
// *b = temp;
//}
//void TestSwap()
//{
// int a = 3;
// int b = 4;
// //Swap(a, b);
// Swap(&a, &b);
// cout << a << endl;
// cout << b << endl;
//}


////引用传递效率cost time:16 远远大于值传递
//struct BigData
//{
// int arr[1000];
//};
//void dealBigData(BigData &x)
//{
// x.arr[0] = 0;
// x.arr[1] = 1;
// x.arr[2] = 2;
//}
//void TestReference3()
//{
// BigData bd;
// int begin = GetTickCount();
// for (int i = 0; i < 1000000; ++i)
// {
// dealBigData(bd);
// }
// int end = GetTickCount();
// cout << "cost time:" << end - begin << endl;
//}
////值传递cost time:127
//struct BigData
//{
// int arr[1000];
//};
//void dealBigData(BigData x)
//{
// x.arr[0] = 0;
// x.arr[1] = 1;
// x.arr[2] = 2;
//}
//void TestReference3()
//{
// BigData bd = {0};
// int begin = GetTickCount();
// for (int i = 0; i < 1000000; ++i)
// {
// dealBigData(bd);
// }
// int end = GetTickCount();
// cout << "cost time:" << end - begin << endl;
//}
//int main()
//{
// //TestReference1();
// //TestReference2();
// //TestSwap();
// //TestReference3();
// system("pause");
// return 0;
//}




////类
//#include<iostream>
//#include<stdlib.h>
//using namespace std;
//class Person
//{
//public:
// void Display();
// //void Display()
// //{
// // cout << _name << "-" << _sex << "-" << _age << endl;
// //}
//public:
// char *_name;//名字
// char *_sex;//性别
// int _age;//年龄
//};
//
//void Person::Display()//在类外定义时,要加上作用域
//{
// cout << _name << "-" << _sex << "-" << _age << endl;
//}
//
//void Test()
//{
// Person p;
// p._name = "qingdou";
// p._sex = "女";
// p._age = 18;
// p.Display();
//
// Person *ptr = &p;
// ptr->_name = "ranran";
// ptr->_sex = "男";
// ptr->_age = 20;
// ptr->Display();
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




////内存对齐
////内存查找的时候提高效率
//#include<iostream>
//#include<stdlib.h>
//using namespace std;
//
//class A//1
//{
//
//};
//
//class B//不含结构体为8的整数倍16
//{
// char ch;//1
// double d;//8
//};
//
//class C//含结构体为结构体的整数倍32
//{
// char ch1;//1
// B a;//16
// char ch2;//1
//};
//
//void Test()
//{
// cout << "A的大小为:" << sizeof(A) << endl;
// cout << "B的大小为:" << sizeof(B) << endl;
// cout << "C的大小为:" << sizeof(C) << endl;
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}


////日期类
//#include<iostream>
//#include<stdlib.h>
//using namespace std;
//class Date
//{
//public:
// //1.无参构造函数
// Date()
// {}
// //2.带参构造函数
// Date(int year, int month, int day)
// : _year(year)
// , _month(month)
// , _day(day)
// {}
// //缺省参数的构造函数
// //Date(int year = 2016, int month = 1, int day = 1)
// //{
// // _year = year;
// // _month = month;
// // _day = day;
// //}
// //拷贝构造函数
// Date(const Date&d)
// {
// _year = d._year;
// _month = d._month;
// _day = d._day;
// }
// //析构函数:清理工作
// ~Date()
// {}
// Date &operator=(const Date& d)//因为赋值操作会改变左值,所以返回Date&
// {
// if (this != &d)//判断是否自赋值
// {
// this->_day = d._day;
// this->_month = d._month;
// this->_year = d._year;
// }
// return *this;
// }
//public:
// void Display()
// {
// cout << _year << "-" << _month << "-" << _day << endl;
// }
//private:
// int _year;
// int _month;
// int _day;
//};
//void TestDate1()
//{
// //Date d1;//调用无参构造函数
// Date d2(2015,1,9);//调用带参构造函数
// //Date d3();//不调用d3的构造函数定义d3
// Date d4(d2);//调用拷贝构造
// Date d5 = d2;//调用拷贝构造
// //d1.Display();//随机数
// Date d6;
// d6 = d2;//调用赋值运算符的重载
// d2.Display();
// d4.Display();
// d5.Display();
// d6.Display();
//}
//int main()
//{
// TestDate1();
// system("pause");
// return 0;
//}


//类成员初始化可以在构造函数当中实现,
//也可以在构造函数的初始化列表当中实现。
//但高手则一般选择后者,是什么原因呢?
//1,省去了临时对象的建立
//2,类中存在const成员,那么该成员必须在初始化列表中做初始化。
//3, 类中含有其它类作为成员,而其他类禁止调用赋值操作的情况下,
//    对它的对象的初始化也只能通过初始化列表来实现。


//哪些成员变量必须放在初始化列表里面?
//1,常量成员变量(常量创建时必须初始化)
//2,引用类型成员变量(引用创建时必须初始化)
//3,没有缺省构造函数的类成员变量




////实现复数类
//#include<iostream>
//#include<stdlib.h>
//using namespace std;
//
//class ComplexNumber//复数
//{
//public:
// //1.无参构造函数
// ComplexNumber()
// :_Re(0)
// , _Im(0)
// {}
// //2.带参构造函数
// ComplexNumber(int Re, int Im)
// : _Re(Re)
// , _Im(Im)
// {}
// ////缺省参数的构造函数
// //ComplexNumber(int Re = 1, int Im = 1)
// // : _Re(Re)
// // , _Im(Im)
// //{}
// //拷贝构造
// ComplexNumber(const ComplexNumber&CNumber)
// {
// _Re = CNumber._Re;
// _Im = CNumber._Im;
// }
// //析构函数:清理工作
// ~ComplexNumber()
// {}
// //=的重载
// ComplexNumber &operator=(const ComplexNumber& CNumber)//因为赋值操作会改变左值,所以返回Date&
// {
// if (this != &CNumber)//判断是否自赋值
// {
// _Re = CNumber._Re;
// _Im = CNumber._Im;
// }
// return *this;
// }
// //+的重载
// ComplexNumber operator+(const ComplexNumber& CNumber)
// {
// _Re = CNumber._Re + _Re;
// _Im = CNumber._Im + _Im;
// return *this;
// }
// //==重载 判断是否相等
// bool operator==(const ComplexNumber& CNumber)
// {
// if ((_Re == CNumber._Re) && (_Im == CNumber._Im))
// return true;
// else
// return false;
// }
// //<<输出运算符重载
// friend ostream &operator<<(ostream& output, const ComplexNumber& CNumber)
// {
// output << CNumber._Re;
// if (CNumber._Im > 0)
// cout << "+";
// output << CNumber._Im;
// output << "i";
// return output;
// }
//
//private:
// int _Re;//实部
// int _Im;//虚部
//};
//void TestComplexNumber()
//{
// ComplexNumber CNumber1(1, 2);//构造
// ComplexNumber CNumber2(CNumber1);//拷贝构造
// ComplexNumber CNumber3;
// CNumber3 = CNumber1;//复制运算符的重载
// ComplexNumber CNumber4(3, -3);
//
// cout << "复数1:" << CNumber1 << endl;
// cout << "复数2:" << CNumber2 << endl;
// cout << "复数3:" << CNumber3 << endl;
// cout << "复数4:" << CNumber4 << endl;
//
// if (CNumber1 == CNumber4)
// cout << "两复数相等" << endl;
// else
// cout << "两复数不相等" << endl;
//
// ComplexNumber CNumber5 = CNumber1 + CNumber4;
// cout << "复数5:" << CNumber5 << endl;
//}
//int main()
//{
// TestComplexNumber();
// system("pause");
// return 0;
//}


//
////成员变量按照声明顺序一次初始化,而不是初始化列表出现的顺序
//#include<iostream>
//#include<stdlib.h>
//using namespace std;
//class Date
//{
//public:
// Date(int x)
// :_day(x)
// , _month(_day)//初始化month时还没有初始化day 所以是随机值
// , _year(x)
// {
// cout << "Date()" << endl;
// }
// void Display()
// {
// cout << "year:" << _year << endl;
// cout << "month:" << _month << endl;
// cout << "day:" << _day << endl;
// }
//private:
// int _year;
// int _month;
// int _day;
//};
//
//void Test()
//{
// Date d1(1);
// d1.Display();
//}
////class Time
////{
////public:
//// Time(const Time&t)
//// {
//// cout << "Time(const Time&t)" << endl;
//// _hour = t._hour;
//// _minute = t._minute;
//// _second = t._second;
//// }
////private:
//// int _hour;
//// int _minute;
//// int _second;
////};
//int main()
//{
// Test();
// system("pause");
// return 0;
//}
0 0