Indispensable Windows Native (19), native19

Source: Internet
Author: User
Tags define null

Indispensable Windows Native (19), native19

[Download source code]


Indispensable Windows Native (19)-C ++: dynamic creation and release of objects, assignment and copying of objects, static attributes and static functions, class templates



Author: webabcd


Introduction
Windows Native C ++ is indispensable

  • Dynamic Creation and release of Objects
  • Assignment and copying of Objects
  • Static attributes and static functions
  • Class Template



Example
1. CppEmployee class
CppEmployee. h

# Pragma once # include <string> using namespace std; namespace NativeDll {class CppEmployee {int Number; // The default value is private: // the following values are private string names; bool IsMale; protected: // The following are the string ToString () of protected; public: // The following are all public float Salary; int Age; string Show (); // constructor ), if this parameter is not specified, a default non-parameter constructor will be automatically generated during compilation. (If this parameter is set to private, direct instantiation is prohibited, this is usually done in the singleton mode) // In addition, if a constructor with parameters is declared, The default non-parameter constructor CppEmployee (); CppEmployee (int number, string name = "webabcd") is not automatically generated; // you can specify the default value for parameters in the constructor (refer: cppFunction1.cpp) CppEmployee (int number, string name, bool isMale); // destructor, called when the object is destroyed, such as releasing the dynamically allocated memory. If this parameter is not required, it can be left unspecified. during compilation, a default no-action destructor is automatically generated. The Destructor name is the same as the class name. ~ CppEmployee (); private: // Note: A member function defined in the body, which is automatically processed as an inline function (for inline functions, see CppFunction2.cpp) void Temp () {int a = 100;}/* The following is the same as the above inline void Temp () {int a = 100 ;}*/};}

CppEmployee. cpp

/** CppEmployee class */# include "pch. h "# include" CppEmployee. h "# include" cppHelper. h "using namespace NativeDll; //": "Is the field qualifier string CppEmployee: Show () {return int2string (Number) +" "+ Name ;} string CppEmployee: ToString () {return int2string (Number) + "" + Name;} // constructor without parameters CppEmployee: CppEmployee () {Number = 888; name = "webabcd";} // constructor with parameters. You can specify the default value CppEmployee: CppEmploy for the parameter in the Declaration. Ee (int number, string name) {Number = number; Name = name;} // you can use the following simple method, assign the parameter values in the constructor to the object's variable CppEmployee: CppEmployee (int number, string name, bool isMale): Number (number), Name (name), IsMale (isMale) {} CppEmployee ::~ CppEmployee (){}


2. Demonstrate the dynamic creation and release of objects, assignment and copying of objects, static attributes and static functions, and class templates.
CppClass3.h

#pragma once #include <string>using namespace std;namespace NativeDll{    class CppClass3    {    public:        string Demo();    };}

CppClass3.cpp

/** Dynamic creation and release of objects, assignment and copying of objects, static attributes and static functions, class templates */# include "pch. h "# include" CppClass3.h "# include" CppEmployee. h "using namespace NativeDll; void cppclass3_demo1 (); void cppclass3_demo2 (); void cppclass3_demo3 (); void publish (); string CppClass3: Demo () {// dynamic creation and release of an object cppclass3_demo1 (); // assign values to and copy cppclass3_demo2 (); // static attributes and static functions cppclass3_demo3 (); // class template cppclass3_demo4 (); return "view code and comment";} // dynamic creation of Objects Build and release void cppclass3_demo1 () {// create an object dynamically: a new object, returns a pointer to this object (if the object fails, the return value is 0, and the corresponding macro # define NULL 0) cppEmployee * employee1 = new CppEmployee; CppEmployee * employee2 = new CppEmployee (1, "webabcd"); // The delete pointer is used to release the memory delete employee1; delete employee2 ;} // assign values to and copy objects void cppclass3_demo2 () {CppEmployee employee1; CppEmployee employee2 (1, "webabcd"); // assign values to objects of the same type: objects of the same type can assign values to each other. The value assignment between objects is the value assignment of object properties. The functions they call are still the same code, and the value of Employee 1 = EMPLOYEE 2 is not required. // copying an object: copy an object CppEmployee employee3 (employee1) from scratch; CppEmployee employee4 = employee1;/** assign values to an existing object; copying an object creates a new object from scratch * // It is used to demonstrate static attributes and the static function namespace NativeDll {class CppEmployeeStatic {public: static int Version; // static attribute string Name; string Show () {return this-> Name;} static int GetVersion () // No this pointer re in the static function {// Turn Version;} string ToString () {return "abc" ;};}// initialize static attributes (Initialization is required, or compilation fails) int CppEmployeeStatic: Version = 10; // static attributes and static functions void cppclass3_demo3 () {// static data members are allocated space when the program starts (the allocation scheme is generated during compilation ), space is released when the program ends CppEmployeeStatic employee; employee. version = 100; // supports objects. to access the static attribute int version = CppEmployeeStatic: Version; // supports class: To access the static attribute version = CppEmployeeStatic: GetVersion (); // supports class:: To access the static function v Ersion = employee. getVersion (); // supports objects. access static functions} // used to demonstrate class templates (class templates are similar to function templates. For function templates, see CppFunction2.cpp) namespace NativeDll {// template <class T1, class T2> // declare a template with an indefinite type (virtual type) named T) template <class T> // the class that uses the class template is the template class CppClass3Compare {private: T x, y; public: CppClass3Compare (T a, T B) {x =; y = B;} T Max () {return (x> y )? X: y;} T Min () ;};// Note: To define a member function of the template class outside, to Write template <class T> T CppClass3Compare <T >:: Min () {return (x <y )? X: y;} // use void cppclass3_demo4 () {CppClass3Compare <int> cmp1 (2, 14); int a = cmp1.Min (); // 2 int B = cmp1.Max (); // 14 CppClass3Compare <float> cmp2 (6.26f, 3.14f); float f1 = cmp2.Min (); /// 3.14 float f2 = cmp2.Max (); // 6.26}



OK
[Download source code]

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.