C ++ notes | Lesson 1 | a few basic questions about C ++

C ++ class a few basic questions first notes about C ++

"C language is powerful, we hope to further consolidate the contents of the C language learned, which is the basis for the next step of learning C ++." - Teacher

C ++ history

In 1979, fresh from Cambridge University (CU) 29-year-old Ph.D. Dane Bjarne Stroustrup (Jani Strauss special Loup). Enter the United States, AT & T Bell (New Jersey) laboratory, started to develop C ++ C language on the basis of 1983 developed a C ++ prototype.

1980 was originally called: with class C (C with Class), 1983 was officially named as C ++. Since 1989, the standardization of C ++ in 1994 launched the ANSI C ++ standard

September 2011 approved the latest ISO C ++ standard is: C ++ 11 (ISO / IEC 14882: 2011)

Introduction to C ++

Is called C ++ object-oriented programming language OOPL (Object-Oriented Programming Language)

C is a subset of C ++, C ++ C to keep the original idea of.

C ++ C on the basis of keywords on the increase: catch, class, const, delete, friend, inline, new, operator, private, protected, public, template, this, throw, try, typeid, virtual, volatile keyword, etc. .

The most important mechanism of C ++ increase in C language, there are three:

  • Class (class)
  • Function overloading (function overloading)
  • Overload operator (operator overloading)

Basic data types

New Type: bool (Boolean) only two values: true / false
to assign other bool number, will be converted into a force true (not 0) or false (is 0)
bool type (one byte) equivalent to char

input Output

#include <iostream>
using namespace std;

The original stdio.h C language, string.h, math.h other documents cited still allowing
use, but need to be removed .h file name suffix, preceded c:

#include <cstring>
#include <cmath>

There is no essential difference between the input and output operations for the general wording of the above two, but the file operations will be quite different.

Common I / O stream library manipulator (Function):

  • setw (int) settings are then (a) the content of the output field width, not automatically break
  • setprecision (int) arranged subsequent to the output (a) of the floating-point number of bits (excluding the decimal point) - Note: is the total number of digits
  • All subsequent hex integer output values ​​in hexadecimal
  • All subsequent oct integer octal output
  • All subsequent dec decimal integer value of output
  • endl carriage return
    in order to use setw and setprecision, you must also cited at the beginning of the program iomanip.h add: #include <iomanip.h>or#include <iomanip>
#include <iostream> 
#include <iomanip> 
using namespace std;
void main(void)
{
int i=33, j=35, k=12345; float f=3.14159f;
cout << setw(10) << i << setw(8)<< j <<endl;
cout << setw(3) << k <<endl; // 场宽不足,自动突破
cout << hex << i <<setw(3) << j <<endl; // 用十六进制输出整数 
cout << i <<setw(3) <<j <<endl;
cout << oct << i <<setw(j/8) << j <<endl; // 用八进制输出整数
cout << setw(12) << setprecision(5) << f << endl;
cout << setw(4)<<setprecision(6)<<f<<endl; //场宽不足,自动突破
cout << setprecision(4) << f << endl; // 未定场宽,按照实际宽度输出 
cout << setprecision(1) << f << endl;
cout << setprecision(2) << 314.159 << endl; }
//运行结果
        33      35
12345
21 23
21 23
41  43
      3.1416 //这里总位数5位
3.14159
3.142
3 //保留到一位,相当于取整(只针对0.0~9.9)
3.1e+02
Press any key to continue . . .
char s1[20], s2[20], s3[20];
cin >> s1 >> s2 >> s3;
// 等同于 scanf("%s%s%s", s1, s2, s3);
cout << s1 << s2 << s3 << endl;
// 若从键盘上输入:How are you?
// 输出结果是: Howareyou? 整行输入字符串:
cin.getline(s1, N, 结束符); // '\n' 为默认结束符,这里可以缺省参数
cin.get(s2, N, 结束符); // 一次连续读入多个(包括空格)字符,直到读满N-1个,或遇到 结束符
// getline读取但不存结束符,get既不读取也不存结束符

Inline functions

In order to improve the efficiency, C ++ inline function increases. Definition and similar macro define, position compiler function name will appear replace it with a function body code of the so-called online, so save the parameters of the function call stack, unstack, the time it takes to jump and so on. Inline function convenient than define, function also stronger, not simply the replacement string, take full account of the type matching.

Declared as inline functions: add inline before the function

Writing inline function has some limitations (but no uniform clearly defined, determined by the extent of the various smart compiler):
• inline function can define a variable, you can use the loop, branching statements such as;
• but inline recursive function not directly or indirectly recursive;
• other so that the compiler can not put the case of function calls online.
Those who could not get a compiler function call line of inline function, the compiler will automatically ignore inline, think of it as a general function to call, inline it automatically fails.

Dynamic memory allocation

C ++ with new and delete dynamically allocate and free memory, equivalent to the C functions malloc and free, but the new and delete more powerful

