C++复习2

#define _CRT_SECURE_NO_WARNINGS 1




const修饰的成员变量
const 对象不能调用非const成员函数
非const对象可以调用const成员函数
const成员函数不能调用非const成员函数
非const成员函数可以调用const成员函数
//#include<iostream>
//#include<stdlib.h>
//
//using namespace std;
//
//class Date
//{
//public:
// void Display()const
// {
// cout << _year << "-" << _month << "-" << _day << endl;
// }
// void Display()
// {
// cout << _year << "-" << _month << "-" << _day << endl;
// }
// //取地址运算符重载(这两个默认人员函数一般不用重新定义)
// Date *operator &()
// {
// return this;
// }
// const Date *operator &()const
// {
// return this;
// }
//private:
// int _year;
// int _month;
// int _day;
//};
//void Test()
//{
// Date d1;
// d1.Display();
//
// const Date d2;
// d2.Display();
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




//内联inline
//提升程序运行的效率
//以空间换取时间
//对编译器只是建议,编译器自动优化,如果内有循环和递归,编译器自动忽略
//必须放在定义处,放在声明处不起作用
//定义在类内的成员函数默认为内联函数


//尽量以const enum inline替换#define
//宏的优点:
//1,增强代码的复用性和可维护性
//2,提高性能
//宏的缺点:
//1,不方便调试(因为预编译阶段进行了替换)
//2,导致代码的可读性差,可维护性差,容易误用
//3,没有类型安全的检查




友元函数:friend
1,他不是类的成员函数
2,可以通过对象访问所有类的成员,包括私有和保护
//#include<iostream>
//#include<stdlib.h>
//
//using namespace std;
//
//class Date
//{
//public:
// friend void Display(const Date &d);
//
//private:
// int _year;
// int _month;
// int _day;
//};
//
//void Display(const Date &d)
//{
// cout << d._year << "-" << d._month << "-" << d._day << endl;
//}
//void Test()
//{
// Date d1;
// Display(d1);
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




输入输出运算符的重载的友元函数
operator<<不能定义为成员函数
   因为<<是双目运算符operator<<使用的时候,是cout << str;这样用的.左侧是cout,而不是类的对象,所以operator<<不能是类的成员函数,需要定义为友元函数。
友元在一定程度上破坏了C++的封装性,所以不宜多用,应恰当的使用友元
//#include<iostream>
//#include<stdlib.h>
//
//using namespace std;
整个类可以是另一个类的友元。友元类的每个成员函数都是另一个类的友元函数,都可以访问另一个类的保护或私有成员
//class Time
//{
// //Date是Time的友元,所以Date可以访问Time的所有成员,包括私有和保护成员
//public:
// friend class Date;
//private:
// int _hour;
// int _minute;
// int _second;
//};
//
//class Date
//{
//public:
// friend ostream&operator<<(ostream&os, const Date &d);
// friend istream&operator>>(istream&is, Date &d);
// void DisplayTime();
//private:
// int _year;
// int _month;
// int _day;
//
// Time _t;
//};
//ostream&operator<<(ostream&os, const Date &d)
//{
// os << d._year << "-" << d._month << "-" << d._day << endl;
// return os;
//}
//istream&operator>>(istream&is, Date &d)
//{
// cout << "请分别输入年月日:" << endl;
// is >> d._year >> d._month >> d._day;
// return is;
//}
//void Date::DisplayTime()
//{
// cout << "时:" <<_t._hour << endl;
// cout << "分:" << _t._minute << endl;
// cout << "秒:" << _t._second << endl;
//}
//
//void Test()
//{
// Date d1;
// cin >> d1;
// cout << d1;
// d1.DisplayTime();
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




