Debug : 在单独运行时,往往需要编译器提供一些库文件
Release : 可以在没有安装visual c++的computer上正常运行
常规设置
1)
在共享DLL中使用MFC : 表示把程序中用到的MFC类库作为动态链接库,这样编译生成器的程序比较小,但是在运行的时候,需要操作系统提供额外的动态库支持。2)
在静态库中使用MFC : 表示把用到的MFC类的内容作为静态库加到程序中,这样编译产生的程序可以在任何Windwos环境下运行,但是程序的体积比较大。3)
使用标准Windows库 : 表示不适用MFC类库,仅使用标准的Windows类库。 #define and const宏的使用是在预处理的时候进行五条件的替换,并没有明确指定这个常量的数据类型、所以带来便利的同时也容易带来问题。 所以出现了const 枚举的使用---------------------------------- enum Weekday
- {
- mon,
- tue,
- wed,
- thu,
- fri,
- sat,
- sun
- };
enum Weekday{ mon, tue, wed, thu, fri, sat, sun};枚举默认的值是0,如果不想用默认,可以直接赋值 枚举定义之后,不能再修改其中某个元素的值 使用位操作符---------------------------------------------------
- cout << "use transpose operator :" << endl;
- int iValue = 1;
- // iValue = iValue * 4;
- iValue = iValue << 34; // 34 % 32 = 2左移 因为超过了int的大小空间
- 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 拷贝构造函数
- 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;
- }
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 操作符重载
函数重载和操作符重载-------------------------------------- #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;
- }
#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;}构造函数私有化的含义---------------------------------------------
- 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;
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)友元关系是单向的,不具有交换性。- #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;
- }
#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;}