Inheritance and enumeration in instance parsing C++/CLI

Source: Internet
Author: User
Tags abstract bool datetime inheritance visibility

In this paper, we will introduce the C++/CLI theme related to inheritance, and use the three forms of bank transaction in real life: deposit, withdrawal and transfer, to explain the inheritance system of class, and to implement it in a new form of enumeration.

Enumerator

See the type declared in Example 1, which exists in its own source file, and compiles to an assembly that contains only this type:

Example 1:

public enum class TransactionType :
unsigned char {Deposit, Withdrawal, Transfer};

As imagined, the deposit, withdrawal, transfer in the enumerator represent the constant values of 0, 1, and 2, but there are three aspects that make this enum type very different from the enum type of standard C + + (that is, "local enum").

The enum class is used only to replace an enum. This makes Transactiontype a CLI enum. (also allows the enum structure, which is equivalent to the Enum class.) )

• The accessibility of this type is public so that it can be visible from outside the parent class assembly. (in C++/cli, a local enum type can also have an access qualifier.) )

The enum class has an explicit base type qualifier: In this case, unsigned char. (in C++/cli, a local enum can also have a basic type.) By default, the base type is int. The basic type can also be arbitrarily shaped for either bool or other than wchar_t. (If you specify bool as the base type, the enumerator must be initialized explicitly because there is no default initial value.) )

This new syntax is supported because the CLI enum complies with the CLS Standard, while the local enum does not comply.

The biggest difference between a CLI enum and a local enum is the way in which the enumeration name is scoped by its parent class enum type. In addition, integer elevation defined in standard C + + does not apply to CLI enum.

Like a local enum, a CLI enum can also be defined in a class, in which case the access qualifier is not allowed because the visibility of the nested type is replaced by the type visibility to which it is embedded.

Abstract base class for trading

The inheritance system of transaction type in the base class transaction, the default is inherited from System::Object, see Example 2:

Example 2:

using namespace System;
using namespace system::threading;
/*1*/
Public ref class Transaction Abstra   CT
{
Transactiontype typeoftransaction;
/*2*/DateTime datetimeoftransaction;
Public:
/*3a*/the property transactiontype typeoftransaction
{
Transactiontype get () {return TYPEO Ftransaction;
Private:
void Set (Transactiontype value) {typeoftransaction = value;}
}
/*3b*/Property DateTime datetimeoftransaction
{
DateTime get () {return datetimeoftransaction;}
Private:
void Set (DateTime value) {datetimeoftransaction = value;}
}
/*4*/virtual void posttransaction () abstract;
Protected:
/*5*/Transaction (transactiontype transtype  
{
/*6*/thread::sleep (gcnew Random)->next (1000,2001));
/*7*/typeoftransaction = Transtype;
/*8*/datetimeoftransaction = Datetime::now;
}
;

In label 1, this class is marked abstract (abstract class), which means that it cannot be instantiated directly. (Abstraction is not a keyword and is reserved only in this context.) This abstract modifier can be used to define an abstract class without explicitly declaring one or more member functions as pure virtual types.

In the private data member portion of a class, a transaction contains a transaction type and a timestamp of a timestamp, both of which are accessed by attributes defined in the label 3a and 3b. CLI Library value types used in label 2 system::D Atetime allows you to display the date and time of the day with an immediate variable. Notice how the two attributes have a public get method and a private set method. (This is based on the new CLI Standard and is now CLS-compliant.) )

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.