Essential Windows Native-C + +: Dynamic creation and deallocation of objects, assignment and copying of objects, static properties and static functions, class templates

Source: Internet
Author: User
Tags define null

[SOURCE DOWNLOAD]


Essential Windows Native-C + +: Dynamic creation and deallocation of objects, assignment and copying of objects, static properties and static functions, class templates



Webabcd


Introduced
Essential C + + of Windows Native

    • Dynamic creation and deallocation of objects
    • Assignment and replication of objects
    • static properties and Static functions
    • Class template



Example
1, Cppemployee 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. Dynamic creation and release of objects, assignment and copying of objects, static properties and static functions, class templates
CppClass3.h

#pragma<string>usingnamespace  std; namespace nativedll{    class  CppClass3    {    public:         string Demo ();    };}

CppClass3.cpp

/** Dynamic creation and deallocation of objects, assignment and copying of objects, static properties and static functions, class templates*/#include"pch.h"#include"CppClass3.h"#include"CppEmployee.h"using namespaceNativedll;voidcppclass3_demo1 ();voidCppclass3_demo2 ();voidCppclass3_demo3 ();voidCppclass3_demo4 ();stringCppClass3::D emo () {//dynamic creation and deallocation of objectsCppclass3_demo1 (); //assignment and replication of objectsCppclass3_demo2 (); //static properties and static functionsCppclass3_demo3 (); //class TemplateCppclass3_demo4 (); return "look at the code and the comments.";}//dynamic creation and deallocation of objectsvoidCppclass3_demo1 () {//dynamically created object: A new object that returns a pointer to the object (returns a value of 0 if it fails, and a macro #define NULL 0)Cppemployee *employee1 =NewCppemployee; Cppemployee*employee2 =NewCppemployee (1,"WEBABCD"); //the memory is freed by the delete pointer.    Deleteemployee1; Deleteemployee2;}//assignment and replication of objectsvoidCppclass3_demo2 () {Cppemployee employee1; Cppemployee Employee2 (1,"WEBABCD"); //Assignment of objects: objects of the same type can be assigned to each other. Assignment between objects is the assignment of object properties, and the function they call is still the same code, without assigning a valueEmployee1 =Employee2; //Copying objects: Copying an object from scratchcppemployee employee3 (EMPLOYEE1); Cppemployee employee4=employee1; /** The assignment of an object is to assign a value to an already existing object, and the object's copy is to create a new object from scratch .*/}//used to demonstrate static properties and static functionsnamespacenativedll{classcppemployeestatic { Public:        Static intVersion;//Static Properties        stringName; stringShow () {return  This-Name; }        Static intGetVersion ()//static Functions        {            //There is no this pointer in a static function            returnVersion; }        stringToString () {return "ABC"; }    };}//Initialize static properties (must be initialized, otherwise compile error)intCppemployeestatic::version =Ten;//use of static properties and static functionsvoidCppclass3_demo3 () {//static data members are allocated space at program startup (their allocation scheme is generated at compile time) and space is freed at the end of the programCppemployeestatic employee; Employee. Version= -;//support for accessing static properties in the same way as objects.    intVersion = Cppemployeestatic::version;//support for accessing static properties in the same way as class::version= Cppemployeestatic::getversion ();//support for accessing static functions in the same way as the class::Version = employee. GetVersion ();//support for accessing static functions in the same way as objects.}//for demonstration class templates (class templates like function templates, function templates see: CppFunction2.cpp)namespacenativedll{//Template <class T1, class t2>//declares a template with an indeterminate type named T (virtual type)Template <classT>//A class that uses a class template is a template class    classCppclass3compare {Private: T x, y;  Public: Cppclass3compare (t A, T b) {x=A; Y=b; } T Max () {return(x > Y)?x:y;    } T Min (); };}//Note: To define the member functions of the template class outside, write as followsTemplate <classT>T Cppclass3compare<T>:: Min () {return(x < y)?x:y;}//use of template classesvoidCppclass3_demo4 () {Cppclass3compare<int> Cmp1 (2, -); intA = Cmp1. Min ();//2    intb = Cmp1. Max ();// -Cppclass3compare<float> Cmp2 (6.26f,3.14f); floatF1 = cmp2. Min ();//3.14    floatF2 = cmp2. Max ();//6.26}



Ok
[SOURCE DOWNLOAD]

Essential Windows Native-C + +: Dynamic creation and deallocation of objects, assignment and copying of objects, static properties and static functions, class templates

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.