You are on page 1of 54

INTRODUCTION TO C++

Module 3
Brief History | Turbo C++ Editor Environment | Elements of C++ | Structure of C++ Program
Brief History
 C++ is an extension of C
 Developed by Bjarne Stroustrup
 Early 1980’s at Bell Laboratories
 It provides capabilities for object-
oriented programming.
TURBO C++ EDITOR ENVIRONMENT

 To execute the integrated version of


Turbo C++, simply double-click the
icon available in your desktop.
MAIN MENU SCREEN
FILE MENU
COMPILE MENU
RUN MENU
SUMMARY OF THE MAIN MENU ITEMS
Item Description
FILE Loads and saves files, handles directories, invokes DOS, and
exits Turbo C++
EDIT Invokes the Turbo C++ editor
RUN Compiles, links, and runs the program currently loaded in the
environment
COMPILE Compiles the program currently in the environment
PROJECT Manages multiple projects
OPTIONS Sets various compiler, linker and environmental options

DEBUG Sets various debug options


BREAK/ Manages debugger watch expressions and break points
WATCH
ELEMENTS OF
C++ PROGRAMMING LANGUAGE
BASIC ELEMENTS OF A C++ PROGRAM
1. Character Set

2. Identifiers
• Variables
• Constants

3. Data Types

4. Operators
• Assignment
• Arithmetic
• Increment / Decrement
• Relational
• Logical
1. CHARACTER SET
Characters that can be used in a program are:
 Lowercase letters (a-z)
 Uppercase letters (A-Z)
 Digits (0-9)
 Special Characters (+,-,*,/,=,(,),{,}……)
2. IDENTIFIERS
These are a sequence of one or more
letters, digits, or underline symbols( _ ) that
are used to identify functions, classes, or
other elements of C++ language.

TYPES OF IDENTIFIERS
• Predefined Identifier – Reserved Words / Keywords
• User-defined Identifier – Variable Names
PREDEFINED IDENTIFIERS / C++ RESERVED WORDS

THE 32 ANSI KEYWORDS


auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
USER-DEFINED IDENTIFIERS WORDS

What is a variable?
 A variable is a data storage location that has a value,
which can change during program execution.
 By using a variable’s name in your program, you are, in
effect referring to the data stored there.
 You must initialize a variable - probably giving it a value
of zero - before you use it in a calculation or you need to
make sure that it is given a value in some other way
 All variables in C++ must be declared prior to their use.
How to create a variable name?
Variables can be given any name up to 31 characters in
length which is composed of any of the following
characters:
 Characters a to z and A to Z
(NOTE: upper case and lower case are distinct)
 The first character must be a letter.
 Only letters, digits or underscores may follow the
initial letter.
 Blank spaces and special characters are not allowed .
How to create a variable name?
 Use the underscore to separate words in a
name consisting of multiple words or
capitalize the first letter of one or more
words.
 Do not use underscore as the first character
of a name.
 Must not be any of the 32 keywords.
 Variable names must be meaningful.
Tell whether the following variables
are valid or invalid:

velocity @discount
#sam
unit_1
break
day_month_year
first-variable
temp_data
%_rate
pay_period
40_birthday
2percent
Tell whether the following variables
are valid or invalid:

velocity @discount
#sam
unit_1
break
day_month_year
first-variable
temp_data
%_rate
pay_period
40_birthday
2percent Red means invalid
How to declare variables?
Syntax:
datatype variable_name;
datatype var1, var2, varn;

Examples:
int value;
char letter; - - > l e t t e r v a r i a b l e ca n o n l y s t o r e s i n g l e ch a r a c t e r i n p u t
float num;
int a, size, x;
double interest, profit;
char name[20]; - - > n am e v ar i able can s to re u p to 20 characters i n pu t
Declaring variables with initial values

int value1 = 0;
int value2 = 27;
T H ER E A R E T HR EE P L A C ES IN A C+ + P R O GR A M WH ER E
VA R IA B L ES CA N B E D E C L A R ED :

