C + + re-repair (i) ———— a header file to explain some of the concepts of the whole program

Source: Internet
Author: User

This is the Czech Republic version of the C + + object-oriented advanced development course, it feels good, write down

the wording of the header file: (Defensive statement)

The header file has a very formal wording, it must be learned:

#ifndef _complex_ #define _complex_           ..... #endif

A header file is written first (as above), why write this thing? Because there are many programs, to use your this header file, or you may also use, if there are many header files, the user in the call (include), will appear in the end of the first call that header file problem, the header file a lot of words, it will be difficult to platoon, so this defensive statement, very useful. The general principle is: But when the machine executes, the first line of code tells the compiler to say: "If you have not defined the" _complex_ "thing, then define it and then proceed to the following, so, when the program first calls (include) this header file, It is easy to define, in the same function, the second time (include), because "_complex_" has been defined, it will not come in, the following code will not be executed again, so that the header file can be avoided repeated introduction.

layout of the header (header file):

Then start writing the middle of the Content section, first write the first part:

class declaration (Declaration):

Any class must have a "head" (class head), in which you need to design, my complex number should have what kind of data, I should be prepared what kind of function, to meet the needs of people who use complex numbers, for example, we all know that mathematics has a conjugate plural, Then I should write a function of a conjugate complex like this here. That's how it ends.

classComplex/*.... <-class head, "complex" is also the name of the class, as well as the same as the names of the classes.*//*... the class body is as follows:-- -----.....?????*/{  Public: Complex (DoubleR =0,Doublei =0): Re (r), Im (i) {} complex& Opeartor + = {Constcomplex&}; DoubleReal ()Const{returnre;} DoubleImag ()Const{returnim;} Private:   DoubleRe, im;/*there are real and imaginary parts in the plural, so I'll define it artificially.*/Friend Complex& __DOAPL (complex*,Constcomplex&)}; 

The usage in the function is:

{    complex  C1 (2,1);    Complex  C2;    . . .}

Inline function: It is best to write most functions in the form of inline, "{return re;}" in the previous example; {return im;} " Forms the inline function, which, if defined within the class body, automatically becomes the inline function, which is not an inline function if defined outside. However, it is important to note that if the function is too complex, there is no way to inline it. For example, for these two functions, we designed the inline function, but it is not compiled by inline, which is the compiler thing.

If the inline function is defined under a non-class body, an example is as follows:

Double imag (const complex&  x) {    return  x.imag  () ;}

In a class body, it is obvious that the area can be divided into several sections, and what is the difference between them? It is "public" (open, seen by the outside world) and "private" (only in my class can see). So what can be put into "private"? Of course it's part of the data, because the data we need is encapsulated and not seen by the outside world. What about the function part? There are two kinds of functions, one for the outside world, one to handle their own personal affairs, and in this case, most of the functions are to be used by the outside world, so all functions are placed in "public". These passages can be staggered, think of a write "public", and then think of a write "private" order, or can only write two paragraphs.

So what's the impact?

As an example:

{    complex  C1 (2,1);    cout  <<  c1.re;    cout  <<  c1.im;}

This is wrong because "re" and "IM" are defined in "private" and are not read.

And so write:

{    complex  c1{2,1};    cout  <<  c1.real ();    cout  <<  c1.imag ();}

Yes, because the "real" and "imag" functions in "public" are called.

In conclusion, we refer to the area "public" and "private" that they form, called the access level.

If you write a function that is intended to be called by the outside world, you put it in "public", and if you write a function that is only intended to be used internally and not intended to be called by the outside world, you put it in "private".

In C + +, if you want to create a thing (that is, an object), there is a function that is automatically called and does not need to be called manually to automatically get up. This is called a constructor, for example, the following example:

{    Complex C1 (2,1);    Complex C2;     New Complex (4);    . . .}

All three of them are creating objects and creating objects, there must be a function inside.

This constructor is very unique, its function name, must be the same as the function name of the class, can be called a constructor, and, it can be used with parameters, in the class body inside the complex header file Definition section. Naturally think of the real part and the imaginary part two parts to define.

  Complex  (double0double0)       :    re  (R),  Im  (i)/* This line is initialized, only the constructor, which means R is assigned to re, and I to im*/   {  }

Where both "r=0" and "i=0" are the default arguments (Defaults argument). Just like the C1 in the above has a specified parameter, that is, using it to specify that the C2 does not specify the parameters, then use the class body in the definition of the default arguments OK

Make full use of the characteristics of each function is to determine whether to write the atmosphere of the symbol, above is a model of the atmosphere!

Constructors can have a lot of-overloading (overloads), for example, you design a class, waiting for the outside world to create an object, how do you create an object? Suppose you have three design ideas, then you write three constructors, C is not allowed to do so, but C + + is allowed. The same function name, but more than one, this is called overloading

As an example:

void Real (double R)   (RE=R;)

This definition is also possible, although, the class body inside the real conflict, they are not the same machine code.

But if this is the case:

Complex  (double0double0)       :    re  (r),  im  (i)   {  }complex   ()    :   Re (0), Im (0)  {}

Because of the duplicate name, there will be conflicts.

function overloading, which generally occurs in constructors.

* Class template (templates)

In the above class, the real and imaginary parts are manually defined as double values, so if I want to design another complex number, which type is floating point (float), or an integer, what should I do? But the above has been written dead, can not move, then had to re-play the original program as it is, only to define the real and imaginary parts, change, so write too much trouble, so, C + + designers, invented for something similar to this situation ———— class template (template), Write: The position of the real part and the imaginary part not first to die, I want to use later, I specify

Template<typename t>/*tell the compiler in advance that T is a type*/classcomplex{ Public: Complex (DoubleR =0,Doublei =0): Re (r), Im (i) {} complex& Opeartor + = {Constcomplex&}; DoubleReal ()Const{Retrun re; }    DoubleImag ()Const{returnim;}Private: T Re, im;/*the meaning of T can be understood as telling the compiler: what type I am now, I have not decided yo! */Friend Complex& __DOAPL (complex*,Constcomplex&)};

The usage in the function is:

{    complex<double>  C1 (2.5,1.5);    Complex<int>  c2 (26);    . . .}

C + + re-repair (i) ———— a header file to explain some of the concepts of the whole program

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.