Instructor Wang C ++

Source: Internet
Author: User

Basic Concepts

1. Classes are data types and promote the concept of structure.

2. Declaration Syntax: class name;

3. Definition Syntax:

Class name {

Private: member declaration | definition;

Public: member declaration | definition;

Protected: member declaration | definition;

};

Member function definition:

In the class body (inline Function): Value Type Function Name (form parameter table) function body

In vitro (must be declared in the class): Value Type Class Name: function name (form parameter table) function body

The content in the public area is the class interface.

1. constructor: used to initialize objects.

Definition Syntax:

Class Name (parameter table) function body

2. copy constructor: used to construct another object based on one object.

The following problems may occur when copying images:

(1) space pointed by the Public pointer;

(2) free may cause problems during Structure Analysis (multiple releases of the same space ).

Deep copy, write by yourself.

Default copy constructor:

Class Name (const Class Name & Parameter Name) function body

Call time:

A. Use one object to initialize another object;

B. When the parameter is of the class type, the parameter is passed;

C. Return objects of the class type.

3. destructor: automatically called when the Object exits the lifetime.

Definition Syntax:

~ Class Name () function body

 

Binary object (class type variable)

Definition Syntax:

Class name variable name; // default constructor in the class

Class name variable name = expression; // There is a one-dimensional Constructor

Class name variable name = Constructor (real parameter table );

Class name variable name (real parameter table); // The real parameter table cannot be blank

Recommended: 1st and 4th. For example:

# Include <iostream>
Using namespace std;

Class
{
Private:
Int x;

Public:
A (int x = 0 ){
This-> x = x;
}
Void f (){
X = x + 1;
}
Int g (){
Return x;
}
};

Int main ()
{
A a = 1;
A. f ();
Cout <a. g () <endl;

A B = A (2 );
B. f ();
Cout <B. g () <endl;

A c (3 );
C. f ();
Cout <c. g () <endl;

A d;
D. f ();
Cout <d. g () <endl;

Return 0;
}

1. Each object has its own set of data members, and all objects share a set of member functions.

2. Member references:

Object Name. Data Member

Object Name. member function (real parameter table)

3. this pointer

Each member function has an implicit parameter named this. Its type is pointer to this class and its value is the address of the current object.

4. Access Permissions

Public members can be accessed anywhere in the program. Private, protecting members can only be used within the class.

The class here refers to the names of the member functions defined by the class body & class definition to the end of the definition.

3. const object and const member functions

Const object

Definition Syntax: const class name Object Name (real parameter table );

Const member function: it is declared as const without modifying the member function of the data member.

Definition Syntax: Value Type Function Name (parameter table) const function body

Four static members

Static data member: data member shared by all objects

Declaration Syntax: static type name member name;

Definition Syntax: type name Class Name: member name = initial expression;

Static member function: only refers to the function of the static data member, declared as static (no this pointer)

Definition Syntax: static Function Definition

Call: Class Name. member function (real parameter table); Class Name. Member

Five Nested classes (internal classes)

The class declared in the class. Example:

# Include <iostream>
Using namespace std;

Class C
{
Private:
Int x;
Void f_c (){
Cout <"private fun in C is called! "<Endl;
}
Public:
Class B {
Private:
Int y;
Void f_ B (){
Cout <"B's private fun is called! "<Endl;
}
Public:
Void fun_ B (C c ){
C. f_c ();
Cout <"C's private data:" <c. x <endl;
}
B (int y ){
This-> y = y;
}
};

C (int x ){
This-> x = x;
}
Void fun_C (B B ){
// Error C2248: 'C: B: y ':

// Cannot access private member declared in class 'C: B'
// Cout <"B's private data:" <B. y <endl;

// Error C2248: 'C: B: f_ B ':

// Cannot access private member declared in class 'C: B'
// B. f_ B ();
}
};

Int main ()
{
C c (5 );
C: B B (3 );
B. fun_ B (c );
C. fun_C (B );
Return 0;
}

 

Program output:

Private fun in C is called!
C's private data: 5

It can be seen that the internal class can access the private variables and private functions of the external class, but the external class cannot access the private Members of the internal class, which is consistent with the visible scope of the private class.

Internal classes are used to form two logically related classes, such as linked list and node classes, container classes, and iteration subclasses.

Liuyouyuan

It is not a member of the class, but can be a private member of the category.

Definition Syntax:

Friend class name;

Friend Class Name: member function name (parameter table );

Friend function name (parameter table); // global function

The preceding code can be displayed in any zone (private, public, and protected ).

<Supplement>
If you do not write the copy constructor or copy constructor, the compiler automatically generates the corresponding default function in the bitwise copy mode. If the class contains pointer members or reference members, the two default functions are implicitly incorrect.
For example:
String a ("hello ");
String B ("world ");
String c = a; // call the copy constructor. It is best to write it as c ();
C = B; // call the value assignment function

The copy constructor and the copy assignment function are easy to confuse. The copy constructor is called when an object is created on the slave node and initialized with another existing object, the value assignment function can assign only one object to another existing object.

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.