int *p=new int(10); //赋值
delete p;
int *pt=new int[10]; //数组
delete []pt;
int (*ptt)[10]=new int[8][10]; //分配80个int单元// pt是一个二维动态数组,ptt[0][0]~ptt[7][9]
delete []ptt;
int **q=new int *[10];
delete []q;

Quote

C ++ provides a mechanism to pass a variable reference address.

Head to pass in a function parameter name & address plus before, and function calls the same way.
Assignment of this variable to pass variables declared in an address to the called function, the operation is actually a direct call to a function in the corresponding variable.

#include <iostream>
using namespace std;
void swap(int &a, int &b) // 引用声明传递a,b变量的地址 
{ 
    int t=a;
    a=b; 
    b=t; 
}
void main()
{ 
    int i=3, j=5;
    swap(i, j); //交换i, j的值
    cout << i <<'\t'<< j<<endl ;
}
// 输出结果 : 5  3 
// a和b的值已经交换

Reference (pass variable address) in addition to simplify procedures when writing, but also improve the efficiency of the program, because when the transmission is a structure (objects), simply pass its address, so that the function accessed directly, without having to pass in accordance with C language the value of the mode structure (object) to the parameter list functions.

See later: the copy constructor for the class, this reference mechanism way (pass variable address), and is not a luxury but a necessity.

Function return value can return a reference, so that also just pass the amount of return address, so call the function accessed directly, without having to pass values ​​in the manner C language will return after copying transfer amount is returned. But can not return a reference to a local variable.

using namespace std;
const int &max(int &a, const int &b) 
{ 
    return a>b? a:b;// 返回的是a或b也就是i或j的地址 
}
void main()
{ 
    int i=3, j=5, k;
    k=max(i, j); //取i, j的值
    cout << k <<endl ; 
}

Reference (reference) is a special type of variable, it can be considered to a variable aliases.

int i, j;
int &r=i; // 变量引用,r和i是同一个内存单元
// r不能再改变引用,可以认为r是i的别名
i=10;
j=r; // 等价于i=20

Const type qualifier

const is a type modifier appears in front of or behind the variable type defined above, making the amount of non-modifiable, i.e. constant.

For example, int const a=1;or const int a=1;the variable to become a non-modifiable amount equivalent to a constant C, but stronger than the constant const C language function, as is the belt type const.

const Function Parameters often used to prevent the inadvertent destruction of the values of the parameters in the function or content referred to.
For example, strcpy(char *str2, const char *str1);by copying a string str1 to str2. The parameter is defined as str1 const, it is to prevent the damage referred to in the string str1 strcpy function.

In particular, the introduction of mass in C ++ the address referencing mechanism, const value or meaning to ensure content does not modify the parameter variables in the function

Often an object
in addition to the definition of a one-time initial value, can not change its value, so that when the common protective effect.
const STU a={"Zhang San", 'm', 2019000001};

Often data members

struct STU
{
    char name[10];
    char gender;
    static int const sno=1;//静态常数据成员 
    //static与const同时出现
}

Often pointer
such STU *const p=&a, can only be assigned in the initialization

Often the object pointer pointing
often pointers STU *const pand pointers general STU *pcan not normally subject to point

Using const STU *p = &a;the pointer to a. The pointer can be modified
Conclusion: pointing object pointer is not normally constant pointer, may be modified, can also point to an ordinary object.

Often often pointed object pointer
const STU * const p = &a; points to a time of initialization and can not be modified

Scope and Visibility

C ++ introduced scope operator (scope operator) '::', so that we can access the local variable with the same name at the same time (max) and global variables (:: max)

#include <iostream>
using namespace std;
const int max=10000;
void main( ) // 从键盘上读入10个数,求最大值max输出,但最大不超过10000 
{ 
    int max, i,j;
    cin >> max;
    for (j=1; j<10;j++) 
    {
        cin >> i;
        if (i>max) max = i;
        if (max > ::max) max = ::max; 
    }
    cout << max<<endl; 
}
//若max大于外部变量max的值,则取外部常量max的值, 这种做法通常用来防止数组越界或溢出。

The default parameters

You may be omitted from the right one or more parameters of such a function call.
The default value can be specified as one or more parameters in the function declaration, it can be omitted from the right one or more parameters of such a function call.

void delay(int day=1, int month=3, int year=2019)
{............}

Data Abstraction

Separating the data types and operations, referred to as data abstraction

T abs(T i)
{ returni<0?–i:i; }

Objects

A (group) variables, as well as acting on a (group) as a function of these variables a packaging unit, which is called an object.

Objects of the same class is abstract (description of the common features), the object is an instance of the class.

Object has closed, you can control access to the outside world of its own, which strictly control access to the outside world which is encapsulated variable.

Published 10 original articles · won praise 0 · Views 188

Guess you like

Origin blog.csdn.net/qq_45379253/article/details/104868460