Introduction to C ++, Const constants, human-computer interaction, quote, overloaded functions and function with default values, dynamic application and reclaim memory, a comprehensive summary of knowledge

Holiday suspension is not closed, we work together to learn C ++
object-oriented programming language:
blogger asked:

  1. Process-oriented and object-oriented surface difference?
  2. Definition programming paradigm?
  3. Differences and relations between C and C ++?

1.1C ++ Introduction:

C ++ is a statically typed, compiled style, universal, case-sensitive, irregular programming language that supports procedural programming, object-oriented and generic programming.
C ++ is considered a mid-level language, which combines the features of high-level languages and low-level languages.
C ++ to further expand and improve the C language, an object-oriented programming language. C ++ can run on multiple platforms, such as various versions of Windows, MAC OS and UNIX are.
C ++ is a superset of C, in fact, any valid C program is legal C ++ program.
Note: Use statically typed programming language is to perform type checking at compile time rather than at run-time type checking.

1, to understand the history of the C ++ language

C ++ and colleagues designed and implemented in the 80s of last century by the American Bell Labs Bjarne Stroustrup (Jani Strauss special Loup).
C ++ is a language derived from C evolution, above all, a better C, the introduction of a mechanism like, the original C ++ is called "band class C", 1983 was officially named C ++.
Since 1989, the standardization of C ++ language.
A draft C ++ standard ANSI (American National Standards Institute) in 1994.
In November 1998 by the International Organization for Standardization (ISO) approved as an international standard, becoming the current C ++.
++ is a C ++ language operators, foreign scientists believe that this name is very interesting.

2, understand the relationship between C ++ and C language

C ++ = C + symbol and syntax expansion + object-oriented
C ++ inherited and developed the C, it contains all the C syntax; maintaining the simplicity of C and efficiency; to make up for the shortcomings of C, an increase of object-oriented programming design support.

3, understand the limitations of procedure-oriented and the main advantages of object-oriented programming

Limitations of process-oriented programming:
A) traditional programming productivity software development is low
b) traditional programming increasingly difficult to cope with the huge amount of information and diverse types of information
c) traditional programming is difficult to adapt to the new environment
object-oriented programming the main advantage of this design:
(1) can improve the reusability of the program
(2) can control the complexity of the process
(3) can improve the maintainability of
(4) to better support the design of large programs
(5) enhanced computer range of processing information
(6) well adapted to the new hardware environment

4, understanding basic concepts and features of object-oriented programming languages

Basic concepts:
(a) objects: Objects are instances of the class of
objects are both attributes (status) data, and includes a set of operations applied to the package body attribute data.
Objects have state and behavior. For example: a dog state - color, name, breed, behavior - shaking, call out to eat. Objects are instances of classes.
Properties:
attribute data or status data object attributes commonly called.
Method / Service:
capability (function) of an object, i.e., the operation can be realized by a method called or service.
Basically, a method represents an act. A class can contain multiple methods.
It can be written in the logical process operational data and perform all operations.
NOTE: In the C ++ language, the attribute data member called Service / method is called member function.
(Ii) Class: class is an abstract object
class is a set of abstract objects having the same properties and the same operation.
Class may be defined to describe the behavior of the object / state template / blueprint.
(C) message
message (Message): is information describing events, an object request is sent to another object.
Messaging: object-oriented programming techniques is provided a mechanism for one object to another object allowed to interact.
Requester or sender: object sends a message
recipient or target object: the object receiving the message
basic features: the object-oriented programming four basic features:
abstraction
encapsulation
inheritance
polymorphism
abstraction: the matter which is a common induction, the process of concentration.
Package: refers to the code data and focused on the internal operation to achieve the object, and as the internal details concealed object.
Inheritance: retention characteristics of existing classes and construction process of a new class called inheritance.
Polymorphism: refers to different objects result in different behavior when more receive the same message.

5, understand the widely used C ++ language

1.2const constant

1, to understand and define the difference between const, const grasp of general usage: constants defined using regular array.

The difference between #define macro definitions and constants of const

And different types of security checks

Macro definition is replaced with the character, there is no difference between data types, while this does not replace the type of security checks, and other errors may have marginal effects;
const constant is declared constants have the type of difference, you need to type checking at compile time

The compiler handles the different

