Learn C ++ and objects from scratch: class declaration, Class scope, Forward Declaration, this pointer, nested class, pimpl techniques, etc.

Source: Internet
Author: User

I. Class Declaration

// A class is a user-defined type. The declaration form is as follows:
Class Name
{
Public:
Public Member (external interface)
PRIVATE:
Private member
Protected:
Protect members
};

After the public keyword, declare that they are class and external interfaces. Any external function can access public data and functions.
After the keyword private is declared, only function access in this class is allowed, and no function outside the class can be accessed.
After the keyword protected is declared, similar to private, the difference is that the inheritance and derivation have different effects on the derived class.

C ++ code
1
2
3
4
5
6
7
8
9
10
11
Class clock
{
Public:
Void display ();
Void Init (INT hour, int minute, int second );

PRIVATE:
Int hour _;
Int minute _;
Int second _;
};


Assume that a clock class is defined. Because the members are private, the clock; ck. hour _ = 12; is incorrect. Define a public void sethour (INT hour) to set the hour _ value.


Ii. overload of inline member functions and their default parameters

Here we have the concept of inline functions. Member functions can also be inline. If they are implemented within the class, the inline keyword can be added or not added. For external class implementations, inline needs to be added,

For example, inline void clock: sethour (INT hour ){}. In fact, even if inline is added, it is not a fixed macro expansion. For example, if a switch is encountered, the for statement is usually not used.

In addition, member functions can be reloaded as normal functions, or have default parameters. For more information, see here.


Iii. Classes and struct

Difference between class and struct: when no access permission is specified, the class is private by default, and the struct is public by default,

Struct Test
{
Int X; // public
...
};

In addition, test can act as a tag independently, instead of using struct test as a Type in C language.


4. Implicit this pointer

A member function has an implicit appended parameter, that is, the pointer to the object. This implicit parameter is called the this pointer (automatically transmitted by the compiler)
The this pointer ensures that each object can have data members with different values, but the code for processing these Members can be shared by all objects.

A member function is a read-only code that is shared by all objects and does not occupy the storage space of objects. Because this pointer points to the current object, a member function can distinguish the object to which it acts.


V. Category scope and Forward Declaration

(1) Each class defines its own scope as the class scope. The identifiers described in the class scope are only visible in the class. In addition to class scopes, there are also block scopes, file scopes, function prototype scopes, and function scopes. For example:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Include <iostream>
Using namespace STD;

Class Test
{
Public:
Int num _;
};

// Num _ = 20; error, num _ scope within the class
Int num _ = 20; // The scope of num _ is the file scope. It is different from the scope of num _ in the class.

Int add (int A, int B); // the scopes of the two identifiers A and B are function prototype scopes.

Int main (void)
{
Int num _ = 30; // num _ is the block-based domain.
{
Int num _ = 100; // num _ is the block-based domain.
}

Cout <num _ <Endl;
Cout <: num _ <Endl;
Return 0;
}

Int add (int A, int B) // The parameters A and B are also block scopes.
{
Return A + B;
}

Int test ()
{
Label1: // function Scope
Cout <"label1" <Endl;
Goto label3;
Label2:
Cout <"label2" <Endl;
Goto label1;
Label3:
Cout <"label3" <Endl;
Goto label2;
}

(2) classes in C ++ must be defined before they can be instantiated. When two classes need to reference the header file to form a "Circular" reference, an error occurs. In this case, the forward declaration is required. The class declared in the forward direction cannot be an instance, but a pointer or reference can be defined.

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Ifndef _ B _h _
# DEFINE _ B _h _

Class;

Class B
{
Public:
B (void );
~ B (void );

Void fun (A &)
{

}

A * A _; // The class declared in the forward direction cannot be instantiated.
};

# Endif // _ B _h _

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Ifndef _ a_h _
# DEFINE _ a_h _

# Include "B. H"
Class
{
Public:
A (void );
~ A (void );

B _;
};

