Features of C and C + +

Source: Internet
Author: User
Tags shallow copy

C language Evolution to C + + process, is a camouflage of the development process.
1, BOOL type variable: 1 byte variable (same size as char), default assignment is True (1), False (0)
2, reference variable: The new application of the variable to hang on the original memory address of the same type. The advantage of quoting is that writing is more fluid and the logic of the code is clearer.
3. Namespaces:
A) When the project code is large, the code written by a large number of programmers has a duplicate function, the name of the global variable
b) Use of the using namespace STD, where statements are called within each space function without adding domain name control
c) The definition field (::) will be used later in the class member function,
4. C + + input and output
Recommended use of #include <iostream> uising namespace std;
Not recommended for use of # include <iostream.h>
cout and CIN are more powerful than printf and scanf, and automatically recognize types.
%d and%s are not required, and so on.
5. Application Heap Space:
New and delete replace malloc and free, making writing smoother, especially if you are requesting an array in the heap to be more intuitive.
For example://stest *p= (Stest *) malloc (sizeof (stest));
Stest *p=new stest;
Free (p)
Delete p;

int *p1= (int *) malloc (sizeof (int.));
int *p1=new int [5];
Delete []P1 does not recommend the use of delete P1
Note: new error notation: int p1=new int (5)
6. Default parameters for functions
When a function is called, a default value can be supplied without specifying a parameter. The default value can be given in the prototype or function definition, but it is not recommended to specify a default value in the prototype declaration in two locations.
int add (int data1, int data2,int data3=9)
{
Return data1 + data2 + data3;
}

int main ()
{
int sum = Add (n);
}
7. Inline function:
Instead: the C language macro definition function, is actually one of C + + 's head. The compilation is still handled as a macro-defined function.
8. C + + is upgraded from struct struct to class
Classes not only support variables, but also support member functions, is a more serious head-over, any one of the member functions of a class is hidden inside the formal parameter of a variable called the This pointer.
9. The difference between struct and class
The only difference between struct and class is that the default permissions are different, public.protected private
1) Private: The member variables and member functions of the privilege are restricted to the member function calls within this class;
2) Public: member variables and member functions of the shared permissions, which can be called within the class and outside the class;
3) If all members within class are public, they are equal to the struct.
4) when developed in the company, structs are often used for the structure of pure data, and the class as the development of multi-functional classes.
10, C + + support overloaded functions, C language can not have the same name function,
Overloaded functions: Function names are the same, but parameter lists are different (types and numbers are not exactly the same)
Recursive functions: C language and C + + support
11. Constructor:
One:
1) A constructor is a special member function of a class, and in general, constructors are specifically used to initialize member variables, so do not perform operations that are not related to the initialization of the object in the constructor.
2) The function name of the constructor must be exactly the same as the class name, and there is no return value (not that the return value is void or int, but that the return value is not allowed).
3) The return value of the constructor is incorrect if any type is established, and the function of the constructor is to replace the C language with parentheses that initialize the struct object.
4) The C language is initialized to struct members using curly braces, if the class (struct) has constructors that prohibit the use of curly braces to initialize member data.
Two:
Invocation of the constructor function
1) is the constructor is called automatically when defining an object to specify parameters or not specifying parameters, and it is an error to call the constructor manually using the object.
2) constructors are divided into non-parametric, parametric, and copy constructs that define the form of a constructor, assuming that only that object is defined in such a way.
3) Call the constructor with "=", call the constructor of the single argument and the same type as the one on the right of the "=".
4) In general, the "=" right of the type does not fit the single-argument constructor cannot use the "=" construct.
5) There is only one type appropriate "=" right-hand constructor, there is a default copy constructor inside the system, the internal implementation is to the body assignment value.
12. Destructor:
1) destructors can be called manually, but it is not recommended to call manually, as long as the object's life cycle ends, the destructor is called automatically,
2) The destructor name is added in front of the class name, and the destructor cannot return any values, it must have no function arguments, and cannot be overloaded.
3) So a class can have multiple constructors, but only one destructor,
4) in general, the constructor of an object is called whenever an object is created, and the destructor of the object is called whenever an object is undone.
13. Summary of constructors and destructors:
1) Whether it is a constructor or a destructor, we should treat them as a callback function.
2) constructors and destructors do not themselves perform any initialization or cleanup of heap space on member data of the class.
3) The destructor itself does nothing but the point at which the object's space will be destroyed, prompting the programmer to write some code to clean up the heap space that has been applied.
4) in a destructor, the programmer only needs to consider releasing the heap space that is requested, and the object itself is automatically cleaned up either by the stack space or the global space system.
14. Life cycle of class member variables:
1) class member functions (after object construction) can be called by each member function, which looks like the C language calls to global variables.
2) is the same as the life cycle of the object, as long as the object is not effective, the member variable within the object has been valid.
3) The Life start time of a member variable within a class object is: When the object is defined, that is, when the constructor executes.
4) The life end time of a member variable within a class object is: When the destructor executes.
15. Copy Construction:
1. Form: Cstu (const cstu&)
The essence is the constructor, and the parameter is a constant reference to this class.
2. When to invoke:
(1) Create a new object and initialize it to a homogeneous existing object
Cstu A;
1) cstu A1 (a);
2) Cstu a2=a;
3) Cstu A3=cstu (a); Assigning a value to an object with a temporary object
4) Cstu a4=new Cstu (A1);
But first define two objects, and the assignment between them does not call the copy construct,
For example: Cstu A1;
Cstu A2;
A2=A1;
2) When the program generates a copy
1, the function parameter passes the value of the object
2, function returns object
3. What are the functions:
The default copy constructor, copying the value of a non-static member (copy of a member into a shallow copy), copying the value of a member
16, friend function
1. Advantages and disadvantages of using friend function
Advantages: Can improve efficiency, express simple, clear.
Disadvantage: The friend function breaks the encapsulation mechanism, tries not to use the member function, unless the unavoidable situation makes the UF meta-function.
2. Use of friend functions
2.1 Arguments to the friend function:
Because the friend function does not have this pointer, there are three conditions for the parameter:
2.1.1 To access a non-static member, the object is required to do the argument;
2.1.2 to access a static member or global variable, the object is not required to do the argument;
2.1.3 If the object that makes the parameter is the global object, then the object does not need to do the parameter;
2.2 The location of the friend function
Because a friend function is a function outside the class, its declaration can be placed in the private or public segment of the class without distinction.

