博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
复习C++基础知识-----“我的第一本C++”读书笔记1
阅读量:7048 次
发布时间:2019-06-28

本文共 12624 字,大约阅读时间需要 42 分钟。

hot3.png

Debug : 在单独运行时,往往需要编译器提供一些库文件

Release : 可以在没有安装visual c++的computer上正常运行

 

常规设置

1)

在共享DLL中使用MFC : 表示把程序中用到的MFC类库作为动态链接库,这样编译生成器的程序比较小,但是在运行的时候,需要操作系统提供额外的动态库支持。

2)

在静态库中使用MFC : 表示把用到的MFC类的内容作为静态库加到程序中,这样编译产生的程序可以在任何Windwos环境下运行,但是程序的体积比较大。

3)

使用标准Windows库 : 表示不适用MFC类库,仅使用标准的Windows类库。

#define and const
宏的使用是在预处理的时候进行五条件的替换,并没有明确指定这个常量的数据类型、所以带来便利的同时也容易带来问题。
所以出现了const

枚举的使用---------------------------------

[cpp]
  1. enum Weekday  
  2. {  
  3.     mon,  
  4.     tue,  
  5.     wed,  
  6.     thu,  
  7.     fri,  
  8.     sat,  
  9.     sun  
  10. };  
enum Weekday{	mon,	tue,	wed,	thu,	fri,	sat,	sun};

枚举默认的值是0,如果不想用默认,可以直接赋值
枚举定义之后,不能再修改其中某个元素的值

使用位操作符---------------------------------------------------

[cpp]
  1.     cout << "use transpose operator :" << endl;  
  2.     int iValue = 1;  
  3. //  iValue = iValue * 4;  
  4.     iValue = iValue << 34; // 34 % 32 = 2左移 因为超过了int的大小空间  
  5.     cout << "iValue value is : " << iValue << endl;  
cout << "use transpose operator :" << endl;	int iValue = 1;//	iValue = iValue * 4;	iValue = iValue << 34; // 34 % 32 = 2左移 因为超过了int的大小空间	cout << "iValue value is : " << iValue << endl;

 

内联函数 :

可以更形象地称为内嵌函数,它是C++对函数的一种特殊修饰。当编译器编译程序时,如果发现某段代码在调用一个内联函数,它就不再去调用该函数,而是将该函数的代码整段插入当前函数调用的位置。这样就省去了函数调用的过程,提高了代码的执行效率。关键字inline

函数重载 :
1)函数重载的意义在于可以根据输入参数的类型或者个数,自动地在多个重载函数中查找与之匹配的重载函数,从而只能地决定采用哪个函数版本。
2)只有相互之间的参数类型或者个数不同,才可以构成合法的重载函数

函数的声明----也称为函数的接口
1)试一试在纯c中使用接口------------------------------------------------
2)尽量在函数中使用断言assert判断函数的有效性
3)函数的功能要做到单一----如果一个函数需要完成多项任务,最好拆分成多个函数

面向对象的知识 :
1)封装
在传统的结构化程序设计思想中,数据和算法是相互分离的。
在面向对象的程序设计思想中,对象就是封装数据和操作这些数据的算法的逻辑实体,也是现实世界中物体在程序中的反映,使用封装有的时候可以很好的保护对象的私有部分

2)继承

从父亲那里得到技能,自己又可以继续学习。比如我现在就站在前人的基础上学习的c++

3)多态

就是指对象在不同情况下具有不同形式的能力。多态机制使具有不同内部结构的对象可以共享相同的外部接口。比如,给别人一幅画,不一定是你自己画的,也可以直接把你老爸的画送出

想想 : 如何在面向对象程序设计思想上,考虑扩展、复用、可维护问题

new :
在new不成功是,不必去判断指针是否为null,因为new不成功时,系统会自动抛出std::bad_alloc异常,new操作永远不会返回null

