Object-oriented base lecture 1

zhaozj2021-02-08  244

The constructing is used to build an object from zero.

Constructing a "initialization function"; it makes a bunch of bytes into a living object. Minimum

It will initialize the internally used field, or you may configure the resources required (memory, profile, semaphore)

, Socket, etc.).

"CTOR" is the most common abbreviation for constructing subcutor.

=========================================

Q18: How can I make the construction of the scratch call another room in the same room?

There is no way.

The reason is: If you call another construct, the compiler will initialize a temporary regional object; but not

There is initialization "this" you want. You can use the default parameter,

The built-constructed subscription or share their program code in the private "init ()" member function.

=========================================

Q19: What is DESTRUCTOR?

The deconstruction is the funeral of the object.

The deconstruction is used to release the resources configured by the object, such as: Lock category may lock a

Semaphore, the deconstruction is used to release it. The most common example is: When the construction of the bunglery uses "new",

The structure is "delete".

The deconstruction is a "Method", usually abbreviated as "DTOR".

=========================

■ □ Section 6: Multi-load operation

=========================

Q20: What is Operator OVERLOADING?

It allows people who use categories to operate in intuition.

The operation of the operation is multi-loaded to make C / C operand the number of operations (object categories) to self-reserve. it

We shape the syntactic sugar: SyntActic Sugar:

Class fred {

PUBLIC:

// ...

}

#if 0

Fred add (fred, fred); // No operational number of operations

Fred mul (fred, fred);

#ELSE

Fred Operator (Fred, Fred); // has a multi-load operation

Fred Operator * (Fred, Fred); # ENDIF

Fred f (Fred A, Fred B, Fred C)

{

#if 0

Return Add (ADD (Mul (A, B), MUL (B, C)), MUL (C, A)); // Nothing ...

#ELSE

RETURN A * B B * C C * a; // has ...

#ENDIF

}

=========================================

Q21: Which transport operands can / can't be multi?

Most of them can be multi.

The number of cirked cystects is "" "and"?: "(And technically, it can be" SIZEOF ").

C adds some of its own operations, which can be multi-load except "::" and ". *".

Under the bottom, it is an example of the number of operations (it will return a reference). The top is "no need

"Multiple:

