C + + Prerequisite fundamentals and programming specifications

Source: Internet
Author: User
Tags function definition shallow copy

C + + Prerequisite fundamentals and programming specifications


C language is process-oriented programming, emphasizing the execution order of programs, top-down, and C + + is Object-oriented programming, the program as a number of objects, each object has its own attributes (variables) and behavior (function).

2. Attributes are data that describe the characteristics of an object, and the behavior is what the object can do, such as every hero in the league has its own attributes (health, mana, defense, attack) and behavior (common attack, cast skill qwer).


C + + Programming Specification:

Class name first word uppercase, data member and variable lowercase;

member function The first letter of the second word is capitalized;

member functions are defined outside the class and declared within the class;

The parameters of set and constructor have the same name as the data, and this is used to access;


First, what are classes and objects


1. Classes (entities with commonalities) are in fact an upgraded version of the C language structure, which can be understood in the program as variables that the compiler allocates storage space for.

If a struct with a function is defined, then the struct is changed to class

Class Stu

{

int id;

Char *name;

void study ()

{

cout<<name<< "Learning C + +" <<endl;

}

};

Here class Stu are classes that define a variable Stu Czy with this class; Czy is the object.

The object can be manipulated such as Czy.id =1;czy.study ();

In C + +, the suffix is *.cpp, compiler g++, contains header file #include<iostream>

Cin,cout for input and output, use of namespace using namespace Std is required;


2. Namespaces are used to resolve naming duplication problems, and custom scopes encapsulate multiple variables and functions, such as:

Namespace MyName

{

int num = 10;

void Fun ()

{

std::cout<< "in MyNamespace fun\n";

}

}

Then, in the main function, reference, Myname::fun ();


3.I/O Stream cin>> and cout<<, which is the C language of printf () and scanf () but do not specify the format, CIN input data type mismatch when not read, multiple inputs are separated by a space, when a space is encountered, the end, do not check the length of the data, There is a danger of buffer overflow. So there is cin.getline (str, sizeof (STR)), similar to the C language of Fgets ();


The struct names in 4.c++ can be directly used as type names (automatic typedef), unlike C, where const-decorated values in C + + are constants and literal constant areas exist.


5. Default function

Functions with default parameters, such as void fun (int num = 0);


6. Overloaded functions

A function with the same name is allowed to appear, but with different parameters, the return value cannot be a distinguishing flag and cannot have the same name as the default function.


7. Type Conversion

char a = char (num);

char *p = char * (num);


8.new/delete operator

Remember malloc and free in C, where the pointer and space are still in C, but the pointer is no longer working on the space (as if it could be read), so it should point to null

New and delete are not functions are operators

int num = 10;//stack area define a variable

such as int *p = new int (100);//Open a 4-byte space in the heap, with a number of int 100

Char *str = new char[100];//opens up a 100-byte space in the heap area, with the first address assigned to STR

Note the difference between the new int (100) and the new char[100] (PIT).

New application space is automatically assigned a value of 0

Delete a single variable space delete num; Remove the array space delete []str;


9. References

The operator &, instead of the pointer, takes an individual name to the variable and references the space

int num = 10;

int &a = num;//defines the alias of a num

The reference must be assigned an initial value and cannot be modified after definition.

It can be used as parameter and return value to improve the execution efficiency of the program.


10.c++ with encapsulation, increased access to members

Private can only be accessed in the class and is accessible outside of the class in the public class, and the members of class classes default to Private

Or the class?

Class Stu

{

private://all members after the start of this line are private members

int id;

Char *name;

public://all members after the start of this line are public members

void study ()

{

cout<<name<< "Learning C + +" <<endl;

}

};

In the canonical 1:c++, the property is generally set to private, the operation is set to public, and then each property has the appropriate action, such as SetID (), GetId (), SetName (), GetName () as if object-oriented programming is this routine.

Spec 2: Member functions are generally defined outside the class, declared within the class, and defined with the class name:: Specify

such as void stu::setid (int num)


11. Constructors

A special member function that is used to initialize an object when it is defined

The constructor name is the same as the class, can have a parameter, can have no parameters, has no return value (including void), and is called automatically by the compiler.

If there is no custom constructor, the default constructor (empty function) is used, and the constructor can be overloaded and default.

The constructor is also called when using new to define an object Stu *s2 = new Stu ();


12. Destructors

