C + + basic syntax

Source: Internet
Author: User

Document: Http://www.cprogramming.com/tutorial/lesson6.html

1. header file # include <string.h> is C language, and C + + is used with # include <string>

Otherwise, the following program cannot output a string:

int main () {

String a = "333ddd";

cout << a << Endl;

Cin.get ();

}

2.switch Case Statement

Note: The case cannot be a string and can only be of type int

int main ()
{
int a = 20;
Switch (a)
{
Case (10):
cout << "im ten" << endl;
Case (20):
cout << "im" << endl;
Default
cout << "im default" << Endl;
Break
}
Cin.get ();
return 0;
}

3. Hands, pointer

I can't read it. Document: http://www.cprogramming.com/tutorial/lesson6.html

Concept:

Pointer, also a variable. Its value holds the memory address of the other variable.

Eg: there are two ways to access variable A, one is to access cout << a directly,

The other is indirect access (that is, access with the address of a),

int A; Define int type variable A

int *p; Defines a pointer to the int type (int denotes the base type, * indicates that the variable p is a pointer type), and why the base type is defined, as described below.

p = &A; Pass the address of A to P (that is, the value of the variable p holds the address of a)

cout << p; Address of output A

cout << *p; Output variable a!!!!

Defined:

One pointer, one regular intint *pointer1, nonpointer1;//-Pointersint *pointer1, *pointer2;

Use:

#include <iostream>using namespace Std;int main () {   int x;            A Normal integer  int *p;           A pointer to an integer  p = &x;           Read it, "Assign the address of X to P"  cin>> x;          Put a value in X, we could also use *p here  cin.ignore ();  cout<< *p << "\ n"; Note the use of the * to get the value  cin.get ();}

Base type:

eg

int *p;

float *p;

String *p;

where int float string is the base type, since there is * to indicate that p is a pointer variable, why base type?

The reason for this is that there is p+1, which means that the value of P is +1, which is address +1, and that different int float string 1 is different, such as int is 2 bytes, string 4 bytes

4. Structural body

Concept:

A bit like a class, when used, you also have to instantiate an object.

Defined:

struct aaa{

int a = 10; The struct can only be initialized for shaping

String B;

}

Use:

AAA obj;

OBJ.B = "Im a string from struct";
cout << obj.a << obj.b <<endl;

Use pointers in structs:

struct aaa{

int a = 10;

}

AAA obj;

AAA *pointer;

pointer = &obj;

cout << pointer->a << Endl; Use, just like the object of a class invokes a method

5. Arrays

Defined:

String box[10]; Type and length must be specified

Use: (slightly)

Use pointers in arrays:

String box[10];

String *pointer;

pointer = &box;

6. Methods

Defined:

C + + basic syntax

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.