Macro definition is a "compile-time" concept, launched in the pre-processing stage, can not define macros for debugging, End of Life and compile time;
const constant is a "run-time" concept, used in the program run, similar to a read-only line data

Different Memories

Is a direct replacement macro definition, the code does not allocate memory segment, storing a program;
const constant need for memory allocation, and the data stored in the program segment

Different domain

F1 void () {
#define N 12 is
const int n-12 is;} void F2 () {
COUT N << << endl; // correct, N has been defined, the definition is not restricted domain
cout << n << endl; // error, n is defined only in the domain of the function f1}

After defining whether to cancel

Before passing through the macros can be #undef macros failure
const constants are defined in the domain of definition after permanent
void F1 () {
#define N 12 is
const int = n-12 is;

After #undef N // cancel the macro definition, even if the function f1, N is also invalid
after #define N 21 //} cancellation may be redefined

Can you do the function parameters

Macro definitions can not passed as an argument to a function
const constants can appear in the function's parameter list

Const using constant type declaration specifies a prefix, as follows:
const type variable = value;

2, the master pointer const used in combination: a constant pointer to point, often pointer often pointer constant. (difficulty)

(1) a constant pointer - the pointer to be constant, the contents not allowed to change, but the pointer itself may become

         const char * name = “liu”;  //定义了一个指针变量name,指向一个字符串常量
                                                       //const是修饰char的
         name[2] = ‘n’;      //非法,name指向的是一个常量,其内容不许变。
         name = "zhang";  //合法,name指向了另一个常量字符数组

(2) constant pointer - the pointer points to the same position, i.e., the pointer itself constant, but variable pointing to the content

         char * const name = “liu”;  //const修饰name,定义了一个不动的指针
         name[2] = ‘n’;   //合法,里面的内容可变
         name = "zhang";   //非法,name不许指向另一个字符数组

(3) directed normally constant pointer - the pointer points to itself and its contents are allowed to change

         const char * const name=“liu”;  //一个const修饰name
                                                              //还有一个const修饰char
         name[2]=‘n’;       //非法,指针指向的内容不许变。
         name="zhang";  //非法,指针也不能指向另一个字符数组

3, master const usage scenarios in function.

1. Overview of the constant const type variable name = initialization expression (const variable = value equivalent type)
2. often defined using a variable type variable const
3. normally defined by reference use const type & variable = initialize (initialization)
4. often variable name Object type name = const initialization expression (initializing expression)

4, understand the often quoted, often the object of general usage. (Ibid)

1.3 Human-Computer Interaction

Included in the common header file for input and output of numerous classes, the following table lists the flow stream commonly used class libraries, and indicates those classes which flows in the header file.
Here Insert Picture Description

1, to understand why C ++ to build their own input output system

1.C language input-output system does not support user-defined class types
2.C language data type checking is not critical

2, understand the format of the input and output which may be controlled, to control how the common format

 cin istream object class for processing input standard input device (keypad);
 COUT ostream class object, processing for outputting the standard output device (screen);
 ostream cerr, object class, for standard processing device outputs an error message (without a buffer, i.e. displayed immediately);
 the clog ostream object class for outputting an error processing information (and buffer) on the standard output device when the output buffer is full.

Input stream

Example I:
Here Insert Picture Description
Example II:
Here Insert Picture Description
Example III:
Here Insert Picture Description

Output stream

Example 1:
Here Insert Picture Description
Example Two:
Here Insert Picture Description

3, master the simple input and output

Here Insert Picture Description

1.4 references

1, reference grasp concepts, definitions and use

References aliases variable, mainly used as arguments and the return type, a reference to direct the operation of the variable operation of the same. A statement quoted in the format:

  类型 &引用名=已定义的变量名或引用;

Its use Description:
1) also defines referenced, it must be initialized, in addition to the function as the outer parameters or return type.

2) reference is different from ordinary variables, declare illegal the following:

	    不能建立引用的数组            int & a[9];
        不能建立指向引用的指针        int & *p;
        不能建立引用的引用            int & &px;
        不能建立void引用             void & a=x;
        
        不能用类型或NULL来初始化     
        int & a=int;   
        int & a =NULL;

3) reference symbol with the address-of operator there is a difference:

type&——类型后的是引用符
	&i——变量前的为取址符

4) address may be assigned to a reference pointer.

2, focus on mastering cited as an argument

Biography pointer calls and call references. When the shape parameters and arguments, to deliver the address of a variable.
Example 1: Pointer

