C + + Learning Note 1

Source: Internet
Author: User

I was a freshman in C, now the sophomore opened C + +. So we started to start with C + +. So in the beginning there is no indifferent basic summary. The direct transition from C to C + +.

First, the preliminary understanding of C + +

1. In C, expressions do not support lvalue, that is, in C a=b=100 can, but (C=d) =200 not, because (C=d) is an expression. In C + +, an expression can be assigned a value.

a=b=100; Operation procedure: Assign 100 to B before assigning B to a, and this code is also available in C.

(C=d) =200; Operation Process: First assign D to D, and then 200 to C. Equivalent to c=d;c=200, this code can only be passed in C + +.

2. A preliminary understanding of constants and pointer constants

const int a=100;
int b=a;
/*const int * Const PA;//ERROR
The first const defines the variable that the PA points to as a constant, whose value cannot be changed, so it should be initialized at the time of definition, otherwise it cannot be initialized later, but it can be compiled in the past.
The second const defines that the pointer pa is a constant and its value cannot be changed, so it will error.
correct notation: const int * Const pa=&a;
    */
Const int *pa;//This line simply defines the variable that the PA points to as a constant, so the pointer can be assigned a value.
pa=&a;
//*pa=50;//error The variable A that the PA points to is a constant, and its value cannot be modified.
//int *pb=pa;//error Pointer pa points to a variable that is constant, and PB points to a variable whose type is different and cannot be assigned the address of two different type variables.
The const int *pb=pa;//Defines the variable that the PB points at this time as a constant, yes.
cout<<*pa<<endl<<*pb<<endl;

3.c++ Input and output

CIN, cout as Class objects, scanf, printf as functions
For a real type, cout outputs six bits of valid data by default.
SETW () Specifies the domain width, and the header file is Iomanip.
The Setiosflags (ios::left) is used for left-aligned output.
Setprecision (n) <<setiosflags (ios::fixed), you can set the number of digits to the right of the decimal point. n represents the number of decimal digits.
The Setfill ("character") is used to specify a character fill, which is global.

Note: The default cout output is decimal,
However, when the format output is changed, the default is no longer valid, and the output format becomes the final format.
The decimal output is still the default when the program finishes closing and opens again.

int A;
cout<< "Enter a number:";
cin>>a;
cout
<< "Default output:"
<<a<<endl;
cout
<< "Decimal Output:"
<<dec<<a<<endl;
cout
<< "Octal output:"
<<oct<<a<<endl;
cout
<< "Hexadecimal output:"
<cout
<< "When you use the default output at this time, its default output is specified as the last format output" <<endl
<< "Default output:"
<<a<<endl;
cout
<< "Specify the field wide output:" <<endl
&LT;&LT;SETW (8) <<dec<<a<<endl;
cout
<< "left-aligned output:" <<endl
&LT;&LT;SETW (8) <<setiosflags (ios::left) <<a<<endl;

4. Function overloading

function Overloading principle: function name is the same, function parameter table is different (parameter type, parameter number, parameter order)
Unlike function return types, they cannot form overloads.
Matching overloaded function principle:
A strict match is called when it is found.
A match is sought through an implicit conversion, and a call is found.
Attention:
int to long and double,double to int and float implicit type conversions
In both cases, the ambiguity causes the error to occur.
The principle of overloading: Using the technique of strife, the function name is strife.
The strife takes place in two stages: the. CPP compilation phase and the declaration phase of. H,
There are only two stages in which the call can be matched.
Ways to avoid strife: add extern "C" before function

int print (int a)
{
cout<< "parameter is number" <<endl;
return 0;
}


int print (char b)
{
cout<< "parameter is character" <<endl;
return 0;
}

int print (char b,int a)
{
cout<< "parameter is character, number" <<endl;
return 0;
}

int print (int a,char b)
{
cout<< "parameter is number, character" <<endl;
return 0;
}

5. Operator overloading

Operator overloading: Its essence is to define a function, defined with the operator keyword.

struct COMP
{
    float real;
    float image;
};

Comp operator+ (Comp One,comp another)
{
    one.image+=another.image;
    one.real+=another.real;
    return One;
}

int Chongzai ()
{    
    comp num1={1,2};
    comp num2={3,5};
    comp sum=num1+num2;
    cout<< ("<<sum.real<<", "<<sum.image<<") "<<endl;
    return 0;
}

6. References
1. A reference is not defined and is a relational declaration.
Declares a relationship between it and the original variable (entity). therefore type
is consistent with the original type and does not allocate memory. Has the same address as the referenced variable.
2. Declaration must be initialized, once declared, cannot be changed.
3. Reference can be referred to again. The result of multiple references is that a variable has multiple aliases.
4. References cannot be established, but references can be referenced again. "Int &rb=ra"
You can take the address "int *p=&ra" to the reference

Frequently cited:
Const cannot be changed, not by pointers or references.
The reference to a const object must be const, and it is not legal to bind a normal reference to a const object.
A const reference can be initialized with objects of a related type (constants, variables of a non-identical type, or expressions).

Reasons for Using const:
1, use const to avoid programming errors that unintentionally modify data.
2, const and non-const arguments can be handled using Const. Otherwise, only non-const data will be accepted.
3, using a const reference allows the function to correctly generate and use temporary variables such as the fruit participating in the reference parameter
If the number does not match, a temporary variable will be generated)

int a=4;

const int b=5;
NT &ra=a;
const int *p=a;//error Const object reference must be const
const INT &rb=b;
cout<< "a=" <<a<<endl<< "ra=" <<ra<<endl;
cout<< "b=" <<b<<endl<< "rb=" <<rb<<endl;

C + + Learning Note 1

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.