//类的静态态成员static
//类的静态成员是该类型的所有对象所共享的
//静态成员函数没有隐含的this指针参数,所以可以用::作用域访问符直接调用静态成员函数
//非静态成员函数可以直接访问静态成员
//  直接用::作用域符莱调用
//静态成员函数不可以直接访问非静态成员
//  因为要访问非静态成员必须通过对象
//#include<iostream>
//#include<stdlib.h>
//
//using namespace std;
//
//class Date
//{
//public:
// Date()
// {
// cout << "Date()" << endl;
// ++sCount;
// }
// void Display()
// {
// cout << "年:" << _year << endl;
// cout << "月:" << _month << endl;
// cout << "日:" << _day << endl;
// }
// static void printCount()
// {
// cout << "Date count:" << sCount << endl;
// }
//private:
// int _year;
// int _month;
// int _day;
//private:
// static int sCount;
//};
//int Date::sCount = 0;//定义并初始化静态成员变量
//void Test()
//{
// Date d1,d2;
// Date::printCount();//访问静态成员
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




构造函数拷贝赋值函数的几种调用情况
//#include<iostream>
//#include<stdlib.h>
//
//using namespace std;
//
//class Date
//{
//public:
// Date()
// {
// cout << "Date()" << endl;
// }
// Date(const Date&d)
// {
// cout << "Date(const Date&d)" << endl;
// }
// Date&operator=(const Date&d)
// {
// cout << "Date&operator=(const Date&d)" << endl;
// return*this;
// }
// ~Date()
// {
// cout << "~Date()" << endl;
// }
//private:
// int _year;
// int _month;
// int _day;
//};
//
1,Date对象做参数
void fun1(Date d)
{}
//void fun1(Date& d)
//{}
//
2,Date对象做返回值
Date fun2()
{
Date d;
return d;
}
//Date& fun2()
//{
// Date d;
//  return d;
//}
//
3,Date对象做临时返回值(编译器优化问题)
Date fun3()
{
return Date();
}
//Date& fun3()
//{
// return Date();
//}
//void Test()
//{
// //1
// Date d1;
// fun1(d1);
// cout << endl;
//
// //2
// Date d2 = fun2();
// cout << endl;
//
// //3
// Date d3;
// d3 = fun3();
// cout << endl;
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




动态内存管理
//#include<stdlib.h>
//#include<iostream>
//#include<stdio.h>
//using namespace std;
//void Test()
//{
// int*p1 = new int;//动态分配4个字节(1个int)的空间单个数据
// int*p2 = new int(3);//动态分配4个字节(1个int)的空间单个数据并初始化为3
// int*p3 = new int[3];//动态分配12个字节(3个int)的空间
//
// delete p1;
// delete p2;
// delete[] p3;//一定要加[]
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




//1,operator new/operator delete和operator new[]/operator delete[]和malloc/free一样
//2,他们只负责分配空间/释放空间,不会调用对象构造函数/析构函数来初始化/清理对象
//3,operator new/operator delete只是malloc和free的一层封装


//new:
//1,调用operator new分配对象
//2,调用构造函数初始化对象(若是new[N],调用N次)


//delete:
//1,调用析构函数清理对象(若是new[N],调用N次)
//2,调用operator delete释放对象


//#include<stdlib.h>
//#include<iostream>
//using namespace std;
//
//class Array
//{
//public:
// Array(size_t size = 10)
// :_size(size)
// , _a(0)
// {
// cout << "Array(size_t size)" << endl;
// if (_size > 0)
// {
// _a = new int[size];
// }
// }
// ~Array()
// {
// cout << "~Array()" << endl;
// if (_a)
// {
// delete[] _a;
// _a = 0;//防止野指针
// _size = 0;
// }
// }
//private:
// int *_a;
// size_t _size;
//};
//void Test()
//{
// Array *p1 = (Array*)malloc(sizeof(Array));
// Array *p2 = new Array;//调用一次
// Array *p3 = new Array(20);//调用一次
// Array *p4 = new Array[10];//调用十次
//
// free(p1);
// delete p2;
// delete p3;
// delete[] p4;
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




