Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 51

INTRODUCTION

TO
C++
Turbo C++ Editor Environment | Elements of C++ | Structure of C++ Program
M A I N M E N U SCREEN
FILE M E N U
COMPILE M E N U
RUN MENU
S U M M A R Y OF THE M A I N M E N U 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++ PROGRAMM IN G
LANGUAGE
BASIC ELEMENTS OF A C + +
P R O G RA M
1. Character Set

2. Identifiers
• Variables
• Constants

3. Data Types

4. Operators
• Assignment
• Arithmetic
• Increment / Decrement
• Relational
• Logical
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 IDE NTIFIERS/C + + RE SERVED
WORDS
THE 3 2 ANSI K E
YWORDS
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
U SER-D EFIN ED I D EN TIFIER S
W O RD S
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_
break 1
day_month_year
first-variable
temp_dat
%_rat a
e pay_perio
40_birthda
d
y
2percent
TELL WHETHER THE FOLLOWING
VARIABLES ARE VALID OR INVALID:

velocity @discount
#sa
m unit_
break 1
day_month_year
first-variable
temp_data
%_rat
e pay_perio
40_birthda
d
2percent y Red means
invalid
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 C A N B E A N Y 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 O R 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


-2147483648 to
int integer 4bytes
2147483647
unsigned int 4bytes 0 to 4294967295
-2147483648 to
signed int 4bytes
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


HOW TO DECLARE VARIABLES?
Syntax:
datatype
variable_name; datatype
var1, var2, varn;
Examples
: int
value;
char
letter; -- >
lettervari
able cano
n l ys t o r e s
ingle char
acterinpu
t

float num;
int a, size,
DECLARING VARIABLES WITH INITIAL VALUES

int value1 = 0;
int value2 = 27;
T HE R E AR E T HR E E P L A C E S IN A C ++P R
OGR AM WH E R E VA R I A B L E S CA N BE D E C L
ARED:
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.
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
STRU CTU RE OF A
C + + P R O G RA M
GENERAL F O R M OF A C + + P R O G R A M
function1 ()
•p re p ro c e sso r d ire c t {
ive s glo b a l d e c la ra t l o c a l d e c l a ra t i
io n s; on; statement
•re m a rks 1;
:
statement n;
•m a in( ) }
•{ :
:
•lo c a l d e c la ra t i function2()
o n s ; st a te m e nt {
1; l o c a l d e c l a ra t i
•: on; Statement
1;
•:
:
•st a te m e nt Statement n;
n; g e tc h (); }
•re t urn va
lue ;
•}
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 --> renaminggo
toxy command to letter g
THE #DEFINE DIRECTIVE

// defined constants: calculate circumference


# include < iostream.h >
# include< conio.h >
# define pi 3. 1 4 1 5 9
# define NEWL INE ' \
n' int main ()
{
double r = 5. 0; // radius double
circle; circle = 2 * pi * r;
cout << circle; cout << NEWL INE;
getch();
return 0;
}
2. VARIABLE
D E FI N I TI ON

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.
Exam ples:
/* this is your first program using the
language C*/
or
//This is your first program
//using the language C
4. M A I N ( ) F U N C T I O N

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. P R O G RA M
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
Example:
More complex expressions
x =2 + 8 consist of
simpler expressions connected by
y = 1.25/5+2*x*x/y
operators.
7. F U N C T I O N
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;
}
HELLO WORLD!
C++ PROGRAMMING
/* my second program in C++ with IS FUN!
more
comments */
#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