Go-c++ Quick Start

Source: Internet
Author: User
Tags shallow copy types of functions

turn fromCSDN Blog, u010425776,http://m.blog.csdn.net/u010425776/article/details/45134577Reference variable

Referencing a variable means defining a variable that uses the same value as the original variable. The reference variable changes the value, and the value of the variable changes.
It is consistent with the nature of the transmitted address.

/***定义一个引用类型的变量b,引用了变量a*/int a;int &b = a;
    • 1
    • 2
    • 3
    • 4
    • 5
Formal parameter Passing reference
/***声明函数xxx*/void xxx(int &);/***实现函数xxx*/void xxx(int &x){ x++;}/***执行函数xxx*/int main(){ int a = 20; xxx(a);//int &x = a;传递引用 cout<<a;//结果为21}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
Formal parameter address
/***声明函数xxx*/void xxx(int *);/***实现函数xxx*/void xxx(int *x){ *x++;}/***执行函数xxx*/int main(){ int a = 20; xxx(&a);//传递地址 cout<<a;//结果为21}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
Formal parameter Passing value

Passing a value to a function is equivalent to copying a value to the function, and the value of the function is not related to the value of the original, so the original value does not change.

/***声明函数xxx*/void xxx(int );/***实现函数xxx*/void xxx(int x){ x++;}/***执行函数xxx*/int main(){ int a = 20; xxx(a);//传值 cout<<a;//结果为20}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
Parameter passing of the default value
/***函数的声明*/int xxx(int a,double b=0,int c=10);
    • 1
    • 2
    • 3
    • 4

The default value is set at the time of Declaration;
Default values can have no, but parameters with default values must appear on the right.

Different types of functions

Functions can be divided into three types, depending on the type of return value.

    • Returns the base data type
    • Returns the address of a static variable

      int *fun(){//要将函数名前加上*    static int n;    n++;    return &n;//返回一个地址}
      • 1
      • 2
      • 3
      • 4
      • 5
    • Returns the alias of a static variable
int &fun(){//要将函数名前加上&    static int n;    n++;    return n;//返回一个引用}
    • 1
    • 2
    • 3
    • 4
    • 5
inline functions

The function body of the called function is embedded directly in the use of the function called the inline function.
Adding inline to the front of the function is the inline function.
The function body of the inner function cannot have cyclic, switch, complex if-else.

Stencil functions
/***只要在函数定义的时候进行以下操作,函数调用的时候照常使用即可*/template <typename T>T add(T x,T y){    return x+y;}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
Pointers to functions

Functions also have addresses, because the code of the function also needs to be placed in a contiguous storage space. The address of the function is the function name.

Declaration of a function pointer:

//已有函数声明double fun(int,int,double);//声明函数指针p,并将p指向fun的代码区域double (*p)(int,int,double)=fun;
    • 1
    • 2
    • 3
    • 4

At this point, use p and the fun effect is exactly the same.

Include directives
    • #include < file name >
      Contains C + + library function header files
    • #include "file name"
      Include files you have written
/#define指令

The Define command is also called the Macro directive, and the macro name is replaced with the macro content. You can include punctuation in the macro content.
When a macro name appears in a string, it is not replaced.
The "UNDEF macro name" means that the macro substitution terminates here.

Conditional compilation
/***如果定义了xx,就编译……,否则就编译……*/#ifdef/#ifndef xx……else……#endif
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

Conditional compilation prevents the header file from nesting, thus avoiding duplicate compilation.

#ifndef HD    #define HD……#endif
    • 1
    • 2
    • 3
    • 4
Definition of a function

member functions can write only function prototypes within a class, while definitions are written outside of the class. When the definition of a function is written outside the class, precede the function name with the class name: void Class Name:: function name (parameter) {
function implementation

Multi-file organization of object-oriented programming

A source program is divided into three files, an. h file, a declaration of a class, a. cpp file, a. cpp file that stores the use of the class.

Use of objects
//与java中不同,c++中只要这么写,对象就已经创建好了。Student stu1,stu2;//没有使用new创建的对象之间可以整体赋值stu1 = stu2;//这样就是把stu2各个成员变量的值一一复制给了stu1
    • 1
    • 2
    • 3
    • 4
    • 5

Two ways that an object invokes a member

//对象调用成员,通过.来调用stu1.getName();stu1.name//对象的地址通过->来调用&stu1->getName();&stu1.name;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

This pointer
The this pointer representing this class of object is hidden in the row parameter table of each member function

void Student::setName(string name,Student *this){    this->name = name;}
    • 1
    • 2
    • 3

Create an object using new

new Student();//使用new创建的是对象的指针,必须在对象前加上*//使用new会调用相应的构造函数,也可以没有括号表示调用空参构造器。
    • 1
    • 2
    • 3

Destructors
Destructors are used to complete an object that frees up memory space before the end of the life cycle. is automatically called by the system before the end of the object's life cycle.
Features of the destructor:

    • is a public function
    • The function name is the same as the class name
    • No parameters
    • No return value
    • Start with ~
    • Destructors that do not define a destructor system automatically generate an empty parameter, without any action.
    • When you create an object using new, you must define a destructor that frees the memory space with delete inside the destructor
#include <iostream>UsingNamespaceStdClass student{PrivateChar *name;Public:student (Char *name); ~student ();void Show () {cout<<"Name:" <<name<<endl;} Student::student (Char *name) {if (name) {name = new char[strlen (name) +1]; //creates an array of type char, length is the length of name +1 this->name = name;} this->show ();} ~student::student () {if (this->name) delete []name; //free space for data name} int Main () {Student stu1 ( This is the abbreviated form of Student STU1 = new Student ("Chai") Student stu2 ( "Zhang"); return 0;} //when executed here, the system automatically calls two destructors, releasing STU1, STU2 space.  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
Copy constructor
    • Shallow copy
      The default shallow copy constructor is provided automatically if the copy constructor is not defined manually.
Student s1;//已经创建了对象s1Student s2(s1);//将s1浅拷贝给s2Student s2 = s1;//与上面等价
    • 1
    • 2
    • 3

A shallow copy is a s2 pointer to the area where the S1 resides, thereby s1 sharing an area with the S2.
Shallow copy There is a problem, because the copy before and after the two objects simultaneously point to the same piece of memory space, so when the destruction of the two objects, the same space will be destroyed two times, if the first time the storage space is released, before the second destruction, the space has already deposited other data, at this time the second time to analyze the space , so the space that did not need to be deconstructed was released, and therefore the error was generated.

    • Deep copy
      Must be manually defined by the programmer, in their own definition of the copy constructor, to request a new storage space for the object's member variables, so that the new and old objects, although the values of the member variables are the same, but all point to a different memory space, which is a deep copy. As objects are divided into separate destruction, two memory space does not affect each other.
//Custom copy constructor student::  Student (Student &stu) {if (stu.name) {(this->) name = new char[strlen (name) +1]; strcpy (name,stu.name); //equivalent to This->name = name? }}int Main () {Student stu1; Stu1.setname ( "Chai"); //call copy constructor Student STU2 (STU1); return 0;}         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
Static members
    • Static member variables
//静态成员变量在类内的引用性声明static 类型名 成员变量;
    • 1
    • 2
//静态成员变量在类的定义之外,在文件作用域的某处作正式声明,即进行初始化类型 类名::变量名 //默认值为0类型 类名::变量名=xxx;//给静态变量指定初始化值
    • 1
    • 2
    • 3
//静态成员变量使用的两种方法(和java一样)//假设a是一个静态成员变量Student stu;stu.a;//第一种使用方法,用对象调用;Student::a;//第二种使用方法,用类调用;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
      • Static member functions
        Before declaring a class with static, as defined outside the class and normal member function definitions, there is no need for static adornments, and there are two ways to use outside the class, either through an object invocation or through a class invocation.

Go-c++ Quick Start

Related Article

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.