定位表达式是在已分配的原始空间中调用构造函数初始化一个对象
//#include<stdlib.h>
//#include<iostream>
//using namespace std;
//
//class Array
//{
//public:
// Array(size_t size = 10)
// :_size(size)
// , _a(0)
// {
// cout << "Array(size_t size)" << endl;
// if (_size > 0)
// {
// _a = new int[size];
// }
// }
// ~Array()
// {
// cout << "~Array()" << endl;
// if (_a)
// {
// delete[] _a;
// _a = 0;//防止野指针
// _size = 0;
// }
// }
//private:
// int *_a;
// size_t _size;
//};
//void Test()
//{
// //malloc/free+定位操作符new()/显示调用析构函数,模拟new/delete
// Array *p1 = (Array*)malloc(sizeof(Array));
// new(p1)Array(100);
// p1->~Array();
// free(p1);
//
// //malloc/free+多次调用定位操作符new()/显示调用析构函数,模拟new[]/delete[]
// Array *p2 = (Array*)malloc(sizeof(Array)*10);
// for (int i = 0; i < 10; ++i)
// {
// new(p2 + i)Array;
// }
// for (int i = 0; i < 10; ++i)
// {
// p2[i].~Array();
// }
// free(p2);
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




简单的赋值浅拷贝
//#include<stdlib.h>
//#include<iostream>
//#include<string.h>
//using namespace std;
//
//class String
//{
//public:
// String(const char *str)
// :_str(new char[strlen(str)+1])//多一个位置放\0
// {
// strcpy(_str, str);
// }
// String(const String &str)
// :_str(str._str)
// {}
// String& operator=(const String&str)
// {
// if (this != &str)//判断自赋值
// {
// _str = str._str;
// }
// return *this;
// }
// ~String()
// {
// if (_str)
// {
// delete[]_str;
// }
// }
// friend ostream&operator<<(ostream&os, const String &str)
// {
// os << str._str;
// return os;
// }
//private:
// char*_str;
//};
//void Test()
//{
// String s1("hello world");
// String s2 = s1;
// cout << s1 << endl;
// cout << s2 << endl;
//}
//int main()
//{
// Test();
// system("pause");
// return 0;
//}




//写时拷贝
//由于浅拷贝使多个对象共用一块内存地址,调用析构函数时导致一块内存被多次释放,导致程序奔溃。
//实现string类的时候通常显示的定义拷贝构造函数和运算符重载函数。
//由于释放内存空间,开辟内存空间时花费时间,因此,在我们在不需要写,只是读的时候就可以不用新开辟内存空间,
//就用浅拷贝的方式创建对象,当我们需要写的时候才去新开辟内存空间。这种方法就是写时拷贝。
//#include<iostream>  
//#include<stdlib.h> 
//#include<string.h>
//using namespace std;
//class String
//{
//public:
// String(char *str = "")
// :_str(new char[strlen(str) + 5])//多开辟 用来存储引用计数 一个int+一个char存\0
// {
// *(int *)_str = 1;
// _str += 4;
// strcpy(_str, str);
// }
//
// ~String()
// {
// if (_str != NULL)
// {
// _Release();
// }
// }
//
// String(const String& str)
// {
// _str = str._str;
// ++_GetRefCount();
// }
//
// String& operator=(const String& str)
// {
// if (this != &str)
// {
// _Release();
// _str = str._str;
// ++_GetRefCount();
// }
// return *this;
// }
// char& operator[](int index)//写时拷贝  
// {
// if (_GetRefCount()>1)//当引用次数大于1时新开辟内存空间  
// {
// --_GetRefCount();//原来得空间引用计数器减1  
// char *str = new char[strlen(_str) + 5];
// strcpy(str + 4, _str);
// _str = str + 4;
// _GetRefCount()++;
// }
// return _str[index];
// }
//
// friend ostream& operator<<(ostream& output, const String& str)
// {
// output << str._str;
// return output;
// }
//
//private:
// int& _GetRefCount()
// {
// return *(int *)(_str - 4);
// }
//
// void _Release()
// {
// if (--_GetRefCount() == 0)
// {
// delete[](_str - 4);
// }
// }
//private:
// char *_str;
//};
//
//void Test()
//{
// String s1("hello world");
// String s2 = s1;
// cout << s1 << endl;
// cout << s2 << endl;
//}
//
//int main()
//{
// Test();
// system("pause");
// return 0;
//}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值