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!