C + + Learning notes------from a small program.

Source: Internet
Author: User

From a small program.

The main purpose of this talk is to help you to establish a connection with C + + in the background knowledge of the language.

Problem exploration

Problem: Summing an integer array.

Requirements: Define an array of n elements that require the C language to accomplish this task.

Hurry up: Let's do it together!

Reference:

#include <stdio.h>intAddArray (intArray[],intn);intMain () {intData[] = {0,1,2,3,4,5,6,7,8,9}; intSize =sizeof(data)/sizeof(data[0]); printf ("The result is:%d\n", AddArray (data, size)); return 0;}intAddArray (intArray[],intN) {      intsum =0; inti;  for(i=0; I < n; i++) {sum+=Array[i]; }      returnsum;}
A little deeper thinking.

We're talking about arrays and pointers. Their relationship is complex.

In just the example, we add "material" to try ~

Then we can draw a second version of the program:

1#include <stdio.h>2 3 intAddArray (int*array,intn);4 5 intMain ()6 {7       intData[] = {0,1,2,3,4,5,6,7,8,9};8       intSize =sizeof(data)/sizeof(data[0]);9 Tenprintf"The result is:%d\n", AddArray (data, size)); One  A       return 0; - } -  the intAddArray (int*array,intN) - { -       intsum =0; -       inti; +  -        for(i=0; I < n; i++ ) +       { ASum + = *array++; at       } -  -       returnsum; -}
It's the C + +.

The above two programs are essentially the same, and we conclude that declaring the parameters of a function as an array and declaring it as a pointer is the same.

Now let's consider a new problem: use C + + for this program to try.

1#include <iostream>//ostream C92 C992 3 //using namespace std;4 5 intAddArray (int*array,intn);6 7 intMain ()8 {9       intData[] = {0,1,2,3,4,5,6,7,8,9};Ten       intSize =sizeof(data)/sizeof(data[0]); One  AStd::cout <<"? á1?ê?:"<< AddArray (data, size) <<Std::endl; -  -       return 0; the } -  - intAddArray (int*array,intN) - { +       intsum =0; -       inti; +  A        for(i=0; I < n; i++ ) at       { -Sum + = *array++; -       } -  -       returnsum; -}
Program Analysis

This applet shows the first use of C + + objects. Where are the objects? This is the "cout".

cout is an output stream object, which is the abbreviation for "Console out" (console output). is an object belonging to the Basic_ostream class. The Ostream class is defined in the iostream header file.

What is an output stream?

In fact, a concept that introduces a lot of similar concepts in C + +: Data can flow from the keyboard to the program, and from the program to the screen, printer, etc. ~

Using NAMESAPCE std;

This designation brings us to a whole new concept: namespaces.

All identifiers used by the C + + standard library (that is, the names of classes, functions, objects, etc.) are defined in the same special namespace (STD).

If we do not use this instruction, we will need to use syntax such as std::cout to invoke the output stream object.

I wonder if you have noticed that the symbol "<<" looks familiar?

Is this operator not the left-shift operator of the C-language bitwise operation? Did you change the rules in C + +?

In fact, this is not the case, it simply embodies another feature of C + +: support for overloading. Overloading is actually allowing us to use the same operator in different ways. We'll explain it in detail behind us.

C + + Learning notes------from a small program.

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.