复习C++基础知识-----“个人第一本C++”读书笔记1

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

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

 

常规设置算法

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

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

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


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


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

  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,若是不想用默认,能够直接赋值
枚举定义以后,不能再修改其中某个元素的值操作系统


使用位操做符---------------------------------------------------.net

  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 拷贝构造函数

  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 操做符重载
函数重载和操做符重载-------------------------------------

  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;
}


 


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

  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)友元关系是单向的,不具备交换性。

  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;
}
相关文章
相关标签/搜索