C ++ BASICS (study notes)

Source: Internet
Author: User

1. Standard input/output stream of C ++

C ++ provides a set of input and output stream objects, which are Cin, cout, and cerr. They correspond to three file pointers in C language, stdin, stdout, and stderr, respectively pointing to terminal input, terminal output and standard error output (also output from the terminal). Cin and> complete the input operation, cout, cerr, and <together to complete the output and standard error output. Using CIN and cout is much more convenient than using sanf and printf in C language. Cin and cout can automatically identify the input and output data types and adjust the input and output formats, unlike sanf and printf, you do not need to specify them one by one. Using Cin, cout is not only convenient, but also reduces the possibility of errors.

For the output, we can use int I = 3; cout <x <Endl;

For input, we can use Int J; CIN> I;

Note the direction of the arrow. In the output, we also use Endl (end of line) to indicate line breaks. The last character is the character "L", not the number "1". Endl is equivalent to '\ n' of C language, indicating that a line break is output.

2. Constructor

The most important role of constructors is to create objects in this province.

C ++ stipulates that each class must have a constructor. Without constructor, no object can be created.

C ++ also stipulates that if a class does not provide any constructor, the C ++ compiler provides a default constructor without any parameters, it is only responsible for creating objects without any initialization.

Only one class defines a constructor. no matter whether the constructor is a constructor with parameters, C ++ no longer provides default constructor. If you need a constructor without parameters, you must define it yourself.

3. destructor

When an object's lifecycle ends, the occupied memory space will be reclaimed, and this work will be done by the destructor.

A destructor is a reverse constructor. A destructor does not allow return values. More importantly, it does not allow parameters, in addition, a class can only have one destructor.

4. Function Overloading

Analyze the following situation: whether to constitute a function overload.

(1) void output (); int output (); --- no

(2) void output (int A, int B = 5); void output (int A); --- no

Condition: the method name is the same, the parameter type of the function, and the number of parameters is different, can constitute a function overload.

5. This pointer

This pointer is an implicit pointer that points to the object address of this province.

The member functions called by an object of a class are all in the same code segment. How does a member function identify data members of the same object? Originally, PT. output (), in addition to accepting two real parameters, the member function also receives the address of object s, which is obtained by an implicit this pointer, it is equivalent to executing this = & PT. all accesses to data members are implicitly prefixed

This->. For example, x = 0; equivalent to: This-> x = 0.

6. inherited access features of Classes

Access features of the base class inherit the access features of the feature subclass

Public

Protected public protected

Private no access

 

Public protected

Protected

Private no access

 

Public Private

Protected private

Private no access

7. Polymorphism

When the C ++ compiler is compiling, it finds that if the method of the parent class is a virtual function, C ++ will adopt the late binding technology at this time, during the runtime, the system determines which function to call based on the object type. This function is made of C ++ polymorphism.

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.