Geekband C + + Object-oriented advanced programming (I.)

Source: Internet
Author: User
Tags class definition

Objective: To develop normal atmospheric programming habits.


Key points of the week: defensive declarations, initial columns, pass by value vs. pass by reference, const, operator overloading


I. Introduction to C + + programming


1, Class Classic classification: with pointer and without pointer. The two classifications are very different in thinking.


2.
2.1, for the design of a single class (object based based objects)
2.2, design multiple objects, they are related to each other (object oriented oo)
The relationship between class straight:
--Inheritance (inheritance)
--Composite (composition)
--Commissioned (delegation)


3. C + + Learning content: C + + language and C + + standard library


Ii. declaration of the header file and class
1, and the C language functions and data to do the comparison.
C language has no object concept, the data are global variables;
C + + provides data and functions in a sub-assembly. (What is the difference between class and struct?) )


2. C + + about data and functions
Classes--objects have data and functions, data can have many copies, but only one copy of the function!??


3. C + + programs code basic form
*.h: Header file; *.cpp: Main program; #include < standard library .h>; #include "non-standard library. h";


4. Defensive statements in the header file
#ifndef __complex__
#define __complex__
Prevent duplicates of include
#endif


5. Layout of header Files
-1: Defense-style statement
0: Predecessor statement
1: class declaration
2: Class definition (Implementation)


6. class declaration
Class classname
{
Public
constructor function
Other functions
Private
Data
Friend function (or maybe not)
}


7. Class template (Profile)


Third, the constructor function
1, the function of the class can be defined within the class body, can also be declared within the body, defined outside the body.
2, inline (inline function): (in the body definition or outside the body defined function before the addition of inline) (just a suggested role)
3. Access level: Public and private, protected (Private: Data section)
4, constructor (ctor, constructor): New dynamic way to create an object, get the object pointer.
4.1, the function name is the same as the class name, 4.2, can have parameters, 4.3, can have a default value, 4.4, no return value type, 4.5, (skill) initial column;
4.6, the constructor can be overloading (overloaded).
5, the constructor into the private area, the class is not allowed to be created outside the object.
Singleton (singleton mode): static function, static data.
6. Destructors: Most classes without pointers do not require a destructor.


Iv. parameter passing and return value
1, const member functions (constant member function): Adds a const after the function name.
If a function does not change the object data content, it should be added const!
If you create an object that is marked as const, the compiler will give an error if the object calls a function that is not marked as const at this point. (See Courseware)
Const extends the use of functions and limits the function (permissions) of functions.


2. Parameter pass: pass by value vs. pass by reference (to const)
Return value pass: return by Value vs. return by reference (to const) (as far as return by reference: Return value is not local Var)


3, Friend (Friend): You can access the object private members.
3.1. Each object of the same class is mutually friends (friend)


V. Operator overloading vs. temporary objects
1, the operator is a function. Operator "+" refers to the addition or the plus sign, to be differentiated according to the parameters;
2, operator overloading (operator overloading-1, member function) This (any member function has a this Pointer)
3, return by reference, pass to no need to know the recipient is in the form of reference receive! Return by reference very tough, don't care about sex, men and women all!
3. Temp Object usage: TypeName (); When returning a temporary object, the function return value cannot be reference. It is a local object
4, operator overloading (operator overload-2, non-member function) (no this)
5. Operator << Why can't I write a member function?

Geekband C + + Object-oriented advanced programming (I.)

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.