拷贝构造函数 :
默认也会有拷贝构造函数,当类中含有指针类型的属性时,以拷贝内存形式出现的默认拷贝构造函数只能复制指针属性的值,而不能复制指针属性所指向的内存,在这个情况下需要自定义类的拷贝函数,完成指针属性等需要页数处理的属性的拷贝工作。

p163 拷贝构造函数

[cpp]
  1. namespace Zeng  
  2. {  
  3.     class CTest_A  
  4.     {  
  5.     public:  
  6.         CTest_A( int iValue, char* cName )  
  7.         {  
  8.             this->m_iValue = iValue;  
  9.             this->m_pName = new char[ strlen( cName ) + 1 ];  
  10.             strcpy( m_pName, cName );  
  11.         }  
  12.         // 拷贝构造函数  
  13.         CTest_A( const CTest_A& rCG )  
  14.         {  
  15.             this->m_iValue = rCG.m_iValue;  
  16.             this->m_pName = new char[ strlen( rCG.m_pName ) + 1 ];  
  17.             strcpy( m_pName, rCG.m_pName );  
  18.         }  
  19.         void print()  
  20.         {  
  21.             cout << "CTest_G m_iValue value is : " << this->m_iValue << endl;   
  22.             cout << "CTest_G m_pName value is : " << this->m_pName << endl;   
  23.             cout << "CTest_G m_pName address is : " << *this->m_pName << endl;   
  24.         }  
  25.     public:  
  26.         int m_iValue;  
  27.         char* m_pName;  
  28.     }; // 试试拷贝构造函数  
  29. }  
  30.   
  31. int _tmain(int argc, _TCHAR* argv[])  
  32. {  
  33.     Zeng::CTest_A CA( 10, "Zengraoli" );  
  34.     Zeng::CTest_A CA2( 10, "Zengraoli" );  
  35.     Zeng::CTest_A CA3( CG );  
  36.     cout << "CA print" << endl;  
  37.     CA.print();  
  38.   
  39.     cout << "\n";  
  40.     cout << "CA2 print" << endl;  
  41.     CA2.print();  
  42.   
  43.     cout << "\n";  
  44.     cout << "CA3 print" << endl;  
  45.     CA3.print();  
  46.     cout << "class CTest_A size is : " << sizeof( Zeng::CTest_A ) << endl;  
  47.   
  48.     return 0;  
  49. }  