# Endif/_ a_h _

6. nested and local classes

(1) Nested classes

The nested class object must be used as the underlying implementation of the peripheral class. The nested class is only used for the implementation of the peripheral class and can be hidden from the user.

From the scope perspective, the nested class is hidden in the peripheral class, and the class name can only be used in the peripheral class. If the class name is used outside the scope of the peripheral class, the name must be specified.

Member functions in a nested class can be defined in the external body of the class.

The member functions of the nested class have no access to the private members of the peripheral class, and vice versa.

Nested classes are just syntactically embedded.

(2) Local class

Classes can also be defined in the function body. Such classes are called local classes ). The local class is visible only in the defined local domain.

The member functions of a local class must be defined in the class body.

There cannot be static members in a local class. We will discuss the static members and static member functions in the class later.

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Include <iostream>
Using namespace STD;

Class Outer
{
Public:
Class inner
{
Public:
Void fun ();
//{
// Cout <"inner: Fun..." <Endl;
//}
};
Public:
Inner OBJ _;
Void fun ()
{
Cout <"outer: Fun..." <Endl;
OBJ _. Fun ();
}
};

Void outer: inner: Fun ()
{
Cout <"inner: Fun..." <Endl;
}

Void fun ()
{
Class localclass
{
Public:
Int num _;
Void Init (INT num)
{
Num _ = num;
}
Void display ()
{
Cout <"num =" <num _ <Endl;
}

// Static int num2 _; // static members cannot be defined inside a local class
};

Localclass lC;
LC. INIT (10 );
LC. Display ();
}

Int main (void)
{
Outer O;
O. Fun ();

Outer: inner I;
I. Fun ();

Fun ();
// Localclass lC; error. The local class can only be used in the function body that defines it.
Return 0;
}


VII. pimpl Techniques


Let's look at the following example:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// File Y. h
# Include "x. H"
Class y
{
Void fun ();
X _;};
// File Y. cpp
# Include "Y. h"
Void Y: Fun
{
Return X _. Fun ();
}
// File main. cpp
# Include "Y. h"
Int main (void)
{
Y;
Y. Fun ();
}

The preceding program has the following problems:

1. Introduce more header files to reduce Compilation speed

2. During the compilation period, if the size of x changes, Y. cpp and Main. cpp must be re-compiled. During the runtime, if X has sub-classes, no multi-state virtual function can be used.

3. Suppose y. CPP is compiled into a dynamic library for Main. CPP is used. When the size of x changes, the dynamic library needs to be re-compiled. CPP is also required because it has a defined object y.

Recompile.


The following describes a pimpl technique. Some people regard it as a design mode:


Pimpl (Private implementation or pointer to implementation) is also called Handle/body Idiom

The idea behind pimpl is to isolate the customer from all the knowledge about the private part of the class. Avoid other classes from knowing their internal structure

Reduces compilation dependencies and speeds up re-Compilation

Separation of interfaces and Implementations

Reducing the Coupling Degree of modules

Compilation phase

Runtime

Improved interface stability

Methods cannot be changed for library usage

For library compilation and dynamic library changes, the customer program does not need to be re-compiled.

Modified program:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// File Y. h
Class X;
Class y
{
Y ();
~ Y ();
Void fun ();
X * PX _;
};
// File Y. cpp
# Include "x. H"
Y: Y (): Px _ (new x ){}
Y ::~ Y ()
{
Delete PX _;
Px _ = 0;
}
Void Y: Fun ()
{
Return PX _-> fun ();
}
// File main. cpp
# Include "Y. h"
Int main (void)
{
Y;
Y. Fun ();
}

That is, the internal Member of Y is x * pointer. In a 32-bit system, the pointer size is fixed to 4 bytes. Even if the size of X is changed, Y is not affected. If X has a subclass, it uses the base class pointer PX _

It also supports virtual function polymorphism.

Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications


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.