Getting Started with C + + Basics: Classes and references

Source: Internet
Author: User

I. Classes (6 default functions of a Class), objects, object-oriented concepts

1.cpp data type (except for C)

Basic data type: BOOL type logic non-basic type: Class

Non-type void type

"1" namespace (in order to resolve the problem of global variable naming conflicts)

Example: #include <iostream.h>

using namespace Std; Standard input-output stream, namespace Std

int main ()

{

cout<< ' Hello World ' <<Endl; Cout and Endl are objects, from namespaces, written as:std::cout<< "Hello World" if the namespace is not previously declared <<std::endl;

System ("pause");

return 0;

}

You can add definitions to the namespace at any time, but all are defined in the same namespace

Example: #inlcude <iostream.h>

Namespace Bit1//define a namespace

{

int a=0;

}

namespace Bit2//defining another namespace

{

int a=20;

}

int main ()

{

bit1::a=30; :: For Scope resolver

Bit2::a=40

}

2. Input/output stream

cout→ standard output Stream object,<< output operator;

cin→ standard input Stream object,>> input operator;

endl→ standard line break operator;

3. Function overloading

Example: #include <iostream.h>

using namespace Std;

int add (int num1,int num2)

{

return num1+num2;

}

Float (float f1,float F2)

{

return F1+F2;

}

int main ()

{

int num1=0;

int num2=0;

float f1=0.0;

float f2=0.0;

cin>>num1>>num2;

int Ret=add (NUM1,NUM2);

cout<< "ret=" <<ret<<endl;

cin>>f1>>f2;

Float F3=add (F1,F2);

}

Note: "1" is within the same scope, a set of function names is the same, the parameter list < number/type is different > different, the return value type can be different.

When the "2" determines whether a function is overloaded, it is only necessary to determine whether its functions and parameter lists are the same.

"3" in C and CPP, the rules of function overloading are different, so use the C function in CPP with extern "C".

4. Default function

"1" Full default function

Example: int add1 (int a,int b)

{

return a+b;

}

int main ()

{

int ret=add1 (); There is no need to write a parameter here, if you change it to ADD1 (3), the result is 3, because the parameter is filled from left to right

return 0;

}

"2" semi-default function

Example: int add2 (int a,int b=0)

Two. Pointers and references

Reference: "1" does not define a new variable, but instead assigns a new name to a variable that is already defined, and the variable can be referenced multiple times, that is, a variable can have multiple aliases;

"2" reference to be initialized, to identify who is whose alias;

"3" references can be passed, such as &q=m;&r=q;

"4" an alias can reference only one variable

1.const references

Example: #include <iostream.h>

using namespace Std;

int main ()

{

int *q=null;

const int *P=Q;

int m=10;

const INT &n=m; Often referenced, at this time n as a constant treatment, can not directly modify N, only by modifying m to modify

return 0;

}

Note: "1" Cannot assign a const-modified variable to a variable that is not const-modified

"2" cannot refer to a constant directly unless you add a const, such as a const int &r=5

Example: Double b=3.14;

const INT &k=b;

"3" in the execution of the code, intermediate variables < temporary > is a constant attribute, you have to use const to decorate.

2. References are used in function parameters

Example: #include <iostream.h>

using namespace Std;

int swap (int *a,int *b)//pass pointer to exchange values of two variables

{

int tmp=*a;

int *a=*b;

*b=tmp;

}

For the swap function, there is another way to use the reference

Example: void swap (int &a,int &b)

{

int tmp=a;

A=b;

b=tmp;

}

int main ()

{

int a=10;

int b=20;

Swap (A, b);

return 0;

}

3. Refer to the arguments frequently (when you do not want the value of the parameter x to be changed within the function)

Example: void Readbigdata (const bigdata &x)

{

int a=x.arr[1000];

}

Note: When you return a value as a reference, you cannot reference a temporary variable

Example: int fun ()

{

int ret=10;

return ret; This returns a temporary variable, preferably not written like this.

}

4. Value return and reference disassembly

"1" Value returns: Place the value in the RET in the EAX register

Assembly language: Mov Eax,dword Ptr[ret];

"2" Reference disassembly: Place the address of the RET in the eax, like a pointer

Assembly language: Lea Eax,[ret];

Example: #include <iostream.h>

using namespace Std;

①int Add (int a,int b)

{

int ret=a+b;

return ret;

}

② int *add ()

{

int ret=a+b;

Return &ret;

}

③int &add (int a,int b)

{

int ret=a+b;

return ret;

}

The functions of the above three add functions are the same.

References and pointers differ and relate to:

1. The reference can only start when initialized once, can not point to other variables;

2. The reference must point to a valid variable, and the pointer can be empty;

The 3.sizeof reference refers to the size of the reference object, and the sizeof pointer refers to the size of the object address;

4. The meaning of the self-increment or decrement of the pointer and the reference is not the same;

5. Reference is more secure than a pointer;


This article from "Fu da Xin" blog, reproduced please contact the author!

Getting Started with C + + Basics: Classes and references

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.