namespace Zeng{	class CTest_A	{	public:		CTest_A( int iValue, char* cName )		{			this->m_iValue = iValue;			this->m_pName = new char[ strlen( cName ) + 1 ];			strcpy( m_pName, cName );		}		// 拷贝构造函数		CTest_A( const CTest_A& rCG )		{			this->m_iValue = rCG.m_iValue;			this->m_pName = new char[ strlen( rCG.m_pName ) + 1 ];			strcpy( m_pName, rCG.m_pName );		}		void print()		{			cout << "CTest_G m_iValue value is : " << this->m_iValue << endl; 			cout << "CTest_G m_pName value is : " << this->m_pName << endl; 			cout << "CTest_G m_pName address is : " << *this->m_pName << endl; 		}	public:		int m_iValue;		char* m_pName;	}; // 试试拷贝构造函数}int _tmain(int argc, _TCHAR* argv[]){	Zeng::CTest_A CA( 10, "Zengraoli" );	Zeng::CTest_A CA2( 10, "Zengraoli" );	Zeng::CTest_A CA3( CG );	cout << "CA print" << endl;	CA.print();	cout << "\n";	cout << "CA2 print" << endl;	CA2.print();	cout << "\n";	cout << "CA3 print" << endl;	CA3.print();	cout << "class CTest_A size is : " << sizeof( Zeng::CTest_A ) << endl;	return 0;}

 

P166 操作符重载

函数重载和操作符重载-------------------------------------

[cpp]
  1. #include "stdafx.h"  
  2. #include "iostream"  
  3. using namespace std;  
  4.   
  5. namespace Zeng  
  6. {  
  7.     class CTest_A  
  8.     {  
  9.     public:  
  10.         CTest_A()  
  11.         {  
  12.         }  
  13.         void print()  
  14.         {  
  15.             cout << "this is CTest_A print()" << endl;  
  16.         }  
  17.     }; // 占1个字节的大小  
  18.   
  19.     class CTest_B : virtual CTest_A  
  20.     {  
  21.     public:  
  22.         CTest_B( int iValue ) : m_iValue( iValue )  
  23.         {  
  24.         }  
  25.         void print()  
  26.         {  
  27.             cout << "m_iValue value is : " << m_iValue << endl;  
  28.         }  
  29.     private:  
  30.         int m_iValue;  
  31.     }; // 占8个字节的大小  
  32.   
  33.     class CTest_C : virtual CTest_A  
  34.     {  
  35.     public:  
  36.         CTest_C()  
  37.         {  
  38.         }  
  39.     }; // 占4个字节的大小  
  40.   
  41.     class CTest_D : virtual CTest_B  
  42.     {  
  43.     public:  
  44.         CTest_D( int iValue ) : CTest_B( iValue )  
  45.         {  
  46.             this->m_iValue = iValue + 89;  
  47.         }  
  48.         void print()  
  49.         {  
  50.             cout << "m_iValue value is : " << this->m_iValue << endl;  
  51.         }  
  52.     private:  
  53.         int m_iValue;  
  54.     }; // 占16个字节的大小  
  55.   
  56.     class CTest_E : public CTest_A  
  57.     {  
  58.     public:  
  59.         CTest_E( int iValue )  
  60.         {  
  61.             this->m_iValue = iValue + 89;  
  62.         }  
  63.         void print()  
  64.         {  
  65.             cout << "this is CTest_E not parameter's print()" << endl;  
  66.         }  
  67.         void print( int iValue )  
  68.         {  
  69.             cout << "this is CTest_E has parameter's print()" << endl;  
  70.         }  
  71. /* 
  72.         int print( int iValue ) 
  73.         {
     
  74.             cout << "this is CTest_E has parameter's print()" << endl; 
  75.             return 0; 
  76.         } // c++可以忽略函数的返回值,所以只能靠参数不同来进行函数重载 
  77. */  
  78.     private:  
  79.         int m_iValue;  
  80.     }; // 试试函数重载  
  81.   
  82.     class CTest_F  
  83.     {  
  84.     public:  
  85.         CTest_F( int iValue )  
  86.         {  
  87.             this->m_iValue = iValue;  
  88.         }  
  89.         void print()  
  90.         {  
  91.             cout << "CTest_F m_iValue value is : " << this->m_iValue << endl;   
  92.         }  
  93.         const CTest_F& operator+ ( const CTest_F& rCF )  
  94.         {  
  95.             this->m_iValue += rCF.m_iValue;  
  96.             return *this;  
  97.         }  
  98.         const CTest_F& operator= ( const CTest_F& rCF )  
  99.         {  
  100.             this->m_iValue = rCF.m_iValue;  
  101.             return *this;  
  102.         }  
  103.     public:  
  104.         int m_iValue;  
  105.     }; // 试试操作符重载  
  106. }  
  107.   
  108. int _tmain(int argc, _TCHAR* argv[])  
  109. {  
  110.     Zeng::CTest_A CA;  
  111.     CA.print();  
  112.     cout << "class CTest_A size is : " << sizeof( Zeng::CTest_A ) << endl;  
  113.     cout << "class CTest_B size is : " << sizeof( Zeng::CTest_B ) << endl;  
  114.     cout << "class CTest_C size is : " << sizeof( Zeng::CTest_C ) << endl;  
  115.   
  116.     cout << "\n";  
  117.     Zeng::CTest_D CD( 10 );  
  118.     CD.print();  
  119.     cout << "class CTest_D size is : " << sizeof( Zeng::CTest_D ) << endl;  
  120.   
  121.     cout << "\n";  
  122.     Zeng::CTest_E CE( 10 );  
  123.     CE.print();  
  124.     CE.print(1);  
  125.     cout << "class CTest_E size is : " << sizeof( Zeng::CTest_E ) << endl;  
  126.   
  127.     cout << "\n";  
  128.     Zeng::CTest_F CF( 10 );  
  129.     Zeng::CTest_F CF2( 89 );  
  130.     CF = CF + CF2;  
  131.     cout << "in class CTest_F override add after : " << sizeof( Zeng::CTest_F ) << endl;  
  132.     CF.print();  
  133.     CF = CF2;  
  134.     cout << "in class CTest_F override equal after : " << sizeof( Zeng::CTest_F ) << endl;  
  135.     CF.print();  
  136.     cout << "class CTest_F size is : " << sizeof( Zeng::CTest_F ) << endl;  
  137.   
  138.     return 0;  
  139. }  
#include "stdafx.h"#include "iostream"using namespace std;namespace Zeng{	class CTest_A	{	public:		CTest_A()		{		}		void print()		{			cout << "this is CTest_A print()" << endl;		}	}; // 占1个字节的大小	class CTest_B : virtual CTest_A	{	public:		CTest_B( int iValue ) : m_iValue( iValue )		{		}		void print()		{			cout << "m_iValue value is : " << m_iValue << endl;		}	private:		int m_iValue;	}; // 占8个字节的大小	class CTest_C : virtual CTest_A	{	public:		CTest_C()		{		}	}; // 占4个字节的大小	class CTest_D : virtual CTest_B	{	public:		CTest_D( int iValue ) : CTest_B( iValue )		{			this->m_iValue = iValue + 89;		}		void print()		{			cout << "m_iValue value is : " << this->m_iValue << endl;		}	private:		int m_iValue;	}; // 占16个字节的大小	class CTest_E : public CTest_A	{	public:		CTest_E( int iValue )		{			this->m_iValue = iValue + 89;		}		void print()		{			cout << "this is CTest_E not parameter's print()" << endl;		}		void print( int iValue )		{			cout << "this is CTest_E has parameter's print()" << endl;		}/*		int print( int iValue )		{			cout << "this is CTest_E has parameter's print()" << endl;			return 0;		} // c++可以忽略函数的返回值,所以只能靠参数不同来进行函数重载*/	private:		int m_iValue;	}; // 试试函数重载	class CTest_F	{	public:		CTest_F( int iValue )		{			this->m_iValue = iValue;		}		void print()		{			cout << "CTest_F m_iValue value is : " << this->m_iValue << endl; 		}		const CTest_F& operator+ ( const CTest_F& rCF )		{			this->m_iValue += rCF.m_iValue;			return *this;		}		const CTest_F& operator= ( const CTest_F& rCF )		{			this->m_iValue = rCF.m_iValue;			return *this;		}	public:		int m_iValue;	}; // 试试操作符重载}int _tmain(int argc, _TCHAR* argv[]){	Zeng::CTest_A CA;	CA.print();	cout << "class CTest_A size is : " << sizeof( Zeng::CTest_A ) << endl;	cout << "class CTest_B size is : " << sizeof( Zeng::CTest_B ) << endl;	cout << "class CTest_C size is : " << sizeof( Zeng::CTest_C ) << endl;	cout << "\n";	Zeng::CTest_D CD( 10 );	CD.print();	cout << "class CTest_D size is : " << sizeof( Zeng::CTest_D ) << endl;	cout << "\n";	Zeng::CTest_E CE( 10 );	CE.print();	CE.print(1);	cout << "class CTest_E size is : " << sizeof( Zeng::CTest_E ) << endl;	cout << "\n";	Zeng::CTest_F CF( 10 );	Zeng::CTest_F CF2( 89 );	CF = CF + CF2;	cout << "in class CTest_F override add after : " << sizeof( Zeng::CTest_F ) << endl;	CF.print();	CF = CF2;	cout << "in class CTest_F override equal after : " << sizeof( Zeng::CTest_F ) << endl;	CF.print();	cout << "class CTest_F size is : " << sizeof( Zeng::CTest_F ) << endl;	return 0;}

 

构造函数私有化的含义---------------------------------------------

[cpp]
  1. namespace Rao  
  2. {  
  3.     class CTest  
  4.     {  
  5.     public:  
  6.         static CTest* makeAnObject()  
  7.         {  
  8.              // 程序结束的时候 自动释放  
  9.             static CTest instance;  
  10.             return &instance;  
  11.         }  
  12.         ~CTest()  
  13.         {  
  14.             cout << "CTest Destructor...." << endl;  
  15.         }  
  16.         static int m_nValue;  
  17.     private:  
  18.         CTest()  
  19.         {  
  20.             m_nValue++;  
  21.             cout << "CTest Constructor...." << endl;  
  22.         }  
  23.           
  24.         CTest( const CTest& CopyCTest )  
  25.         {  
  26.             m_nValue++;  
  27.             cout << "CopyCTest Constructor...." << endl;  
  28.         }  
  29.   
  30.         const  CTest& operator= ( const CTest& );  
  31.     };  
  32. }  
  33. main:  
  34.     int Rao::CTest::m_nValue;  
  35.   
  36.     cout << "\n";  
  37.     cout << "use Constructor privatization :" << endl;  
  38.     Rao::CTest* RaoCTest = Rao::CTest::makeAnObject();  
  39.     cout << "m_nValue :" << Rao::CTest::m_nValue << endl;  
  40.     Rao::CTest* RaoCTest2 = Rao::CTest::makeAnObject();  
  41.     cout << "m_nValue :" << Rao::CTest::m_nValue << endl;  
  42.   
  43.     // 调用拷贝构造函数  
  44.     Rao::CTest sg = *Rao::CTest::makeAnObject();  
  45.     cout << "m_nValue :" << Rao::CTest::m_nValue << endl;  
namespace Rao{	class CTest	{	public:		static CTest* makeAnObject()		{			 // 程序结束的时候 自动释放			static CTest instance;			return &instance;		}		~CTest()		{			cout << "CTest Destructor...." << endl;		}		static int m_nValue;	private:		CTest()		{			m_nValue++;			cout << "CTest Constructor...." << endl;		}				CTest( const CTest& CopyCTest )		{			m_nValue++;			cout << "CopyCTest Constructor...." << endl;		}		const  CTest& operator= ( const CTest& );	};}main:	int Rao::CTest::m_nValue;	cout << "\n";	cout << "use Constructor privatization :" << endl;	Rao::CTest* RaoCTest = Rao::CTest::makeAnObject();	cout << "m_nValue :" << Rao::CTest::m_nValue << endl;	Rao::CTest* RaoCTest2 = Rao::CTest::makeAnObject();	cout << "m_nValue :" << Rao::CTest::m_nValue << endl;	// 调用拷贝构造函数	Rao::CTest sg = *Rao::CTest::makeAnObject();	cout << "m_nValue :" << Rao::CTest::m_nValue << endl;

 

类的成员访问控制

public : 公有访问接口
protected : 1)可以供类自身访问的成员 2)可以供下级子类访问的成员
private : 仅供类自身访问的成员

友元函数
简单理解为 : 由于类成员的访问控制机制,很好地实现了数据的隐藏与封装,类的成员变量一般定义为私有成员,成员函数一般定义为公有成员,以此来提供类与外界间的通信接口;但有特殊的情况,比如需要定义某个函数/某个类,这个函数/类不是类CA的一部分,但又需要频繁地访问类CA的隐藏信息,所以C++提供了一个"friend"关键字来完成这个任务。
友元函数和友元类都需要试一试--------------------------------------P172
但需要记住的一点 :
1)友元关系不能被继承。这一点很好理解,我们跟某个类是朋友,并不表示我们跟这个类的儿子(派生类)同样是朋友。
2)友元关系是单向的,不具有交换性。

[cpp]
  1. #include "stdafx.h"  
  2. #include "iostream"  
  3. using namespace std;  
  4.   
  5. namespace Zeng  
  6. {  
  7.     class CTest_A  
  8.     {  
  9.     public:  
  10.         CTest_A( int iValue )  
  11.         {  
  12.             this->m_iValue = iValue;  
  13.         }  
  14.         void print()  
  15.         {  
  16.             cout << "CTest_A's m_iValue current value is : " << this->m_iValue << endl;  
  17.         }  
  18.         friend void firendFunction( const CTest_A& CA );  
  19.         friend class CTest_B;  
  20.     private:  
  21.         int m_iValue;  
  22.     };  
  23.   
  24.     class CTest_B  
  25.     {  
  26.     public:  
  27.         CTest_B()  
  28.         {  
  29.         }  
  30.         void print( const CTest_A& CA )  
  31.         {  
  32.             cout << "this's firend class CTest_B print : " << CA.m_iValue << endl;  
  33.         }  
  34.     private:  
  35.     }; // 友元类  
  36.   
  37.     void firendFunction( const CTest_A& CA )  
  38.     {  
  39.         cout << "this's firend function print : " << CA.m_iValue << endl;  
  40.     }  
  41. }  
  42.   
  43. int _tmain(int argc, _TCHAR* argv[])  
  44. {  
  45.     Zeng::CTest_A CA( 100 );  
  46.     firendFunction( CA );  
  47.   
  48.     cout << "\n";  
  49.     Zeng::CTest_B CB;  
  50.     CB.print( CA );  
  51.   
  52.     return 0;  
  53. }  
#include "stdafx.h"#include "iostream"using namespace std;namespace Zeng{	class CTest_A	{	public:		CTest_A( int iValue )		{			this->m_iValue = iValue;		}		void print()		{			cout << "CTest_A's m_iValue current value is : " << this->m_iValue << endl;		}		friend void firendFunction( const CTest_A& CA );		friend class CTest_B;	private:		int m_iValue;	};	class CTest_B	{	public:		CTest_B()		{		}		void print( const CTest_A& CA )		{			cout << "this's firend class CTest_B print : " << CA.m_iValue << endl;		}	private:	}; // 友元类	void firendFunction( const CTest_A& CA )	{		cout << "this's firend function print : " << CA.m_iValue << endl;	}}int _tmain(int argc, _TCHAR* argv[]){	Zeng::CTest_A CA( 100 );	firendFunction( CA );	cout << "\n";	Zeng::CTest_B CB;	CB.print( CA );	return 0;}

转载于:https://my.oschina.net/u/1455799/blog/220042

你可能感兴趣的文章
如何为你的App配置多环境变量
查看>>
学习OpenGL ES之什么是Shader?
查看>>
RxJava学习之结合(组合)型操作符
查看>>
Python基础(三): 数值和布尔
查看>>
从零开始实现一个简易的Java MVC框架
查看>>
iOS 12, watchOS 5, macOS Mojave 10 14, tvOS 12 等beta版描述文件下载
查看>>
Python3爬虫-04-模拟登录爬取企信宝200页数据
查看>>
javascript设计模式
查看>>
打造 Laravel 优美架构 谈可维护性与弹性设计
查看>>
JS每日一题: 请简述一下vuex实现原理
查看>>
从 TodoList 中学父子组件通信
查看>>
用koa开发一套内容管理系统(CMS),支持javascript和typescript双语言
查看>>
Promise面试题,控制异步流程
查看>>
css-从笔试题中看知识
查看>>
LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
查看>>
前嗅ForeSpider教程:数据浏览与可视化
查看>>
SQLServer之锁简介
查看>>
Spring Boot 参考指南(开发者工具)
查看>>
Linux 程序包的管理
查看>>
JavaScript 异步、栈、事件循环、任务队列
查看>>