1 / 30

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

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.

hewitt
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Learning the C++ language 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

  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

  4. 3.1 Revisit HelloWorld

  5. A simple program #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; }

  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

  7. Preprocessor and Program Codes #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; } preprocessor 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 Run before compiling

  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.

  9. Program Codes Think from the point of view of the compiler. • The basic element of a program is function. • A function is composed of: 1. Return Type 2. Function name int main() { std::cout << "Hello World!" << std::endl; return 0; } 3. Input parameters 4. Program codes enclosed by the opening and closing braces Note: The meaning of std::cout is checked in iostream

  10. 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 abovemain() function executes successfully, it should return an integer 0 to the operating system. Call main() main() Return 0 Means everything fine on executing main()as it is the last statement.

  11. Program Codes Send the stringHello World! to std::cout – the standard output, defined in iostream int main() { std::cout << "Hello World!" << std::endl; return 0; } 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.

  12. Namespaces • std::cout and std::endl means that we are referring to the cout and endl of the stdnamespace • 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. Folders and files concept in XP

  13. We can have our own cout by putting it in a namespace defined by ourselves #include <iostream> namespace myns { int cout=0; //Integer variable } //No semi-colon int main() { std::cout << myns::cout << std::endl; return 0; } This cout refers to the number 0 This cout refers to the standard output • 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.

  14. That’s why using cout without the namespace is an error, because the system does not know which cout you are referring to. #include <iostream> namespace myns { int cout=0; } int main() { std::cout << cout << std::endl; return 0; } 

  15. It may be a bit cumbersome to write the namespace every time. • A short form is to use the using statement. All names that are not a part of C++ will belong to the namespace std, unless otherwise stated. #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; } No need to put std in front of cout and endl

  16. We can also print integers, floating- point numbers or even combination of string and integers in standard output #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; } \n - Another way to show newline escape sequence \t – Add a tab character Another line Ex. 3.1a

  17. Result

  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 }

  19. Exercise 3.1a • a. 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?

  20. More on Functions • 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.

  21. Mr. A wants to decorate his house 2 Call his friend B to help mowing 1 Call his friend C to help painting Return him the mowed lawn Return him the painted house 3 Call a function is just similar to asking somebody to help!

  22. Return nothing They must be the same A function is defined A function is called #include <iostream> using namespace std; //function DemonstrationFunction() //show a useful message void 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; }

  23. #include <iostream> using namespace std; //function DemonstrationFunction() // show a useful message void 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

  24. Passing Parameters to Function • 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. function(a,b) Branch to function(a,b) main() return c

  25. Mr. A wants to decorate his house Call his friend B to help mowing and give him a mowing machine 2 1 Call his friend C to help painting and give him the paint brush Return him the mowed lawn Return him the painted house 3 If you want your friend to help, you'd better give him the tool!

  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; } Input parameters need to declare type - the same as those in the calling function Add() will return an integer x+y back to main() Add() is called with two parameters c holds the return value of Add()

  27. Exercise 3.1b • a. Build the program in the last slide. Note the output on the console. • b. Modify main() to calculate the square ofc. 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.

  28. Reading • Hours 2-3 in 24 hours

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

  30. Some references • http://www.cplusplus.com/ • http://www.cplusplus.com/doc/tutorial/ • http://www.cplusplus.com/reference/

More Related