1. Outside of all functions, including the main() function.


This sort of variable is called global and may be used
by any part of the program.
2. Inside the function. Variables in this way are called
local variables and may be used only by statements
that are also in the same function.
3. In the declaration of a formal parameter of a function.
The formal parameters are used to receive the
arguments when that function is called.
Constants
 A constant is any expression that has a
fixed value.
 Unlike a variable, the value stored in a
constant cannot be changed during
program execution.
CONSTANTS CAN BE ANY OF THE DATA TYPES:

1. Characters are enclosed between single quotes.


2. String constant contains a series of characters
enclosed by double quotation.
3. Integer numbers are specified as numerical
constants without fractional components. To
express a numerical constant we do not need to
write quotes (“) nor any special character.
4. Floating-point constants require the use of
decimal point followed by the number’s fractional
component. It may include a decimal point, an e
character or both.
ESCAPE CODES OR BACKSLASH CODES
These are special character constants that can be
expressed using its respective backslash code
CODE MEANING CODE MEANING
\b backspace \’ single quote
\f form feed \0 null
\n new line \\ backslash
\r carriage return \v vertical tab
\t horizontal tab \a alert
\” double quote \N octal constant
\x hexadecimal constant
3. DATA TYPES
Typical Bit
Type Meaning Typical Range
Width

char character 1byte -127 to 127 or 0 to 255

unsigned char 1byte 0 to 255

signed char 1byte -127 to 127

int integer 4bytes -2147483648 to 2147483647

unsigned int 4bytes 0 to 4294967295

signed int 4bytes -2147483648 to 2147483647

short int 2bytes -32768 to 32767


3. DATA TYPES
Typical Bit
Type Meaning Typical Range
Width
unsigned short int Range 0 to 65,535

signed short int Range -32768 to 32767

long int 4 bytes -2,147,483,647 to 2,147,483,647

signed long int 4 bytes same as long int

unsigned long int 4 bytes 0 to 4,294,967,295

float Floating point 4 bytes +/- 3.4e +/- 38 (~7 digits)

double Double Precision 8 bytes +/- 1.7e +/- 308 (~15 digits)

long double 8 bytes +/- 1.7e +/- 308 (~15 digits)

wchar_t Wide character 2 or 4 bytes 1 wide character


4. OPERATORS
These are the symbols that tell the compiler to
perform specific mathematical or logical
manipulations.

Types of Operators
 Assignment Operator
 Arithmetic Operators
 Compound Operators
 Relational Operators
 Logical Operators
a. Assignment Operator

In C++, assignment operator is a single equal


sign (=) which means that the value on the
right side of the assignment expression after
the equal sign is assigned to the variable on
the left.
Example:
sum = x+y;
assignment operator
b. Arithmetic Operators
C++ supports the following binary operators:
ORDER OF PRECEDENCE
(Left to Right Evaluation)
()

++, --
*, /, %

+, -

=
c. Compound Operators

Below are the compound operators:


+= , -= , *= , /=, %=

Example:
x += y; --> this means x=x+y
compound operator
d. Increment/Decrement Operator

These allow you to use a single operator


that adds 1 to or subtract 1 from any value.

Example:
a++
b--
d. Relational Operators
These are operators that allow the
comparison of two or more numerical values,
yielding a result based on whatever the
comparison is true(1) or false(0).
d. Relational Operators (cont.)

OPERATORS ACTION
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
== Equal to
!= Not equal
Example:

Expression Evaluates As
5 == 1 0 (false)
5 >= 1 1 (true)
5 != 1 1 (true)
(5+10) == (3*5) 1 (true)
e. Logical Operators
These operators work with logical values
true(1) and false(0) allowing to combine
relational operators.

OPERATORS ACTION
&& and
|| or
! not
The truth table of basic logical operations

p q p && q p || q !p
false false false false true
false true false true true
true false false true false
true true true true false
Examples:

Expression Evaluates As
(5 == 5) && (6 !=2) True
(5 > 1) || (6 < 1) True
STRUCTURE
OF A
C++ PROGRAM
GENERAL FORM OF A C++ PROGRAM
f u n c t i o n1 ( )
p reprocessor directives {
gl ob al decl arations; l o c a l d e c l a ra t io n ;
rem arks statement 1;
:
m ain ( ) statement n;
}
{
:
l ocal decl arations;
:
statem ent 1; f u n c t i o n2 ( )
: {
: l o c a l d e c l a ra t io n ;
statem ent n ; Statement 1;
g etch (); :
Statement n;
return val ue;
}
}
1. PREPROCESSOR DIRECTIVE

 These are various instructions in the source


code of the program written to be included
in the Turbo C++ compiler.
 These are not actually part of the C
language but they are used to expand the
scope of the C programming language.
 All preprocessor directives begins with a #
sign.
The #include Directive
 This directive instructs the computer to add the
contents of an include file into your program during
compilation.
 it is included because its functionality is used later
in the program.
Syntax:
#include<header_file>
Example:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>
The #define Directive
 This directive defines own names for constants. This
may also used to rename commands to shorten the
word but do not alter its function.

Syntax: #define identifier value

Example:
#define pi 3.14159
#define taxrate 0.0725
#define g gotoxy --> r e n a m i n g g o t o x y c o m m a n d t o l e t t e r g
The #define Directive
// defined constants: calculate circumference
#include < iostream.h>
#include< conio.h>
#define pi 3.14159
#define NEWLINE ' \n'
int main ()
{
double r = 5.0; // radius double circle;
circle = 2 * pi * r;
cout << circle; cout << NEWLINE;
getch();
return 0;
}
2. VARIABLE DEFINITION

A variable definition informs the compiler


of the variable name and the type of data it
is to hold.

Variables can be defined globally or


locally.
3. REMARKS/COMMENTS
 The text contained between /* and */ are considered
comments.
 They are ignored by the compiler.
 Also, // can be used to make a line of statement a
comment.
Examples:
/* this is your first program using the
language C*/
or
//This is your first program
//using the language C
4. MAIN() FUNCTION

The main function is the point where all C


programs begin their execution.
It is independent from whether it is at the
beginning, at the end, or at the middle of the
code.
Its content is always the first to be executed
when a program starts.
In addition, it is essential that all C++
programs have a main function.
5. PROGRAM STATEMENTS
 The real work of the C program is done by its
statements.
 C++ statements display information on the screen,
read keyboard input, perform mathematical
operations, call functions, read disks files, and all
other operations that a program needs to perform.
 A statement is a complete direction instructing the
computer to carry out some task.
 A compound statement , also called a block, is a
group of two or more C statements enclosed in
braces.
6. EXPRESSIONS
 An expression is anything that evaluates to a
numeric value.
 C++ expressions come in level of complexity.
 The simplest C expression consists of a single item:
a single variable, literal constant or symbolic
constant.
Example: PI = 3.1416
 More complex expressions consist of simpler
expressions connected by operators.
Example: x =2 + 8
y = 1.25/5+2*x*x/y
7. FUNCTION PROTOTYPE
A function prototype is a statement that
provides the C compiler with the name and
the arguments of the function contained in
the program and must appear before the
function is used.

Example:
void area(int l, int w);
int hypotenuse(int a, int b);
8. RETURN STATEMENT

The return instruction makes the main()


function finish and return the code that the
instruction is followed by, in this case 0.
Sample Program:

// my first program in C++ Hello World!


#include <iostream.h>
int main ()
{
cout << "Hello World!";
return 0;
}
/* my second program in C++ with more Hello World!
comments */ C++ programming is fun!
#include <iostream.h>
#include<conio.h>
void main ()
{
clrscr();
cout << "Hello World! ";
// says Hello World!
cout << “\nC++ programming is fun!";
getch();
}

You might also like