Revisit Hello World

advertisement
1
Computer Programming
3. The Nuts and Bolts of C++
Learning the C++ language
3. The Nuts and Bolts of C++
2
Computer Programming
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.
–

Encountered syntax errors.
–
–

Source code, object code, and executable code
Compile-time errors are useful.
Run-time errors
Debugging process
–
Debugging all kinds of errors
3
Computer Programming
3. The Nuts and Bolts of C++
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
Computer Programming
3. The Nuts and Bolts of C++
3.1 Revisit HelloWorld
5
Computer Programming
3. The Nuts and Bolts of C++
A simple program
#include <iostream>
int main()
{
std::cout << "Hello World!"
<< std::endl;
return 0;
}
6
Computer Programming
3. The Nuts and Bolts of C++
Elements of a C++ program

A main() function
–
–

Inputs and outputs
–
–
–



Standard output stream (std::cout)
Standard input stream (std::cin)
Insert a new-line character (std::endl)
Standard library
–

A program consists of many functions.
It is expected to return an integer value.
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
Computer Programming
3. The Nuts and Bolts of C++
Preprocessor and Program Codes
#include <iostream>
preprocessor
int main()
{
std::cout << "Hello World!"
<< std::endl;
return 0;
}
• Preprocessor: Run before compiling
• Instruct the compiler on how to
compile the program
• Will not generate machine codes
• Start with a pound (#) symbol
The actual
program
• Actual program:
• Every C++ program must have
the main() function
• It is the beginning point of
every C++ program
8
Computer Programming
3. The Nuts and Bolts of C++
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
Computer Programming
3. The Nuts and Bolts of C++
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()
3. Input parameters
{
std::cout << "Hello World!"
<< std::endl;
return 0;
}
4. Program codes
enclosed by the
opening and closing
braces
Note: The meaning of std::cout is checked in iostream
10
Computer Programming
3. The Nuts and Bolts of C++
• 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.
Call main()
main()
Return 0
Means everything fine on executing main()as it is the last statement.
11
Computer Programming
3. The Nuts and Bolts of C++
Program Codes
Send the string Hello
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
Computer Programming
3. The Nuts and Bolts of C++
Namespaces
• 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. Folders and files
concept in XP
• 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.
13
Computer Programming
3. The Nuts and Bolts of C++
#include <iostream>
We can have our own cout
by putting it in a namespace
defined by ourselves
namespace myns {
int cout=0; //Integer variable
} //No semi-colon
This cout refers to
int main()
the standard output
{
This cout
refers to the
number 0
std::cout << myns::cout << std::endl;
return 0;
}
• 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
Computer Programming
3. The Nuts and Bolts of C++
#include <iostream>
namespace myns {
int cout=0;
}
That’s why using cout
without the namespace is
an error, because the
system does not know
which cout you are
referring to.
int main()
{
std::cout << cout << std::endl;
return 0;
}

15
Computer Programming
3. The Nuts and Bolts of C++
• 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;
All names that are not a part of
C++ will belong to the
namespace std, unless
otherwise stated.
int main()
{
cout << "Hello World!" << endl;
return 0;
}
No need to put std in front of cout and endl
16
Computer Programming
3. The Nuts and Bolts of We
C++
can also print integers, floatingpoint numbers or even combination of
string and integers in standard output
#include <iostream>
using namespace std;
int main()
\n - Another way to show newline
{
escape sequence
cout << "Hello there.\n";
cout << "Here is 5: "<< 5 << "\n";
cout << "endl writes a new line to the screen.";
cout <<
\t – Add a tab character
Another line
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;
}
Ex. 3.1a
17
Computer Programming
3. The Nuts and Bolts of C++
Result
18
Computer Programming
3. The Nuts and Bolts of C++
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
Computer Programming
3. The Nuts and Bolts of C++
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
Computer Programming
3. The Nuts and Bolts of C++
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
Computer Programming
3. The Nuts and Bolts of C++
Mr. A wants to
decorate his house
Call his
friend B to
help mowing
1
Call his
friend C to
help painting
3
2
Return him the
mowed lawn
Return him
the painted
house
Call a function is just
similar to asking
somebody to help!
22
Computer Programming
3. The Nuts and Bolts of C++
#include <iostream>
using namespace std;
They must
be the
same
//function DemonstrationFunction()
//show a useful message
Return nothing
void DemonstrationFunction()
{
cout << "In Demonstration Function\n";
cout << "Print one more line\n";
}
A function is defined
//function main - prints out a message, then
//calls DeomonstrationFunction, then shows
//the second message.
A function is called
int main()
{
cout << "In main\n";
DemonstrationFunction();
cout << "Back in main\n";
return 0;
}
23
Computer Programming
3. The Nuts and Bolts of C++
#include <iostream>
using namespace std;
The
execution
sequence
is like that
//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;
}
24
Computer Programming
3. The Nuts and Bolts of C++
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)
main()
return c
Branch to
function(a,b)
25
Computer Programming
3. The Nuts and Bolts of C++
Mr. A wants to
decorate his house
1
Call his friend C
to help painting
and give him the
paint brush
3
Call his friend B
to help mowing
and give him a
mowing machine
2
Return him the
mowed lawn
Return him
the painted
house
If you want your friend
to help, you'd better give
him the tool!
26
Computer Programming
3. The Nuts and Bolts of C++
#include <iostream>
Input parameters need to declare type - the
using namespace std;
same as those in the calling function
int Add (int x, int y)
{
cout << "In Add(),received "<<x<<" and "<<y<<"\n";
return(x+y);
}
Add() will return an integer
int main()
x+y back to main()
{
cout << "I'm in main()!\n";
int a,b,c;
cout << "Enter two numbers: ";
cin >> a;
Add() is called with two
cin >> b;
parameters
cout << "\nCalling Add()\n";
c = Add(a,b);
cout << "\nBack in main().\n";
cout << "c was set to " << c;
cout << "\nExiting...\n\n";
c holds the return value of
return 0;
Add()
}
27
Computer Programming
3. The Nuts and Bolts of C++
Exercise 3.1b
a. 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.
28
Computer Programming
3. The Nuts and Bolts of C++
Reading

Hours 2-3 in 24 hours
29
Computer Programming
3. The Nuts and Bolts of C++
Acknowledgement

The slides are based on the set developed by
Dr. Frank Leung (EIE).
30
Computer Programming
3. The Nuts and Bolts of C++
Some references

http://www.cplusplus.com/
–
–
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/reference/
Download