#inlcude<iostream.h>    
void swap ( int *a, int *b )
{
  	int temp; 
	 temp=*a; 
 	*a=*b; 
	 *b=temp;   
}
void main ( )  
{
	int x=10, y=20;
	swap( &x, &y );  
	cout<<“x:<<x<<“y:<<y<<endl;
}

Example Two: References

 #inlcude<iostream.h>
 void swap ( int &a, int &b)
{
	int temp; 
	 temp=a; 
	 a=b; 
	b=temp;   
}
void main ( )  
{
	int x=10, y=20;
	swap( &x, &y );  
	cout<<“x:<<x<<“y:<<y<<endl;
}

3, to understand the similarities and differences between references and pointers: C ++ pointer vs reference

引用很容易与指针混淆,它们之间有三个主要的不同:
不存在空引用。引用必须连接到一块合法的内存。
一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。
引用必须在创建时被初始化。指针可以在任何时间被初始化。

1.5 带缺省值的函数与函数的重载

1、理解C++中函数的一般用法:函数原型声明,内联函数。

在C++中,如果函数调用的位置在函数定义之前,则要求在函数调用之前必须对所调用的函数作函数原型声明,目的是使编译器能更好的检查。
使用说明:
1、函数原型的参数表中可以没有参数的名字,而只有类型,如int Add(int,int)。
2、缺省的函数返回值为int类型。
内联函数也称内置函数,内联函数定义的形式就是在函数的函数类型前加上inline关键字即可,当编译器发现某段代码在调用一个内联函数时,它不是去调用该函数,而是将该函数的代码,整段插入到当前位置,同时将实参代替形参。这样做的好处是省去了调用的过程,加快程序运行速度。

2、掌握带缺省值的函数定义与调用方法:含一个缺省值的函数,含多个缺省值的函数。(难点)

C++函数声明原型时(若没有原型声明,则在函数定义时),可为若干参数指定默认值,以后调用如果省略一个或多个实参,C++自动将默认值作为其实参。
使用说明:
1、函数的形参与实参从左到右一一结合,实参 不足用默认参数补齐。
2、函数原型中带默认值的参数都必须出现在不指定默认值参数的右边。
3、调用时某一参数省略,其后参数均省略。

3、掌握函数重载的用法。(重点)

把同一作用域内名字相同,但参数不同(个数不同、类型不同、顺序不同)的函数称为重载函数。
(1)参数个数
(2)参数类型
(3)参数顺序
这三个方面上,至少有一个不同之处。
注意:参数相同只函数返回值类型不同,不算重载!

1.6 内存的动态申请与回收

1、理解C++中全局变量、局部变量的作用范围,掌握作用域运算符 :: 的用法。

C++程序中,有两个同名变量:一个全局的,另一个是局部,那么局部变量在其作用域内有较高优先权;如果要在这里使用全局变量,可以使用作用域运算符“::”

2、掌握C++中强制类型转换,union联合类型的用法。

C中把一个int数转换为float数:
int i=10;
float x=(float)i;
C++中不仅支持这样的格式,还提供了一种新的强制类型转换方法:
int i=10;
float x=float(i);
后者相当于函数调用,是C++推荐使用的方式。

3、理解C++中内存动态申请与回收机制,掌握new和delete的用法。(重点)

memory management is new and delete operators
C using malloc () and free () functions in the dynamic memory operation; C ++ using new and delete operators better, easier and releasing allocated memory.
new use forms: a pointer variable = new type;
wherein, type is a data type name, new assignment sizeof (type) bytes of memory, the first address to the pointer variable;
Delete use forms: delete pointer variable;
Instructions:
. 1) new and delete functions similar to malloc and free.
2) new can automatically calculate the size of the type, and more secure.
3) new automatically return the correct pointer type, not cast.
4) using the new space is dynamically allocated arrays, such as
int * PI = new int [10];
. 5) can be initialized at the same time new allocation space.
Such as: new new int = P int * (99);
. 6) to release the dynamic array using the following form:
Delete [] P;
return NULL pointer when 7) new allocated memory is not satisfied.

Finishing is not easy, please indicate the source!
More, the future will continue to add!
thank

Published 52 original articles · won praise 10 · views 3734

Guess you like

Origin blog.csdn.net/weixin_46047285/article/details/104408482