Class INTEGER
{
friend void Print (const integer& obj);//Statement friend function
};
void Print (const integer& obj)
{
function body
}
void Main ()
{
INTEGER obj;
Print (obj);//Direct call
}
3. The difference between a friend function and a member function of a class
The 3.1 member function has this pointer, and the friend function does not have the this pointer.
3.2 friend function is not inherited, just like the father's friends may not be the son's friends.
17, Friend class
#include <iostream>
using namespace Std;

Class FriendDemo1
{
Public
FriendDemo1 ()
{
m_pri=10;
}
Friend class FriendDemo2;
friend void Visitmpri (FriendDemo1 &s);
Private
int m_pri;
};

Class FriendDemo2
{
Public
void VisitDemo1 (FriendDemo1 &s);
};
The Member method of the friend class can access the private member variables of friends directly
void Frienddemo2::visitdemo1 (FriendDemo1 &s)
{
cout << s.m_pri << Endl;
}
Friend functions also have direct access to the private member variables of friends
void Visitmpri (FriendDemo1 &s)
{
cout << S.m_pri <<endl;
}

int main ()
{
FriendDemo1 fd1;
FriendDemo2 fd2;

Fd2.visitdemo1 (FD1);
Visitmpri (FD1);
return 0;
}

C language Evolution to C + + process, is a camouflage of the development process.
1, BOOL type variable: 1 byte variable (same size as char), default assignment is True (1), False (0)
2, reference variable: The new application of the variable to hang on the original memory address of the same type. The advantage of quoting is that writing is more fluid and the logic of the code is clearer.
3. Namespaces:
A) When the project code is large, the code written by a large number of programmers has a duplicate function, the name of the global variable
b) Use of the using namespace STD, where statements are called within each space function without adding domain name control
c) The definition field (::) will be used later in the class member function,
4. C + + input and output
Recommended use of #include <iostream> uising namespace std;
Not recommended for use of # include <iostream.h>
cout and CIN are more powerful than printf and scanf, and automatically recognize types.
%d and%s are not required, and so on.
5. Application Heap Space:
New and delete replace malloc and free, making writing smoother, especially if you are requesting an array in the heap to be more intuitive.
For example://stest *p= (Stest *) malloc (sizeof (stest));
Stest *p=new stest;
Free (p)
Delete p;

int *p1= (int *) malloc (sizeof (int.));
int *p1=new int [5];
Delete []P1 does not recommend the use of delete P1
Note: new error notation: int p1=new int (5)
6. Default parameters for functions
When a function is called, a default value can be supplied without specifying a parameter. The default value can be given in the prototype or function definition, but it is not recommended to specify a default value in the prototype declaration in two locations.
int add (int data1, int data2,int data3=9)
{
Return data1 + data2 + data3;
}

