[Note] C ++ class and Object

Source: Internet
Author: User
Tags date1

Class is the abstraction of objective things in the real world. It sums up a set of objects with similar static attributes and dynamic behaviors into a class.Class and object are the core of object-oriented technology.

1.1 class declaration and definition

Class Declaration Form:

 1   Class  <  Custom class type name  >  
2 {
3 [ Public :]
4 [ < Description of common members > ]
5 [ Private :]
6 [ < Private member description > ]
7 };

Class Definition

The member functions are defined as follows:

1 <Function Type> <Class Name>::<Function Name>(<Parameter table>)
2 {
3 Function body
4 }

Member functions can also be defined in the class.

1.2 objects

Object Declaration

The object declaration process is called class instantiation. First, define the class type, and then declare the general form of the class object:Class Name object table;

Class member access

In vitro, the access form of class members is:

<Object Name>. member nameOr<Pointer to Object>-> member name

For example:

1 Date date1, date2;
2 Date*Pdate= &Date2;
3
4 Date1.setdate (...); // method 1
5 Pdate->Setdate (...); // method 2

Access Control for Class Members

C ++ uses three keywords: public, private, and protectd to specify access restrictions for class members.

◆ Public members:ProgramCan be accessed anywhere. It is generally restricted to member functions, so that it acts as an interface between the class and the outside, and operates class objects through the country.

◆ Private Members: they can only be accessed by member functions of the class or friends of the class.

◆ Protected members: they can only be used within the class or their derived class questions.

The difference between a class and a struct: the default access control type of a class is private, while the default access control type of a struct is public.

1.3 constructor and destructor

1.3.1 Constructor

In C ++, object initialization is completed by constructors. Constructor is a special member function.

Features of constructor:

① The constructor name is the same as the class name.

② There is no description of any function return type.

③ When a new object is created, the constructor is automatically called by the compilation system to complete initialization.

④ If no constructor is provided in the class, the system provides a default non-argument constructor.

⑤ Constructors can be overloaded.

1.3.2 destructor

Memory space is allocated using new in the constructor. When you cancel an object, you need to use Delete to release the dynamically allocated space. This part of work can be placed in the destructor.

Destructor is a special public member function with the following features:

① Destructor name :~ <Class Name>.

② There is no description of any function return type.

③ Destructor have no parameters, so they cannot be overloaded.

④ If no destructor is provided in the class, the system provides a default destructor.

⑤ When a dynamic object is released using Delete, the system automatically calls the destructor to complete the processing before the object is revoked.

1.4 class static members

Add the keyword static before the class member, which is the static member of the class. Static members of a class are characterized:Static members belong to the class and do not belong to any object.Only one static member is saved in the public memory and is shared by all objects in the class.

1.4.1 static data member

Static data members must be defined outside the class. Definition:<Type> <class name >:< static data member name> [= <initial value>];

For example, double account: m_rate = 0.098;

Note: Do not add the keyword static before defining static data members. And can only be defined once.

Static data members of a class do not belong to any class.PublicThe general access format of static data members is:

<Class name >:: <static data member name>Or<Object Name>. <static data member name>Or<Object Pointer>-> <static data member name>

If the static data member is private, you can only use the static member function to indirectly access the static data member.

1.4.2 static data member

The static member function of the class is called in the form:<Class name >:: <static member function call>

$1.5

In a program, if a common function or a function in another class needs to frequently use the public interface that the class has heard to protect the private members of the class or the members, in order to improve the program running efficiency, you can declare them as friends of the class so that they can directly access any member of the class. Youyuan provides the ability to perform operations between common functions and class members and between different class members.Data sharing.

UseFriendThree types of friends can be declared for keywords:

(1) User Functions

The form of a friend function that declares a common function as a class is:Friend <data type> <youyuan Function Name> (parameter table)

For example, if the fun function is declared as a friend function of Class A, after the function is declared, the fun function has the right to access any member of Class A, including private and protected members.

1 ClassA
2 {
3 ...
4 FriendIntFun (IntX );
5 ...
6 }

(2) Friends

Declare a member function of a class as a member function of another class. The Declaration Form of a friend member is:Friend <type> <class name containing friends member >:< friends member name> (parameter table );

For example, a member function in Class A has the right to access any member in Class B, including private and protected members.

1   Class  A
2 {
3 ...
4 Int Fun ( Int X );
5 ...
6 }
7   Class B
8 {
9 ...
10 Friend Int A: Fun ( Int X );
11 ...
12 }

(3) youyuan class

The syntax form of declaring a class as a friend class of another class is:Friend <friend Class Name>;OrFriend class <friend Class Name>;

For example, any member function of Class A has the right to access any member of Class B, including private and protected members.

 
1 ClassB
2 {
3 ...
4 FriendClassA
5 ...
6 }

1.6 This pointer

Each class member function contains a pointer to the called object, which is the this pointer. This pointer is a special pointer hidden in every non-static member function. It is a class pointer type parameter of the member function.

Because of the existence of the this pointer, the same member functions in the class will operate data members of different objects based on the difference that this pointer points to the object.

The static member function of the class does not have the this pointer. This is because static member functions are shared by all objects of the class and do not belong to a specific object.

Two situations in which the this pointer is explicitly used:

(1) When the class object itself is returned in the non-static member function of the class, use "return * This" directly ".

(2) When the parameter and the member variable name are the same.

1.7 string class

The C ++ Standard Library provides a string class specifically used to process string operations. To use the string class, the program must contain the header file string (not a string. H, which is a C string header file ).

1.7.1 string class object initialization

The constructor of the string class includes:

View code

 1   String  ();
2   String ( Const Char * S); initialize the string as a string directed by S, ' \ 0 ' End identifier
3   String (Size_type N, Char C); n elements, each of which is C
4   String ( Const Sring & STR, size_type n = NPOs );
5   String ( Const Char * S, size_type N ); ...

1.7.2 string operations

View code

 1   =  , Assign ();
2 Swap ();
3   + = , Append (), push_back ();
4 Insert ();
5 Erase ();
6 Clear ();
7 Replace ();
8   + ;
9   = , ! = , < , <= , > , > = , Compare ();
10 Size (), length ();
11 Max_size ();
12 Empty ();
13 Capacity ();
14 Reserve ();
15 [], ();
16   > , Getline (); read
17   < ; Write
18 Copy ();
19 C_str ();
20 Data ();
21 Substr ();
22 Find ();

Today's pointer learning is here.

 

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.