Class array {

PUBLIC:

#if 0

INT & ELEM (unsigned i) {if (i> 99) error (); return data [i];

#ELSE

INT & Operator [] (unsigned i) {if (i> 99) error (); return data [i];

#ENDIF

Private:

Int Data [100];

}

Main ()

{

Array A;

#if 0

a.lem (10) = 42;

a.Elem (12) = a.elex (13);

#ELSE

a [10] = 42;

a [12] = a [13];

#ENDIF

}

=========================================

Q22: How to make a "**" "secondary" transportation number?

No solution.

The name, priority, combined law, and magnitude of the operand are killed by the language. No in C

"**" operates, so you can't order one of it.

I still doubt, consider looking at "x ** y" and "x * (* y)", the two are exactly the same (in other words

The compiler assumes that "Y" is a pointer). In addition, the operation of the operation is only the syntax coating of the function call.

Although it is sweet, there is no increase in nature. I suggest you "POW (Base, Exponent"

This function (it's the accuracy version in ).

With one mention: Operator ^ can be used, but its priority and combined law does not match the "second party". ===================

■ □ Section 7: Partner

===================

Q23: What is a partner (Friend)?

Let other categories or functions can access things inside your category.

Partners can be a function or other category. Category will open access to its partners. Under normal circumstances, programmers

The awareness, technical control of this category (otherwise, when you want to be a category,

There are other parts of the owner's consent.

=========================================

Q24: "Partners" violate the encapsulation?

If the good use, it will "strengthen" encapsulation.

We often have a category to two halves, and when these two semi-different cases are different and their life. In this case

Under the shape, they usually need to access the inside of the other party (these two halves "is in the same category,

So you didn't "increase" the number of operational behaviors of accessing the data structure; you are just "moving" these operational behaviors

The situation is there.). The safest way to make these two hairs each other's "partners".

If you use a partner as described above, you are still keeping private things in private. Encounter the above feelings

In the case, if you still want to avoid the use of partnerships, many people are not using public information (bad!),

It is a public GET / SET access operation behavior to access each other's information, in fact, these have destroyed the package.

Sex. Open only when the private information is "still having its meaning" outside the category (in the user's perspective)

The access operation behavior of private information is called proper practices. In most cases, "Access the operation behavior"

Just as "public information": they only hide their "name" for private information, but they have not hidden "

exist".

Similarly, if "Partner Function" is the grammar of another kind of public access function, then the packaging

Sex a member function destroys the package. In other words, the partners and members of the object category are "packaged boundaries"

", Like" Category Definition "itself.

=========================================

Q25: Advantage of partner functions?

It provides freedom in some interface design.

Member functions and partner functions have equal access privileges (100% right), the main difference is: partners

The function is used as "F (x)", and the member function is "x.f ()". Therefore, the partner function allows object categories designers to choose his most pleasant grammar to reduce maintenance costs.

The main drawback of partner functions is: when you want to do dynamic binding, it needs additional

The program code. Want to make a "virtual partner" effect, the partner function should call a hidden (usually put it

"protected:" 里) Virtual member function; like this: "Void F (Base & B) {B.DO_F ();}"

. Derivative category coverage ("Void Derived :: DO_F ()") ")

Not the partner function.

=========================================

Q26: What does "partnership have no inheritance and delivery"?

The privilege of partnership cannot be inherited: partners' derivative categories are not necessarily partners (I will take you as a friend

But this does not mean that I will definitely trust your child). If the "Base" category declared "F ()" for it

Partners, "F ()" does not automatically discomfort over some "derived" categories derived from "Base"

Have special access to power.

Nothing of partnership: Partnership partners are not necessarily partners for the original category (friends of friends

It is also a friend). For example, if the "Fred" category announced the "Wilma" category for its partner,

"Wilma" category announced "F ()" for its partner, "f ()" is not necessarily special access to "Fred"

that power.

=========================================

Q27: Should I declare a member function for category or a partner function?

Possible words, use member functions; if necessary, use partners.

Sometimes in grammar, your partners are better (such as: in the "Fred" category, the partner function can put "fred"

It is a second parameter, but it must be placed in the member function). Another good example is: binary

The number of monogram calculations (such as "ACOMPLEX ACOMPLEX" may define a partner rather than a member letter

Number, because you want "Afloat Acomplex" to be established; recall: member functions

The method enhances the parameters on the left, because it will change the likelihood of the object to which the object to which the member function will be changed. In other cases, please select the member function instead of partner functions.

============================================================================================================================================================================================================= ==

■ □ Section 8: Input / Output: and

============================================================================================================================================================================================================= ==

Q28: How to provide output function for "class fred"?

Partner function Operator <<:

Class fred {

PUBLIC:

Friend Ostream & Operator << (Ostream & O, Const Fred & Fred)

{RETURN O << fred.i;}

// ...

Private:

INT i; // only set to illustrate

}

We don't use the member function as a partner, because "fred" is the second parameter rather than the first. Enter the function

Similar, just rewritten:

iStream & Operator >> (ISTREAM & I, FRED & FRED);

// ^^^^^ ------- is not "const fred & fred"!

=========================================

Q29: Why should I use instead of former ?

Increased types of security, reduce errors, enhance efficiency, ductility, provide derivative capacity.

Printf is ok, and scanf is still possible in addition to easier to write, but the I / O system of C

They have their limitations. C I / O ("<<" and ">>"), and c ("Printf ()" and

"Scanf ()") compared to:

* Type Safety - To do I / O objects, the compiler will know its model in advance, rather than dynamically, by "%" column.

* Not easy to errors - redundant information will increase the opportunity. C I / O does not require an excess "%".

* Faster -Printf is a "interpreter" in a small language, which is mainly "%".

Posing; in executing it uses these fields to select the correct formatting method. C I / O system is

Static selection of subprofits according to the true type of each variable to enhance the performance efficiency.

* Demonstration - C I / O mechanism can be added to the user without changing the original program code.

The type of state (can imagine if everyone is not compatible with "%" fields into Printf and Scanf, will

What kind of chaotic scene is it?!).

* Subclassable - Ostream and iStream (File * replacement items) of Ostream and ISTREAM

The real category can therefore be derived. This means: you can make other custom things with

The appearance and behavior of Stream mini, but actually doing the specific thing you want to do. You are automatically reuse

Tens of thousands of others (you don't even know them) write a good I / O program code, and they don't need to know

The "Extended Stream" category you did.

=========================================

Q30: Why do I have the end of the file when I handle the input?

Because the state of the EOF is to "the action that will exceed the file" will be set. Also

It is to say that the last byte of the read file does not set the state of EOF.

[Translation] This is also a common error.

If your program is like this:

INT i = 0;

While (! cin.eof ()) {

CIN >> X;

i;

// Work with x

}

Your I variable will be more than one.

What you really do is this:

INT I;

While (CIN >> X) {

i;

// Work with x

}

=========================================

Q31: Why is my program executing the first loop?

Because the read value is read, the non-digital character is left in the input buffer (INPUT buffer).

[Translation] This is also a common mistake of C, or even Pascal.

If your program is as follows:

Char Name [1000];

Int agec;

For (;;) {

COUT << "name:";

CIN >> Name;

COUT << "Age:";

CIN >> AGE;

}

You should write this:

For (;;) {

COUT << "name:";

CIN >> Name;

COUT << "Age:";

CIN >> AGE;

Cin.Ignore (int_max, '/ n');

}

=========================================

Q32: How do you come "Reopen" CIN and COUT in DOS and OS / 2 Binary mode?

There is this problem, the most typical situation is: someone wants to make Binary I / O for CIN, COUT, but made

Industrial systems (like DOS or OS / 2), but always do CR-LF conversion actions.

Solution: CIN, COUT, CERR These prior defined streams, are the stream of Text, no standard

Fat can make them binary patterns. Try the stream to try again to reopen in binary mode,

It can lead to unpredictable results.

There are different behaviors of different behavior in these two models, and there must be a way to turn them into binary streams, but you have

Check the files of the system.

转载请注明原文地址:https://www.9cbs.com/read-2850.html

New Post(0)