int main ()
{
int sum = Add (n);
}
7. Inline function:
Instead: the C language macro definition function, is actually one of C + + 's head. The compilation is still handled as a macro-defined function.
8. C + + is upgraded from struct struct to class
Classes not only support variables, but also support member functions, is a more serious head-over, any one of the member functions of a class is hidden inside the formal parameter of a variable called the This pointer.
9. The difference between struct and class
The only difference between struct and class is that the default permissions are different, public.protected private
1) Private: The member variables and member functions of the privilege are restricted to the member function calls within this class;
2) Public: member variables and member functions of the shared permissions, which can be called within the class and outside the class;
3) If all members within class are public, they are equal to the struct.
4) when developed in the company, structs are often used for the structure of pure data, and the class as the development of multi-functional classes.
10, C + + support overloaded functions, C language can not have the same name function,
Overloaded functions: Function names are the same, but parameter lists are different (types and numbers are not exactly the same)
Recursive functions: C language and C + + support
11. Constructor:
One:
1) A constructor is a special member function of a class, and in general, constructors are specifically used to initialize member variables, so do not perform operations that are not related to the initialization of the object in the constructor.
2) The function name of the constructor must be exactly the same as the class name, and there is no return value (not that the return value is void or int, but that the return value is not allowed).
3) The return value of the constructor is incorrect if any type is established, and the function of the constructor is to replace the C language with parentheses that initialize the struct object.
4) The C language is initialized to struct members using curly braces, if the class (struct) has constructors that prohibit the use of curly braces to initialize member data.
Two:
Invocation of the constructor function
1) is the constructor is called automatically when defining an object to specify parameters or not specifying parameters, and it is an error to call the constructor manually using the object.
2) constructors are divided into non-parametric, parametric, and copy constructs that define the form of a constructor, assuming that only that object is defined in such a way.
3) Call the constructor with "=", call the constructor of the single argument and the same type as the one on the right of the "=".
4) In general, the "=" right of the type does not fit the single-argument constructor cannot use the "=" construct.
5) There is only one type appropriate "=" right-hand constructor, there is a default copy constructor inside the system, the internal implementation is to the body assignment value.
12. Destructor:
1) destructors can be called manually, but it is not recommended to call manually, as long as the object's life cycle ends, the destructor is called automatically,
2) The destructor name is added in front of the class name, and the destructor cannot return any values, it must have no function arguments, and cannot be overloaded.
3) So a class can have multiple constructors, but only one destructor,
4) in general, the constructor of an object is called whenever an object is created, and the destructor of the object is called whenever an object is undone.
13. Summary of constructors and destructors:
1) Whether it is a constructor or a destructor, we should treat them as a callback function.
2) constructors and destructors do not themselves perform any initialization or cleanup of heap space on member data of the class.
3) The destructor itself does nothing but the point at which the object's space will be destroyed, prompting the programmer to write some code to clean up the heap space that has been applied.
4) in a destructor, the programmer only needs to consider releasing the heap space that is requested, and the object itself is automatically cleaned up either by the stack space or the global space system.
14. Life cycle of class member variables:
1) class member functions (after object construction) can be called by each member function, which looks like the C language calls to global variables.
2) is the same as the life cycle of the object, as long as the object is not effective, the member variable within the object has been valid.
3) The Life start time of a member variable within a class object is: When the object is defined, that is, when the constructor executes.
4) The life end time of a member variable within a class object is: When the destructor executes.
15. Copy Construction:
1. Form: Cstu (const cstu&)
The essence is the constructor, and the parameter is a constant reference to this class.
2. When to invoke:
(1) Create a new object and initialize it to a homogeneous existing object
Cstu A;
1) cstu A1 (a);
2) Cstu a2=a;
3) Cstu A3=cstu (a); Assigning a value to an object with a temporary object
4) Cstu a4=new Cstu (A1);
But first define two objects, and the assignment between them does not call the copy construct,
For example: Cstu A1;
Cstu A2;
A2=A1;
2) When the program generates a copy
1, the function parameter passes the value of the object
2, function returns object
3. What are the functions:
The default copy constructor, copying the value of a non-static member (copy of a member into a shallow copy), copying the value of a member
16, friend function
1. Advantages and disadvantages of using friend function
Advantages: Can improve efficiency, express simple, clear.
Disadvantage: The friend function breaks the encapsulation mechanism, tries not to use the member function, unless the unavoidable situation makes the UF meta-function.
2. Use of friend functions
2.1 Arguments to the friend function:
Because the friend function does not have this pointer, there are three conditions for the parameter:
2.1.1 To access a non-static member, the object is required to do the argument;
2.1.2 to access a static member or global variable, the object is not required to do the argument;
2.1.3 If the object that makes the parameter is the global object, then the object does not need to do the parameter;
2.2 The location of the friend function
Because a friend function is a function outside the class, its declaration can be placed in the private or public segment of the class without distinction.

Class INTEGER
{
friend void Print (const integer& obj);//Statement friend function
};
void Print (const integer& obj)
{
function body
}
void Main ()
{
INTEGER obj;
Print (obj);//Direct call
}
3. The difference between a friend function and a member function of a class
The 3.1 member function has this pointer, and the friend function does not have the this pointer.
3.2 friend function is not inherited, just like the father's friends may not be the son's friends.
17, Friend class
#include <iostream>
using namespace Std;

Class FriendDemo1
{
Public
FriendDemo1 ()
{
m_pri=10;
}
Friend class FriendDemo2;
friend void Visitmpri (FriendDemo1 &s);
Private
int m_pri;
};

Class FriendDemo2
{
Public
void VisitDemo1 (FriendDemo1 &s);
};
The Member method of the friend class can access the private member variables of friends directly
void Frienddemo2::visitdemo1 (FriendDemo1 &s)
{
cout << s.m_pri << Endl;
}
Friend functions also have direct access to the private member variables of friends
void Visitmpri (FriendDemo1 &s)
{
cout << S.m_pri <<endl;
}

int main ()
{
FriendDemo1 fd1;
FriendDemo2 fd2;

Fd2.visitdemo1 (FD1);
Visitmpri (FD1);
return 0;
}

Features of C and C + +

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.