CS503 Lec1
CS503 Lec1
CS503 Lec1
Course Materials:
1. Lecture notes
2. Textbooks:
a. Robert Lafore: Object-Oriented Programming in C++, 4 th edition, Sams Publishing, 2002.
b. D.S. Malik: C++ Programming: From Problem Analysis to Program Design, Cengage Learning, 2013.
3.
Topics:
1. C++ Programming Basics
2. Decisions and Loops
3. Functions
4. Structs
5. Arrays
6. Pointers
7. Overloading
8. Objects, Classes, and inheritance
9. Streams and Files
Student Assessment:
1.
2.
3.
4.
I recommend that you type all of the examples in every lecture, even
when they are very simple. Keying stuff makes it less likely that you
will forget things later. Dont be afraid to experiment with the code.
Making mistakes is very educational in programming. The more
mistakes you make early, the more you are likely to learn.
The best way to learn to program is by writing programs! At the end of
each lecture and lap, you will find some exercises and/or a quiz.
Please answer them and evaluate your work. The later lectures build
on what you have learned in the earlier ones, so be certain you fully
understand the material before moving on.
The exercises further reinforce learning and challenge students to write
C++ programs with a specified outcome.
Lecture 1:
Getting Started
Your Development Environment: this will be explained in the laps.
Source files are text files and have extension .CPP
Executable files have the .EXE extension
Compilers take source code and transform it into executable files, which
your computer can run as it does with other programs.
3.
// first.cpp
Comments
3 #include <iostream>
4 Using namespace std;
int main()
preprocessor directive
cout << "Welcome to C++!\n";
8
9
return 0;
10 }
Welcome to C++!
Comments
- They help understand the code by providing explanation about it, and
- help you remember the key details of your program in future.
Comments (Cont.)
Comment Syntax
1. Start with a double slash symbol // and terminate at the end of the line.
(This is one of the exceptions to the rule that the compiler ignores whitespace).
Can start at the beginning of the line or on the same line following a program statement.
EX:
// demonstrates comments // at the beginning of the line
#include <iostream>
// following a program statement
// preprocessor directive
// using directive
// function name main
// start the function body
// statement
// statement
// end of the function body
Comments (Cont.)
2. Begins with the /* and ends with */
EX:
/*
This is an old-style comment.
But it is a good approach to make a comment of large text since it
saves inserting // in every line.
It has advantages in special situations. You can write a multi-line
comment with only 2 comment symbols that are: /* and */
*/
Directives
1. Preprocessor Directives: is an instruction to the compiler. It starts with #.
Directives (Cont.)
2. The using directive
NOTE: A namespace is a part of the program in which certain names are recognized and
are unknown outside of this namespace.
The directive using namespace std; says that all the program statements
that follow are within the std namespace.
EX: cout is declared within this namespace.
If we dont use the using directive, we will need to add the std many times.
EX: std::cout << "Welcome to C++!\n";
To avoid adding std:: dozens of times in programs, we use the using directive.
Function name
The parentheses ( ) following the word main are the distinguishing feature
of a function. Theyre used to hold function arguments- values passed
from the calling program to the function.
Program Statements
Program statements are instructions to the computer to do something. such
as adding two numbers or printing a sentence.
A semicolon ; signals the end of the statement. This is a crucial part of the
syntax but easy to forget.
If you leave the semicolon, the compiler will often (although not always)
signal an error. See the next slide.
EX:
White space
The end of a line isnt important to a C++ compiler.
You can put several statements on one line, separated by any number of spaces or tabs,
or you can run a statement over 2 or more lines. Its all the same to the compiler.
EX:
The previous example can be written this way:
#include <iostream>
using
namespace std;
int main () { cout
<<
Hello world !\n
; return
0;}
It will be compiled correctly but it is hard to read.
2. Also, string constants, such as Every age has a language of its own,
cannot be broken into separate lines. (If you need a long string constant,
you can insert a backslash (\) at the line break or divide the string into
two separate strings, each surrounded by quotes.)
The name reflects the fact that the backslash causes an escape from the normal way characters are
interpreted. EX: t is interpreted not as the character t but as the tab character.
Escape sequences can be used as separate characters or embedded in string constants.
Escape
Sequence
Name
Description
\n
New line
\t
Horizontal tab Causes the cursor to skip over to the next tab stop
\a
Alarm
\b
Backspace
\r
Return
\\
Backslash
\'
Single quote
\"
Double quote
// first.cpp
#include <iostream>
int main()
1. Load <iostream>
2. main
9
10
return 0;
11 }
Welcome to C++!
// first.cpp
#include <iostream>
1. Load <iostream>
2. main
int main()
8
9
2.2 newline
return 0;
10 }
2.4 newline
2.5 newline
2.6 Print "C++!"
Welcome
to
2.7 newline
C++!
Program Output
Variables
Location in memory where a value can be stored for use by a program.
Must be declared with a name and a data type before they can be used.
Some common data types are:
int
- integer numbers
double - floating point numbers
char - characters
EX: int myvariable;
//Declares a variable named myvariable of type int
EX: int variable1, variable2; //Declares two variables, each of type int
You can place variable declarations anywhere in a program. Its not necessary to
declare variables before the first executable statement. However, its more
readable if commonly-used variables are located at the beginning of the program.
Variable names
Every variable has a name, a type, a size and a value.
The names given to variables are called identifiers.
Whenever a new value is placed into (assigned to) a variable, it replaces
the previous value the old value is destroyed.
Reading variables from memory does not change them.
The rules for writing identifiers:
You can use upper and lowercase letters, the digits from 1to 9, and the underscore _
// ftemp is a variable
causes the program to wait for the user to type a number. The resulting
number will be placed in the variable ftemp.
Assignment Statements
Assign value to a variable.
The equal sign = causes the value on the right to be assigned to the
variable on the left.
EX:
var1 = 20;
Operators
Arithmetic operators:
Arithmetic calculations
Integer division truncates remainder
7 / 5 evaluates to 1
Operator precedence
Some arithmetic operators act before others (EX: multiplication before addition).
Be sure to use parenthesis when needed
EX:
Find the average of three variables a, b and c
Do not use: a + b + c / 3
Use: (a + b + c ) / 3
// AddingTwoIntegerNumbers.cpp
// Addition program
#include <iostream>
//we didnt write using namespace std; so well repeat std::cout many times
1. Load <iostream>
int main()
2. main
8
9
// declaration
10
// read an integer
11
12
// read an integer
13
// assignment of sum
14
std::cout << The sum is: " << sum << std::endl; // print sum
15
16
return 0;
17 }
Homework:
Re write this program by adding using namespace
Program Output
5 minute question:
What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int y = 7;
cout << x + y / 2 << endl;
return 0;
}
Sample questions:
1. What the following program does without running it?
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 7;
cout << x + y << << x * y;
cout << endl;
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3. What is the output of: cout << \Run, Spot, run,\ she said.;
#include <iostream>
using namespace std;
int main()
{
int ftemp;
//for temperature in fahrenheit
cout << Enter temperature in fahrenheit: ;
cin >> ftemp;
int ctemp;
ctemp = (ftemp - 32) * 5 / 9;
cout << Equivalent in Celsius is: << ctemp << \n;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////