30
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++

Learning the C++ language 3. The Nuts and Bolts of C++

  • Upload
    hewitt

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Learning the C++ language 3. The Nuts and Bolts of C++. What you have learned last week?. Built (compiling and linking) a simple C++ program under the Visual Studio environment. Source code, object code, and executable code Encountered syntax errors. Compile-time errors are useful. - PowerPoint PPT Presentation

Citation preview

Page 1: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

1

Learning the C++ language 3. The Nuts and Bolts of C++

Page 2: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

2

What you have learned last week?

Built (compiling and linking) a simple C++ program under the Visual Studio environment.– Source code, object code, and executable code

Encountered syntax errors.– Compile-time errors are useful. – Run-time errors

Debugging process– Debugging all kinds of errors

Page 3: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

3

What you have learned last week?

Executed the program in the “console mode.”– Many command-line applications are known as

console applications.

Installed Visual Studio at your own PC. Reading

Page 4: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

4

3.1 Revisit HelloWorld

Page 5: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

5

A simple program

#include <iostream>

int main(){

std::cout << "Hello World!" << std::endl;

return 0;}

Page 6: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

6

Elements of a C++ program

A main() function– A program consists of many functions.– It is expected to return an integer value.

Inputs and outputs– Standard output stream (std::cout)– Standard input stream (std::cin)– Insert a new-line character (std::endl)

Standard library– It is a collection of classes and functions, such as iostream.

Data type: character strings and the new-line character Operators: << is an insertion operator. Statements

Page 7: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

7

#include <iostream>

int main(){

std::cout << "Hello World!" << std::endl;

return 0;}

preprocessorpreprocessor

The actual program

The actual program

• Preprocessor: • Instruct the compiler on how to

compile the program

• Will not generate machine codes

• Start with a pound (#) symbol

• Actual program: • Every C++ program must have

the main() function

• It is the beginning point of every C++ program

Preprocessor and Program Codes

Run before compiling

Page 8: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

8

Preprocessor#include <iostream>

• When compiling a file, we need to obtain the definitions of some terms in the program codes.

• These definitions are recorded in some header files.

• These files are shipped with the compiler or other resources.

• #include tells the compiler where to find the header files and insert this file to that location of the program

e.g. #include <iostream> tells the compiler it should get the file iostream through the default path.

e.g. #include "myfile" tells the compiler it should get the file myfile in the current folder.

Page 9: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

9

Program Codes

int main(){ std::cout << "Hello World!"

<< std::endl; return 0;}

• The basic element of a program is function.

• A function is composed of:

1. Return Type1. Return Type 2. Function name2. Function name

3. Input parameters3. Input parameters4. Program codes

enclosed by the opening and closing braces

4. Program codes enclosed by the opening and closing braces

Note: The meaning of std::cout is checked in iostream

Think from the point of view of the compiler.

Page 10: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

10

main()

Call main()

Return 0

Means everything fine on executing main()as it is the last statement.

• The main() function is the beginning point of a program.

• When executing a program, the operating system will first call the main() function of this program.

• If the above main() function executes successfully, it should return an integer 0 to the operating system.

Page 11: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

11

int main(){

std::cout << "Hello World!"<< std::endl;

return 0;}

Send the string Hello World! to std::cout – the standard output, defined in iostream

Send the string Hello World! to std::cout – the standard output, defined in iostream

Return an integer 0 to the operating system

Return an integer 0 to the operating system

• In console mode, the standard output is just the console, or the Command prompt window.

• In C++, character string is represented by a sequence of characters enclosed by “ ”.

• std::endl means newline (or Enter), also defined in iostream.

Program Codes

Page 12: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

12

• std::cout and std::endl means that we are referring to the cout and endl of the std namespace

• The std namespace is defined in iostream.

• Namespace – A new feature of C++

• Designed to help programmers develop new software components without generating naming conflicts.

• Naming conflict – A name in a program that may be used for different purposes by different people.

•cout and endl are not a part of C++, people can use these two names for any purpose; not necessarily referring to the standard output and newline.

Namespaces

Folders and files concept in XP

Page 13: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

13

#include <iostream>

namespace myns {int cout=0; //Integer variable

} //No semi-colon

int main(){

std::cout << myns::cout << std::endl;return 0;

}

We can have our own cout by putting it in a namespace defined by ourselves

We can have our own cout by putting it in a namespace defined by ourselves

• In fact, another definition of cout can be found in iostream.

• The result of this program is a number 0 shown on the standard output.

This cout refers to the standard output

This cout refers to the standard output

This cout refers to the number 0

This cout refers to the number 0

Page 14: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

14

#include <iostream>

namespace myns {int cout=0;

}

int main(){

std::cout << cout << std::endl;return 0;

}

That’s why using cout without the namespace is an error, because the system does not know which cout you are referring to.

That’s why using cout without the namespace is an error, because the system does not know which cout you are referring to.

Page 15: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

15

• It may be a bit cumbersome to write the namespace every time.

• A short form is to use the using statement.

#include <iostream>

using namespace std;

int main(){

cout << "Hello World!" << endl;return 0;

}

All names that are not a part of C++ will belong to the namespace std, unless otherwise stated.

All names that are not a part of C++ will belong to the namespace std, unless otherwise stated.

No need to put std in front of cout and endlNo need to put std in front of cout and endl

Page 16: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

16

#include <iostream>using namespace std;int main(){

cout << "Hello there.\n";cout << "Here is 5: "<< 5 << "\n";cout << "endl writes a new line to the screen.";cout <<

endl;cout << "Here is a very big number:\t" << 70000 <<

endl;cout << "Here is the sum of 8 and 5:\t" << 8+5 << endl;cout << "Here's a fraction:\t\t" << (float) 5/8 <<

endl;cout << "And a very very big number:\t";cout << (double) 7000*7000 <<

endl;cout << "Replace Frank with your name...\n";cout << “Frank is a C++ programmer!\n";return 0;

}

