15. c ++ primer 4th notes, class and data abstraction, youyuan

Source: Internet
Author: User

1The member can be data, function, orType alias(TypedefDefined).

2,ConstMust appear in the declaration and definition at the same time.

3The number and type of parameters of the two overloaded members cannot be the same. You can alsoConst.

T & Display (STD: ostream & OS)
{
...
}
Const T & Display (STD: ostream & OS) const
{
...
}

4,InlineBoth the description and definition are valid.

5, Can declare a class without defining it, this declaration is sometimes called Forward Declaration(Forward Declaration)The declared class is an incomplete type.(Incompete type).

Incomplete types can only be used in a limited manner. Objects of this type cannot be defined.Incomplete types can only be used to define pointers and references pointing to this type; or they can be used to declare(Instead of defining)Use this type as the parameter type or return type function.

It is generally used to write mutually dependent classes.

6The class can be defined only after the class definition body is completed. Therefore, the class cannot have its own data members. However, when a class name appears, it can be considered declared. Therefore,The data member of a class can be a pointer or reference to its own type.

7Member functions cannot be definedThisParameter, which is implicitly specified by the compiler. Function bodies of member functions can be explicitly used.ThisPointer, but not required.

One scenario must be used:When we need an object as a whole reference rather than a member of the referenced objectIf a function returns a reference to the object that calls the function.

Class1 & class1: Set (char C)
{
...
Return * this;
}

8 In Const In member functions, This Is a type pointing to the class type Const Pointer. In Const In member functions, This Is a point Const The Const Pointer. Cannot start from Const The member function returns a normal reference to a Class Object and can only return * This As Const Reference.

ConstObject can only be usedConstMember, notConstThe object can use any member.

9, We want the data members of the class(IncludingConstMember Functions)You can modify them and declare themMutable.

10Generally, data or function members must be accessed through objects. Defines the type of members, and uses the scope operator for access.

11, The form parameter table and function body are in the class scope; the function return type is not necessarily in the class scope. If the function is defined outside the class definition body, the return type name is out of the class scope. If the returned type uses a type defined by the class, you must use a fully qualified name.

12The name must be defined before use. In addition, once a name is used as a type name, it cannot be repeatedly defined. It is different from defining the name of the inner variable of a local variable to overwrite the name of the outer variable.

13YouyuanFriendDeclaration, which can only appear within the class definition. It is a good idea to put it at the beginning or end of the class definition.

You must first define a class that contains a member function to set the member function as a friend. You do not need to declare classes and non-member functions in advance to set them as friends.

14Class must declare every function in the overload function set as a friend.

(1)The relationship between friends and Yuan is one-way, not two-way.

(2)The relationship between friends and friends cannot be passed. (AYesBFriends,BYesCBut not necessarilyAYesCFriends ).

(3)However, youyuan can inherit (this is the result I tested in vs compiler, but it cannot be inherited in C ++ primier ).

Example

# Include <iostream>
Using namespace STD;

Class data;
Class time
{
Public:
Time (INT, Int, INT );
Void display (Data &);
PRIVATE:
Int hour;
Int minute;
Int sec;
};

Class data
{
Public:
Data (INT, Int, INT );
Friend void time: Display (Data &);
PRIVATE:
Int month;
Int Day;
Int year;
};

Class date1: Public Data
{
Public:
Date1 (INT M1, int m2, int m3): Data (M1, M2, M3)
{
Cout <"in date1" <Endl;
}
};

Time: Time (int h, int M, int S)
{
Hour = h;
Minute = m;
SEC = s;
}
Void time: Display (Data & D)
{
Cout <D. Month <"/" <D. year <Endl;
Cout }
Data: Data (int m, int D, int y)
{
Month = m;
Day = D;
Year = y;
}
Int main ()
{
Time T1 (10, 13, 56 );
Data D1 (12, 25, 56 );
T1.display (D1 );
Date1 DD1 (1, 2, 3 );
T1.display (DD1 );
Return 1;
}

Reference

[1]Youyuan

Http://blog.163.com/zhoumhan_0351/blog/static/39954227201002621251123/

[2]Inheritance

Http://blog.163.com/zhoumhan_0351/blog/static/3995422720100284731826/

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.