[SOURCE DOWNLOAD]
Indispensable Windows Native (+)-C + +: Classes and objects
Webabcd
Introduced
Essential C + + of Windows Native
Example
1, the design of the class
CppEmployee.h
#pragmaOnce#include<string>using namespacestd;namespacenativedll{classCppemployee {intnumber;//default is Private Private://The following are private. stringName; BOOLIsmale; protected://The following are all protected. stringToString (); Public://all of the following are public floatSalary; intAge ; stringShow (); //Constructor (constructor), if not defined, the compiler automatically generates a default parameterless constructor that doesn't do anything (if you specify private, you can disallow direct instantiation, which you would do if you would normally do a singleton mode)//also: If a constructor with parameters is declared, the default parameterless constructor is not automatically generatedCppemployee (); Cppemployee (intNumberstringName ="WEBABCD");//You can specify a default value for the parameters in the constructor (ref.: CppFunction1.cpp)Cppemployee (intNumberstringNameBOOLIsmale); //Destructors (destructor), which are called when objects are destroyed, such as freeing dynamically allocated memory. It can be undefined when it is not needed, and the compiler automatically generates a default destructor that does nothing, the function name of the destructor is the same as the class name, preceded by "~"~Cppemployee (); Private: //NOTE: Member functions defined in the body are automatically processed as inline functions (for inline functions, see: CppFunction2.cpp) voidTemp () {intA = -; } /*The following is the same as the one above. inline void Temp () {int a = 100; } */ };}
CppEmployee.cpp
/** Cppemployee class*/#include"pch.h"#include"CppEmployee.h"#include"CppHelper.h"using namespaceNativedll;//"::" is the scope qualifier (field qualifier)stringcppemployee::show () {returnInt2string (number) +" "+Name;}stringcppemployee::tostring () {returnInt2string (number) +" "+Name;}//constructor with no parametersCppemployee::cppemployee () { number=888; Name="WEBABCD";}//constructor with parameters, you can specify a default value for the parameter in the DeclarationCppemployee::cppemployee (intNumberstringname) { number=Number ; Name=name;}//You can assign a parameter value in a constructor to an object's variable in this simple wayCppemployee::cppemployee (intNumberstringNameBOOLIsmale): number, name (name), Ismale (Ismale) {}cppemployee::~Cppemployee () {}
2, the use of the class
CppClass1.h
#pragma<string>usingnamespace std; namespace nativedll{ class CppClass1 { public: string Demo (); };}
CppClass1.cpp
/** Classes and Objects*/#include"pch.h"#include"CppClass1.h"#include"CppEmployee.h"using namespaceNativedll;//"::" is the scope qualifier (field qualifier)stringCppClass1::D emo () {//Method 1 for defining an object: Defining an object at the same time as the class declaration (there can be no class name at this time)//method of defining an Object 2: You typically define an object in the following way//class Cppemployee employee;//class can saveCppemployee employee;//instantiate, allocating memory space at the same time (only part of the data is allocated, the function part does not allocate space)//properties or functions that can access publicEmployee. Salary =100.0f; Employee. Show (); //references to ObjectsCppemployee &employee2 =employee; Employee2. Salary=1000.0f; //pointer to an objectCppemployee *employee3 = &employee; Employee3->salary =10000.0f; //You can also instantiate thisCppemployee Employee5 (1,"WEBABCD");//instantiated 1 timesCppemployee Employee6 = {1,"WEBABCD"};//instantiated 1 timesCppemployee Employee7 = Cppemployee (1,"WEBABCD");//instantiated 2 of times. Cppemployee Employee7 is instantiated once, Cppemployee (1, "WEBABCD") is instantiated once//You can also instantiate thisCppemployee *employee8 =NewCppemployee (1,"WEBABCD");//instantiate, and return the address of the objectEmployee8->Show (); DeleteEmployee8; //Note://1, the above in the Demo function to instantiate a lot of internal objects, they will be in order one by one to press into the stack, the stack is FILO, so when the demo function is finished, to destroy its internal objects, is to call the destructor in order of FILO//2. If n objects are instantiated by the same class, there are n sets of the same size space to hold the data members in N objects. However, different objects call the same function code snippet. return "look at the code and the comments.";}
Ok
[SOURCE DOWNLOAD]
Indispensable Windows Native (+)-C + +: Classes and objects