C + + syntax fourth custom type

Source: Internet
Author: User

I need a type that has both a place where int is stored and a place where bool is stored.

struct syntax is required at this time.

struct ib//type name {    int  n;//content     bool  b;};

This type is called structure and is created with the struct keyword

Grammar:

struct struct Name {     Underlying data type variable name;  }

Statement:

IB IB;

Assignment value:

ib={,true};

Call:

int na; BOOL  = {1,true== ib.b;

Once again we are asking for a new request, and I need a type with built-in method (processor) with several properties (storage). May I?

In fact, +-*/is their processor for the underlying numeric type.

For a custom type, this type, called a class, is also called an object.

Create classes with the Class keyword.

class a                       //declaration section {        int  A;         int Add (intint  m);  } int A::add (int n,int  m)       //completion of the declared method body {        return n+m;}

In general, we need to initialize the properties of class, and to release a related resource from memory in case a is not allowed.

So a class has the following format

classA//Declarations Section{A (); //initialization function~a ();//destroying functions        intA; intAddintNintm); }intA::add (intNintM//complete the declared method body{        returnn+m;} A::a () {A=Ten;//Initialize Properties}a::~A () {//There is no need to release the content}

Statement:

A MA;

Call:

int N; int a,b,c;a=ten; b== ma.a;                        // Call Property C = Ma.add (A, b);             // Calling Methods

C + + syntax fourth custom type

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.