The inverse of the constructor that completes the anti-initialization operation when the object or object exits the life cycle, such as cleaning up memory

Add the ~ sign before the constructor, in general the destructor is executed by the compiler, and the call is triggered when the delete is used

In general, you do not need to define destructors (there is no heap memory for members in the class)

As in the constructor, there are

Stu ()

{

name = new CHAR[100];

}

You must use the destructor

~stu ()

{

if (NULL! = name)

{

delete []name;

name = NULL;

}

}

Each class has only one destructor, cannot be overloaded, and cannot have parameters


13. Copy Constructors

A special constructor that defines an object when it is initialized with an existing object

The name is the same as the class, no return value, and the argument must be a reference to the object of the current class


Conditions of Use: Write Stu s2 = s1, and Stu S3 (S1), when the copy constructor is called and write Stu S2; s2 = S1; Copy constructors are not called.

In general, custom copy constructors are not required (like destructors do not have heap memory for members of the class), because if you do not define copy function The compiler will only perform shallow copy operations (copy values, do not copy memory space), there will be a segment error when releasing

So when a pointer is in a class and space is allocated for the pointer, a deep copy of the copy constructor is to be customized.

Stu (const STU &S1)

{

id = s1.id;

name = new Char[strlen (s1.name) +1];

strcpy (name, s1.name);

}

A custom copy constructor is required when there is a dynamic memory allocation in the class, or a segment error is caused by releasing the same memory multiple times (double free or corruption)


14.const Decorated member functions

void setId (int num) const

{

}

Called a read-only member function that can read only member values and cannot be modified (except for mutable int IDs)


15. Array of objects: A collection of finite objects of the same type

Stu Array[3]={stu (1), Stu (2)};


16.this pointer

A hidden pointer variable, which is the first parameter of the member function, the type of the object

If the member function parameters and properties of the object are the same, this can be used to specify

For specification:

void setId (int id)

{

This->id = ID;

}

If you want to repeat a call to a member such as: S1.setid. GetId (); enables SetId () to return a reference to an object (return *this)


17. Enumeration (often used with switch)

List of constructor functions

Stu (): ID (Ten), Score (99.5)

{


}

Use the same method as the constructor, but you can complete the initialization of special members such as const-decorated members


18.const Decorated members

Can only be initialized with a list of constructors, const-decorated members may have different values in different objects


19 static member static int num; static int getnum ();

Static data members and static member functions, initially 0

Static data members are defined outside the class, declared within the class, and other members of the class are not in a piece of memory, not occupying the object's space, and sizeof () does not calculate.

belongs to the entire class, not to an object, through the class name:: Object Access Stu::num

One of the functions: replace global variables

Static member functions cannot use non-static data members, only static data members and global variables, and cannot use this

To better manipulate static member functions that use static data members, you can manipulate static data members before you define an object.


20. Object Members

That is, the nesting of classes, similar to struct nesting, the object of one class as a member of another class

Assigning a value to a data member in an object member to use a list of constructors

Class A

{

Public

int anum;

A () {

Anum = 1;

cout<< "in a constructor" <<endl;

}

A (int num) {

Anum = num;

}

};

Class B

{

Public

int bnum;

A;

B ()

{

Bnum = 2;

cout<< "in B-constructors" <<endl;

}

B (int num): A (10)

{

Bnum = num;

}

};


21. Friends

Accessing private members of a class outside of the class

Friend function, friend member, friend class

Keyword friend

Friend function: Declare an out-of-class function as friend, you can access the private members of the class, the friend function definition has the order request

Friend member: A member function of a class is declared as friend, such as friend Void Tch::getnum (), and the member function of another class can access that class member.

Friend class: Friend class Tch;

Friends are generally not used because they undermine the encapsulation of functions


22. Operator overloading

Also called operator overloading functions, to accommodate object manipulation

int operator + (a A1, a A2)

{

return a1.num+a2.num;

}

Operator overloading is essentially a function overload and must follow its principle

Except for the member operator ".", "sizeof", the conditional operator "?:", the scope operator "::" can be overloaded, after overloading the precedence and binding unchanged

General overloads are friend functions or member functions

Monocular operator overloading is a member function, binocular operator overloading is a friend function

"=", "()", "[]", "-" cannot be overloaded as friend





C + + Prerequisite fundamentals and programming specifications

Related Article

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.