2. What is c++ ?
C++ is a object-oriented language
It is the “successor” to C, a procedural language
(the “++” is called the successor operator in C++)
C was derived from a language called B which was in turn derived from
BCPL
C was developed in the 1970’s by Dennis Ritchie of AT&T Bell Labs
C++ was developed in the early 1980’s by Bjarne Stroustrup of AT&T Bell
Labs.
Most of C is a subset of C++
3. People & Programs
User: an individual who runs, or executes,
a program
Programmer: an individual who creates, or
writes, a program
4. C++ Programming
Consists of…
Declarations
Define the use of various identifiers, thus creating the elements used by the
program (computer)
Statements
Or executable statements, representing actions the computer will take on
the user’s behalf
5. Identifiers
Names for various entities used in a program;
used for...
Variables: values that can change frequently
Constants: values that never changes
Functions: programming units that represents
complex operations
Parameters: values that change infrequently
6. A Simple c++ Program
#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
7. The Header File
#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Compiler directive:
Tells the compiler
what to do before
compiling
This one includes
source code from
another file
8. The Main Statement
#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Header for main function
States…
data type for the return
value
identifier for function
list of arguments between
parenthesis
(none for this function)
9. The Braces
#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Braces enclose the body of
the function
They represent the start and
end of the function
10. The Declaration Statement
#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Declarations and
statements
Main body of function
(or main part)
“//” represents the start
of a comment
11. The return Statement
#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Return statement
specifies the value
the function returns
All (almost)
declarations and
statements end with
a semi-colon “;”
12. Sample C++ Program
#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
What is the Output of this Program?
13. The DataTypes
#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
Variable declaration
The identifier number is
declared as being of
data type int, or integer
14.
15. The Count ?
#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
cout
the output statement for C++
Note the direction of “<<“
endl represents an end-of-line
16. Cin ?
#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
cin
the input statement for C++
Note the direction of “>>”
17. Copy That Down
#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
18. Value Assignment In c++
Assignment is an operation that assigns
the value of an expression to a variable
Ex. Total = 2 + 3 + 5
First, the expresssion “2 + 3 + 5” is
evaluated
Then, this value is assigned to the variable
“Total”
19. Assignment
When a variable is declared, space is
allocated in the computer’s memory for
the variable
Each data type requires a different
number of bytes in memory for storing a
variable
int - 2
float - 4
double - 8
char, bool - 1
21. I Have A problem for you
Problem: To determine the average of three
numbers
Task: Request, from the user, three numbers,
compute the average and the three numbers,
and print out the original values and the
computed average
Do it!
You have 20 minutes!