We can also print integers, floating- point numbers or even combination of string and integers in standard output

We can also print integers, floating- point numbers or even combination of string and integers in standard output

\n - Another way to show newline\n - Another way to show newline

\t – Add a tab character\t – Add a tab characterAnother lineAnother line

escape sequence

Ex. 3.1a

Page 17: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

17

Result

Page 18: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

18

Comments• A program needs to be well commented to explain the

important points of the program.

• Adding comments in the program will not affect the program execution but only improve readability.

• Comments can be added in two ways:

#include <iostream>using namespace std;int main(){/* Text between these two marks are comments.*/

cout << "Hello World!\n"; return 0; // Text after that are also comments}

Page 19: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

19

Exercise 3.1aa. Build the program in p. 16. Note the output on the console.

b. Add one statement to the program which will show your name and height (in cm) in a single sentence. The name should be shown as a character string. The height should be shown as an integer.

c. Use the comment symbols /* and */ to comment the statements from line 4 to line 8 (inclusive). Is there any change to the results output?

Page 20: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

20

• Although a single main() function is enough for any C++ program, it’s bad to do everything by a single function.

• C++ allows nesting of functions to facilitate "divide and conquer" of jobs.

• The main() can call other functions to help it complete a task.

• When a function is called, the program branches off from the normal program flow.

• When the function returns, the program goes back to where it left before.

More on Functions

Page 21: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

21

Mr. A wants to decorate his house

Call his friend B to help mowing

Return him the mowed lawn

Call his friend C to help painting

Return him the painted house Call a function is just

similar to asking somebody to help!

Call a function is just similar to asking somebody to help!

1

2

3

Page 22: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

22

#include <iostream>using namespace std;//function DemonstrationFunction()//show a useful messagevoid DemonstrationFunction(){

cout << "In Demonstration Function\n";cout << "Print one more line\n";

}

//function main - prints out a message, then//calls DeomonstrationFunction, then shows//the second message.int main(){

cout << "In main\n";DemonstrationFunction();cout << "Back in main\n";return 0;

}

Return nothingReturn nothing

A function is calledA function is called

They must be the same

They must be the same

A function is definedA function is defined

Page 23: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

23

#include <iostream>using namespace std;//function DemonstrationFunction()// show a useful messagevoid DemonstrationFunction(){

cout << "In Demonstration Function\n";cout << "Print one more line\n";

}

//function main - prints out a message, then//calls DeomonstrationFunction, then shows//the second message.int main(){

cout << "In main\n";DemonstrationFunction();cout << "Back in main\n";return 0;

}

The execution sequence is like that

Page 24: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

24

• To let the called function really help the main(), sometimes parameters are passed from main() to the called function.

• After finishing the computation, the function should pass back the results to main().

• It can be achieved by the return statement.

main()Branch to

function(a,b)

function(a,b)

return c

Passing Parameters to Function

Page 25: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

25

Mr. A wants to decorate his house

Call his friend B to help mowingand give him a mowing machine

Return him the mowed lawn

Call his friend C to help painting and give him the paint brush Return him

the painted house If you want your friend

to help, you'd better give him the tool!

If you want your friend to help, you'd better give him the tool!

1

2

3

Page 26: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

26

#include <iostream>using namespace std;int Add (int x, int y){ cout << "In Add(),received "<<x<<" and "<<y<<"\n";

return(x+y);}int main(){

cout << "I'm in main()!\n";int a,b,c;cout << "Enter two numbers: ";cin >> a;cin >> b;cout << "\nCalling Add()\n";c = Add(a,b);cout << "\nBack in main().\n";cout << "c was set to " << c;cout << "\nExiting...\n\n";return 0;

}

Add() is called with two parameters

Add() is called with two parameters

Add() will return an integer x+y back to main()

Add() will return an integer x+y back to main()

c holds the return value of Add()

c holds the return value of Add()

Input parameters need to declare type - the same as those in the calling function

Input parameters need to declare type - the same as those in the calling function

Page 27: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

27

Exercise 3.1ba. Build the program in the last slide. Note the output on the

console.

b. Modify main() to calculate the square of c. Add one more function called Square() to achieve this. The Square() function will take the square of the parameter that is passed to it. It will return the result in the form of an integer back to the calling function.

Page 28: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

28

Reading

Hours 2-3 in 24 hours

Page 29: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

29

Acknowledgement

The slides are based on the set developed by Dr. Frank Leung (EIE).

Page 30: Learning the C++ language  3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++

Computer Programming

3. The Nuts and Bolts of C++

30

Some references

http://www.cplusplus.com/– http://www.cplusplus.com/doc/tutorial/– http://www.cplusplus.com/reference/