An Overview of Computers and C++ Programming Language
An Overview of Computers and C++ Programming Language
Module 1
Objectives
To learn about the architecture of computers !! To learn about machine languages and higher-level programming languages !! To become familiar with your compiler !! To compile and run your first C++ program !! To recognize compile-time and run-time errors !! To describe an algorithm with pseudocode !! To understand the activity of programming
!!
Computer
Computer cont
Computers can carry out a wide range of tasks because they execute different programs, each of which directs the computer to work on a specific task. The computer itself is a machine that stores data (numbers, words, pictures), interacts with devices (the monitor, the sound system, the printer), and executes programs.
Computer cont
A computer program tells a computer, in minute detail, the sequence of steps that are needed to fulfill a task. Hardware The physical computer and peripheral devices are collectively called the hardware. Software The programs the computer executes are called the software.
5
What Is Programming?
Programming
The CPU (central processing unit) !! heart of the computer !! executes one operation at a time !! performs program control and data processing
!!
10
11
What Is This?
a special computer program, that translates the higher-level description (a program) into machine instructions for a particular processor. the compiler-generated machine instructions are different, but the programmer who uses the compiler need not worry about these differences.
!!
13
You will need to know how to start your C++ development environment. An IDE (integrated development environment) is where you will most likely work.
14
You will become a typist because you will use an editor to type your C++ programs into the IDE. Your program is called a source file.
15
You will need to learn how to compile and run your program in the IDE.
16
Theres a lot going on behind the scenes in the IDE that you dont normally see. Anyone know?
!!
17
!!
The linker combines machine code with library code into an executable program.
18
19
20
1.! 2.!
HOW TO BACK UP YOUR WORK. HOW TO SAVE YOUR WORK (same thing as backing up your work). HOW TO SAVE YOUR WORK BY BACKING IT UP (repeated so you will know this is very important).
3.!
21
READY?????
22
23
24
The first line tells the compiler to include a service for stream input/output. It is needed to write on the screen.
ch01/hello.cpp
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6{ 7 cout << "Hello, World!" << endl; 8 return 0; 9}
25
The second line tells the compiler to use the standard namespace. This is used in conjunction with the first line to output -standard output.
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6{ 7 cout << "Hello, World!" << endl; 8 return 0; 9} ch01/hello.cpp
26
The next set of code defines a function. The name of this function is main.
ch01/hello.cpp
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6{ 7 cout << "Hello, World!" << endl; 8 return 0; 9}
27
The main function returns an integer (that is, a whole number without a fractional part, called int in C++) with value 0. This value indicates that the program finished successfully.
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6{ 7 cout << "Hello, World!" << endl; 8 return 0; 9}
ch01/hello.cpp
28
29
You can display more than one thing by re-using the << operator: << "Hello, World!" << endl;
ch01/hello.cpp
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6{ 7 cout << "Hello, World!" << endl; 8 return 0; 9}
30
! To display values on the screen, you send them to an entity called cout. ! The << operator denotes the send to command.
31
C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved
The endl symbol denotes an end of line marker which causes the cursor to move to the next screen row.
32
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6{ 7 cout << "Hello, World!" << endl ; 8 return 0 ; 9}
33
Error
ARGH!!!!
34
35
Error
Without that semicolon you actually wrote:
7! cout << "Hello, World!" << endl return 0; 8 }
which thoroughly confuses the compiler! This is a compile-time error or syntax error. A syntax error is a part of a program that does not conform to the rules of the programming language.
36
Error
Suppose you (accidentally of course) wrote: cot << "Hello World!" << endl;
! This will cause a compile-time error and the compiler will complain that it has no clue what you mean by cot. The exact wording of the error message is dependent on the compiler, but it might be something like Undefined symbol cot.
37
38
Errors
C++
!!
!!
!!
!!
39
Error
Consider this: cout << "Hollo, World!" << endl; Logic errors or run-time errors are errors in a program that compiles (the syntax is correct), but executes without performing the intended action.
really an error?
!!
!!
The programmer is responsible for inspecting and testing the program to guard against logic errors.
40
Error
Some kinds of run-time errors are so severe that they generate an exception: a signal from the processor that aborts the program with an error message. For example, if your program includes the statement cout << 1 / 0; your program may terminate with a divide by zero exception.
41
Error
!!
Every C++ program must have one and only one main function. Most C++ programs contain other functions besides main (more about functions later).
!!
42
Error
C++ !! is case sensitive. Typing:
int Main()
will compile but will not link. A link-time error occurs here when the linker cannot find the main function because you did not define a function named main. (Main is fine as a name but it is not the same as main and there has to be one main somewhere.)
43
Algorithms
An algorithm is a RECIPE
44
45
46
47
You have the choice of buying two cars. One is more fuel efficient than the other, but also more expensive. You know the price and fuel efficiency (in miles per gallon, mpg) of both cars. You plan to keep the car for ten years. Assume a price of gas is $4 per gallon and usage of 15,000 miles per year. You will pay cash for the car and not worry about financing costs.
48
In our sample problem, we have these inputs: purchase price1 and fuel efficiency1 the price and fuel efficiency (in mpg) of the first car !! purchase price2 and fuel efficiency2 the price and fuel efficiency of the second car
!!
We simply want to know which car is the better buy. That is the desired output.
49
3.! 4.!
The total cost for a car is purchase price + operating cost We assume a constant usage and gas price for ten years, so the operating cost depends on the cost of driving the car for one year. The operating cost is 10 x annual fuel cost The annual fuel cost is price per gallon x annual fuel consumed The annual fuel consumed is annual miles driven / fuel efficiency
50
51
If total cost1 < total cost2 The algorithm says: choose the FIRST CAR
52
END OF MODULE 1
53