SlideShare una empresa de Scribd logo
1 de 41
OBJECT ORIENTED PROGRAMMING
WITH C++
Tokens, Expressions and
Control Structures
Tokens
A token is a language element that can be used in
constructing higher-level language constructs.
C++ has the following Tokens:
 Keywords (Reserved words)
 Identifiers
 Constants
 Strings (String literals)
 Punctuators
 Operators
Keywords
Implements specific C++ language features.
They are explicitly reserved identifiers and cannot be used as
names for the program variables or other user-defined program
elements.
Following table is a list of keywords in C++:
asm delete if return try
auto do inline short typedef
break double int signed union
case else long sizeof unsigned
catch enum new static virtual
char extern operator struct void
class float private switch volatile
const for protected template while
continue friend public this
default goto register throw
Identifiers
Identifiers refer to the names of variables, functions, arrays, classes
etc.
Rules for constructing identifiers:
 Only alphabetic characters, digits and underscores are permitted.
 The name cannot start with a digit.
 Uppercase and lowercase letters are distinct.
 A declared keyword cannot be used as a variable name.
 There is virtually no length limitation. However, in many
implementations of C++ language, the compilers recognize only the first
32 characters as significant.
 There can be no embedded blanks.
Constants
Constants are entities that appear in the program code as fixed
values.
There are four classes of constants in C++:
 Integer
 Floating-point
 Character
 Enumeration
Integer Constants
 Positive or negative whole numbers with no fractional part.
 Commas are not allowed in integer constants.
 E.g.: const int size = 15;
const length = 10;
 A const in C++ is local to the file where it is created.
 To give const value external linkage so that it can be referenced from
another file, define it as an extern in C++.
 E.g.: extern const total = 100;
Constants
Floating-point Constants
 Floating-point constants are positive or negative decimal numbers with
an integer part, a decimal point, and a fractional part.
 They can be represented either in conventional or scientific notation.
 For example, the constant 17.89 is written in conventional notation. In
scientific notation it is equivalent to 0.1789X102. This is written as
0.1789E+2 or as 0.1789e+2. Here, E or e stands for ‘exponent’.
 Commas are not allowed.
 In C++ there are three types of floating-point constants:
float - f or F 1.234f3 or 1.234F3
double - e or E 2.34e3 or 2.34E3
long double - l or L 0.123l5 or 0.123L5
Character Constant
 A Character enclosed in single quotation marks.
 E.g. : ‘A’, ‘n’
Constants
Enumeration
 Provides a way for attaching names to numbers.
 E.g.:
enum {X,Y,Z} ;
 The above example defines X,Y and Z as integer constants with values
0,1 and 2 respectively.
 Also can assign values to X, Y and Z explicitly.
enum { X=100, Y=50, Z=200 };
Strings
A String constant is a sequence of any number of characters
surrounded by double quotation marks.
E.g.:
“This is a string constant.”
Punctuators
Punctuators in C++ are used to delimit various syntactical units.
The punctuators (also known as separators) in C++ include the
following symbols:
[ ] ( ) { } , ; : … * #
Basic Data Types
Size and Range of Data Types
Basic Data Types
ANSI C++ added two more data types
 bool
 wchar_t
Data Type - bool
A variable with bool type can hold a Boolean value true
or false.
Declaration:
bool b1; // declare b1 as bool type
b1 = true; // assign true value to b1
bool b2 = false; // declare and initialize
The default numeric value of true is 1 and
false is 0.
Data Type – wchar_t
The character type wchar_t has been defined to
hold 16-bit wide characters.
wide_character uses two bytes of memory.
wide_character literal in C++
begin with the letter L
L‘xy’ // wide_character literal
Built-in Data Types
int, char, float, double are known as basic or
fundamental data types.
Signed, unsigned, long, short modifier for integer
and character basic data types.
Long modifier for double.
Built-in Data Types
Type void was introduced in ANSI C.
Two normal use of void:
o To specify the return type of a function when it is
not returning any value.
o To indicate an empty argument list to a function.
o eg:- void function-name ( void )
Built-in Data Types
Type void can also used for declaring generic pointer.
A generic pointer can be assigned a pointer value of any
basic data type, but it may not be de-referenced.
void *gp; // gp becomes generic pointer
int *ip; // int pointer
gp = ip; // assign int pointer to void pointer
Assigning any pointer type to a void pointer is allowed
in C .
Built-in Data Types
void *gp; // gp becomes generic pointer
int *ip; // int pointer
ip = gp; // assign void pointer to int pointer
This is allowed in C. But in C++ we need to use a cast
operator to assign a void pointer to other type
pointers.
ip = ( int * ) gp; // assign void pointer to int pointer
// using cast operator
*ip = *gp;  is illegal
User-Defined Data Types
Structures & Classes:
struct
union
class
Legal data types in C++.
Like any other basic data type to declare variables.
The class variables are known as objects.
User-Defined Data Types
User-Defined Data Types
Enumerated DataType:
Enumerated data type provides a way for attaching
names to numbers.
enum keyword automatically enumerates a list of
words by assigning them values 0, 1, 2, and so on.
enum shape {circle, square, triangle};
enum colour {red, blue, green, yellow};
enum position {off, on};
User-Defined Data Types
Enumerated DataType:
enum colour {red, blue, green, yellow};
In C++ the tag names can be used to declare
new variables.
colour background;
In C++ each enumerated data type retains its
own separate type.C++ does not permit an int
value to be automatically converted to an enum
value.
User-Defined Data Types
Enumerated DataType:
colour background = blue; // allowed
colour background = 1; // error in C++
colour background = (colour) 1; // OK
int c = red; // valid
Derived Data Types
Arrays
The application of arrays in C++ is similar to that
in C.
Functions
top-down - structured programming ; to reduce
length of the program ; reusability ; function
over-loading.
Derived Data Types
Pointers
Pointers can be declared and initialized as in C.
int * ip; // int pointer
ip = &x; // address of x assigned to ip
*ip = 10; // 10 assigned to x through indirection
Derived Data Types
Pointers
C++ adds the concept of constant pointer and
pointer to a constant.
char * const ptr1 = “GOODS”; // constant pointer
int const * ptr2 = &m; // pointer to a constant
Symbolic Constants
Two ways of creating symbolic constant in C++.
Using the qualifier const
Defining a set of integer constants using enum
keyword.
Any value declared as const can not be modified by
the program in any way. In C++, we can use const in
a constant expression.
const int size = 10;
char name[size]; //This is illegal in C.
Symbolic Constants
const allows us to create typed constants.
#define - to create constants that have no type
information.
The named constants are just like variables except that
their values can not be changed. C++ requires a const to
be initialized.
A const in C++ defaults, it is local to the file where it is
declared.To make it global the qualifier extern is used.
Symbolic Constants
extern const int total = 100;
enum { X,Y, Z };
This is equivalent to
const int X = 0;
const intY = 1;
const int Z = 2;
Reference Variables
A reference variable provides an alias for a
previously defined variable.
For eg., if we make the variable sum a reference to
the variable total, then sum and total can be used
interchangeably to represent that variable.
data-type & reference-name = variable-name
float total = 100;
float &sum = total;
Reference Variables
A reference variable must be initialized at the time
of declaration.This establishes the correspondence
between the reference and the data object which it
names.
int x ;
int *p = &x ;
int & m = *p ;
continue…
Reference Variables
void f ( int & x )
{
x = x + 10;
}
int main ( )
{
int m = 10;
f (m);
}
continue…
When the function call f(m) is
executed,
int & x = m;
Operators
Operators are tokens that result in some kind of computation or
action when applied to variables or other elements in an expression.
Some examples of operators are:
( ) ++ -- * / % + - << >> < <= > >= ==
!= = += -= *= /= %=
Operators act on operands. For example, CITY_RATE,
gross_income are operands for the multiplication operator, * .
An operator that requires one operand is a unary operator, one that
requires two operands is binary, and an operator that acts on three
operands is ternary.
Dynamic initialization of variables
Dynamic Initialization refers to initializing a variable at
runtime. If you give a C++ statement as shown below, it
refers to static initialization, because you are assigning
a constant to the variable which can be resolved at
compile time.
Int x = 5;
Whereas, the following statement is called dynamic
initialization, because it cannot be resolved at compile
time.
In x = a * b;
Initializing x requires the value of a and b. So it can be
resolved only during run time.
Arithmetic Operators
The basic arithmetic operators in C++ are the same as in most other
computer languages.
Following is a list of arithmetic operators in C++:
Modulus Operator: a division operation where two integers are divided and
the remainder is the result.
E.g.: 10 % 3 results in 1, 12 % 7 results in 5 and 20 % 5 results in 0.
Integer Division: the result of an integer divided by an integer is always an
integer (the remainder is truncated).
E.g.: 10/3 results in 3, 14/5 results in 2 and 1/2 results in 0.
1./2. or 1.0/2.0 results in 0.5
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Assignment Operators
E.g.:
x = x + 3; x + = 3;
x = x - 3; x - = 3;
x = x * 3; x * = 3;
x = x / 3; x / = 3;
Increment & Decrement Operators
Name of the operator Syntax Result
Pre-increment ++x Increment x by 1 before use
Post-increment x++ Increment x by 1 after use
Pre-decrement --x Decrement x by 1 before use
Post-decrement x-- Decrement x by 1 before use
E.g.:
int x=10, y=0;
y=++x; ( x = 11, y = 11)
y=x++;
y=--x;
y=x--;
y=++x-3; y=x+++5; y=--x+2; y=x--+3;
Relational Operators
Using relational operators we can direct the computer to compare two
variables.
Following is a list of relational operators:
E.g.: if (thisNum < minimumSoFar) minimumSoFar = thisNum
if (numberOfLegs != 8) thisBug = insect
In C++ the truth value of these expressions are assigned numerical values:
a truth value of false is assigned the numerical value zero and the value
true is assigned a numerical value best described as not zero.
Operator Meaning
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to
Logical Operators
Logical operators in C++, as with other computer languages, are used to
evaluate expressions which may be true or false.
Expressions which involve logical operations are evaluated and found to be
one of two values: true or false.
Examples of expressions which contain relational and logical operators
if ((bodyTemp > 100) && (tongue == red)) status = flu;
Operator Meaning Example of use Truth Value
&& AND (exp 1) && (exp 2) True if exp 1 and exp 2 are BOTH true.
|| OR (exp 1) || (exp 2) True if EITHER (or BOTH) exp 1 or exp 2
are true.
! NOT ! (exp 1) Returns the opposite truth value of exp 1;
if exp 1 is true, ! (exp 1) is false; if exp 1
is false, ! (exp 1) is true.
Operator Precedence
Following table shows the precedence and associativity of all the
C++ operators. (Groups are listed in order of decreasing
precedence.)
Operator Associativity
:: left to right
-> . ( ) [ ] postfix ++ postfix -- left to right
prefix ++ prefix -- ~ ! unary + unary –
Unary * unary & (type) sizeof new delete
right to left
->* * left to right
* / % left to right
+ - left to right
<< >> left to right
< <= > >= left to right
== != left to right
& left to right
^ left to right
| left to right
Operator Precedence Contd…
Operator Associativity
&& left to right
|| left to right
?: left to right
= *= /= %= += -=
<<= >>= &= ^= |=
right to left
, left to right

Más contenido relacionado

La actualidad más candente

C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
princepavan
 
Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)
bolovv
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
Manisha Keim
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
vip_du
 
Introduction of flex
Introduction of flexIntroduction of flex
Introduction of flex
vip_du
 

La actualidad más candente (20)

Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
C++ theory
C++ theoryC++ theory
C++ theory
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
C language
C languageC language
C language
 
1 puc programming using c++
1 puc programming using c++1 puc programming using c++
1 puc programming using c++
 
Data Handling
Data HandlingData Handling
Data Handling
 
Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C Theory
C TheoryC Theory
C Theory
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
 
C++ language
C++ languageC++ language
C++ language
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
 
Introduction of flex
Introduction of flexIntroduction of flex
Introduction of flex
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , Overview
 

Similar a Object Oriented Programming with C++

presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 

Similar a Object Oriented Programming with C++ (20)

Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
 
fds unit1.docx
fds unit1.docxfds unit1.docx
fds unit1.docx
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Mca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c languageMca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c language
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c language
 
Diploma ii cfpc u-2 datatypes and variables in c language
Diploma ii  cfpc u-2 datatypes and variables in c languageDiploma ii  cfpc u-2 datatypes and variables in c language
Diploma ii cfpc u-2 datatypes and variables in c language
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Basics of c
Basics of cBasics of c
Basics of c
 
Data type
Data typeData type
Data type
 
Getting started with C++
Getting started with C++Getting started with C++
Getting started with C++
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 

Más de Rokonuzzaman Rony (20)

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Pointer
PointerPointer
Pointer
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
 
Structure
StructureStructure
Structure
 
Pointers
 Pointers Pointers
Pointers
 
Loops
LoopsLoops
Loops
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Array
ArrayArray
Array
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
C Programming language
C Programming languageC Programming language
C Programming language
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Numerical Method 2
Numerical Method 2Numerical Method 2
Numerical Method 2
 
Numerical Method
Numerical Method Numerical Method
Numerical Method
 
Data structures
Data structuresData structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 

Object Oriented Programming with C++

  • 1. OBJECT ORIENTED PROGRAMMING WITH C++ Tokens, Expressions and Control Structures
  • 2. Tokens A token is a language element that can be used in constructing higher-level language constructs. C++ has the following Tokens:  Keywords (Reserved words)  Identifiers  Constants  Strings (String literals)  Punctuators  Operators
  • 3. Keywords Implements specific C++ language features. They are explicitly reserved identifiers and cannot be used as names for the program variables or other user-defined program elements. Following table is a list of keywords in C++: asm delete if return try auto do inline short typedef break double int signed union case else long sizeof unsigned catch enum new static virtual char extern operator struct void class float private switch volatile const for protected template while continue friend public this default goto register throw
  • 4. Identifiers Identifiers refer to the names of variables, functions, arrays, classes etc. Rules for constructing identifiers:  Only alphabetic characters, digits and underscores are permitted.  The name cannot start with a digit.  Uppercase and lowercase letters are distinct.  A declared keyword cannot be used as a variable name.  There is virtually no length limitation. However, in many implementations of C++ language, the compilers recognize only the first 32 characters as significant.  There can be no embedded blanks.
  • 5. Constants Constants are entities that appear in the program code as fixed values. There are four classes of constants in C++:  Integer  Floating-point  Character  Enumeration Integer Constants  Positive or negative whole numbers with no fractional part.  Commas are not allowed in integer constants.  E.g.: const int size = 15; const length = 10;  A const in C++ is local to the file where it is created.  To give const value external linkage so that it can be referenced from another file, define it as an extern in C++.  E.g.: extern const total = 100;
  • 6. Constants Floating-point Constants  Floating-point constants are positive or negative decimal numbers with an integer part, a decimal point, and a fractional part.  They can be represented either in conventional or scientific notation.  For example, the constant 17.89 is written in conventional notation. In scientific notation it is equivalent to 0.1789X102. This is written as 0.1789E+2 or as 0.1789e+2. Here, E or e stands for ‘exponent’.  Commas are not allowed.  In C++ there are three types of floating-point constants: float - f or F 1.234f3 or 1.234F3 double - e or E 2.34e3 or 2.34E3 long double - l or L 0.123l5 or 0.123L5 Character Constant  A Character enclosed in single quotation marks.  E.g. : ‘A’, ‘n’
  • 7. Constants Enumeration  Provides a way for attaching names to numbers.  E.g.: enum {X,Y,Z} ;  The above example defines X,Y and Z as integer constants with values 0,1 and 2 respectively.  Also can assign values to X, Y and Z explicitly. enum { X=100, Y=50, Z=200 };
  • 8. Strings A String constant is a sequence of any number of characters surrounded by double quotation marks. E.g.: “This is a string constant.”
  • 9. Punctuators Punctuators in C++ are used to delimit various syntactical units. The punctuators (also known as separators) in C++ include the following symbols: [ ] ( ) { } , ; : … * #
  • 11. Size and Range of Data Types
  • 12. Basic Data Types ANSI C++ added two more data types  bool  wchar_t
  • 13. Data Type - bool A variable with bool type can hold a Boolean value true or false. Declaration: bool b1; // declare b1 as bool type b1 = true; // assign true value to b1 bool b2 = false; // declare and initialize The default numeric value of true is 1 and false is 0.
  • 14. Data Type – wchar_t The character type wchar_t has been defined to hold 16-bit wide characters. wide_character uses two bytes of memory. wide_character literal in C++ begin with the letter L L‘xy’ // wide_character literal
  • 15. Built-in Data Types int, char, float, double are known as basic or fundamental data types. Signed, unsigned, long, short modifier for integer and character basic data types. Long modifier for double.
  • 16. Built-in Data Types Type void was introduced in ANSI C. Two normal use of void: o To specify the return type of a function when it is not returning any value. o To indicate an empty argument list to a function. o eg:- void function-name ( void )
  • 17. Built-in Data Types Type void can also used for declaring generic pointer. A generic pointer can be assigned a pointer value of any basic data type, but it may not be de-referenced. void *gp; // gp becomes generic pointer int *ip; // int pointer gp = ip; // assign int pointer to void pointer Assigning any pointer type to a void pointer is allowed in C .
  • 18. Built-in Data Types void *gp; // gp becomes generic pointer int *ip; // int pointer ip = gp; // assign void pointer to int pointer This is allowed in C. But in C++ we need to use a cast operator to assign a void pointer to other type pointers. ip = ( int * ) gp; // assign void pointer to int pointer // using cast operator *ip = *gp;  is illegal
  • 19. User-Defined Data Types Structures & Classes: struct union class Legal data types in C++. Like any other basic data type to declare variables. The class variables are known as objects.
  • 21. User-Defined Data Types Enumerated DataType: Enumerated data type provides a way for attaching names to numbers. enum keyword automatically enumerates a list of words by assigning them values 0, 1, 2, and so on. enum shape {circle, square, triangle}; enum colour {red, blue, green, yellow}; enum position {off, on};
  • 22. User-Defined Data Types Enumerated DataType: enum colour {red, blue, green, yellow}; In C++ the tag names can be used to declare new variables. colour background; In C++ each enumerated data type retains its own separate type.C++ does not permit an int value to be automatically converted to an enum value.
  • 23. User-Defined Data Types Enumerated DataType: colour background = blue; // allowed colour background = 1; // error in C++ colour background = (colour) 1; // OK int c = red; // valid
  • 24. Derived Data Types Arrays The application of arrays in C++ is similar to that in C. Functions top-down - structured programming ; to reduce length of the program ; reusability ; function over-loading.
  • 25. Derived Data Types Pointers Pointers can be declared and initialized as in C. int * ip; // int pointer ip = &x; // address of x assigned to ip *ip = 10; // 10 assigned to x through indirection
  • 26. Derived Data Types Pointers C++ adds the concept of constant pointer and pointer to a constant. char * const ptr1 = “GOODS”; // constant pointer int const * ptr2 = &m; // pointer to a constant
  • 27. Symbolic Constants Two ways of creating symbolic constant in C++. Using the qualifier const Defining a set of integer constants using enum keyword. Any value declared as const can not be modified by the program in any way. In C++, we can use const in a constant expression. const int size = 10; char name[size]; //This is illegal in C.
  • 28. Symbolic Constants const allows us to create typed constants. #define - to create constants that have no type information. The named constants are just like variables except that their values can not be changed. C++ requires a const to be initialized. A const in C++ defaults, it is local to the file where it is declared.To make it global the qualifier extern is used.
  • 29. Symbolic Constants extern const int total = 100; enum { X,Y, Z }; This is equivalent to const int X = 0; const intY = 1; const int Z = 2;
  • 30. Reference Variables A reference variable provides an alias for a previously defined variable. For eg., if we make the variable sum a reference to the variable total, then sum and total can be used interchangeably to represent that variable. data-type & reference-name = variable-name float total = 100; float &sum = total;
  • 31. Reference Variables A reference variable must be initialized at the time of declaration.This establishes the correspondence between the reference and the data object which it names. int x ; int *p = &x ; int & m = *p ; continue…
  • 32. Reference Variables void f ( int & x ) { x = x + 10; } int main ( ) { int m = 10; f (m); } continue… When the function call f(m) is executed, int & x = m;
  • 33. Operators Operators are tokens that result in some kind of computation or action when applied to variables or other elements in an expression. Some examples of operators are: ( ) ++ -- * / % + - << >> < <= > >= == != = += -= *= /= %= Operators act on operands. For example, CITY_RATE, gross_income are operands for the multiplication operator, * . An operator that requires one operand is a unary operator, one that requires two operands is binary, and an operator that acts on three operands is ternary.
  • 34. Dynamic initialization of variables Dynamic Initialization refers to initializing a variable at runtime. If you give a C++ statement as shown below, it refers to static initialization, because you are assigning a constant to the variable which can be resolved at compile time. Int x = 5; Whereas, the following statement is called dynamic initialization, because it cannot be resolved at compile time. In x = a * b; Initializing x requires the value of a and b. So it can be resolved only during run time.
  • 35. Arithmetic Operators The basic arithmetic operators in C++ are the same as in most other computer languages. Following is a list of arithmetic operators in C++: Modulus Operator: a division operation where two integers are divided and the remainder is the result. E.g.: 10 % 3 results in 1, 12 % 7 results in 5 and 20 % 5 results in 0. Integer Division: the result of an integer divided by an integer is always an integer (the remainder is truncated). E.g.: 10/3 results in 3, 14/5 results in 2 and 1/2 results in 0. 1./2. or 1.0/2.0 results in 0.5 Operator Meaning + Addition - Subtraction * Multiplication / Division % Modulus
  • 36. Assignment Operators E.g.: x = x + 3; x + = 3; x = x - 3; x - = 3; x = x * 3; x * = 3; x = x / 3; x / = 3;
  • 37. Increment & Decrement Operators Name of the operator Syntax Result Pre-increment ++x Increment x by 1 before use Post-increment x++ Increment x by 1 after use Pre-decrement --x Decrement x by 1 before use Post-decrement x-- Decrement x by 1 before use E.g.: int x=10, y=0; y=++x; ( x = 11, y = 11) y=x++; y=--x; y=x--; y=++x-3; y=x+++5; y=--x+2; y=x--+3;
  • 38. Relational Operators Using relational operators we can direct the computer to compare two variables. Following is a list of relational operators: E.g.: if (thisNum < minimumSoFar) minimumSoFar = thisNum if (numberOfLegs != 8) thisBug = insect In C++ the truth value of these expressions are assigned numerical values: a truth value of false is assigned the numerical value zero and the value true is assigned a numerical value best described as not zero. Operator Meaning > Greater than >= Greater than or equal to < Less than <= Less than or equal to == Equal to != Not equal to
  • 39. Logical Operators Logical operators in C++, as with other computer languages, are used to evaluate expressions which may be true or false. Expressions which involve logical operations are evaluated and found to be one of two values: true or false. Examples of expressions which contain relational and logical operators if ((bodyTemp > 100) && (tongue == red)) status = flu; Operator Meaning Example of use Truth Value && AND (exp 1) && (exp 2) True if exp 1 and exp 2 are BOTH true. || OR (exp 1) || (exp 2) True if EITHER (or BOTH) exp 1 or exp 2 are true. ! NOT ! (exp 1) Returns the opposite truth value of exp 1; if exp 1 is true, ! (exp 1) is false; if exp 1 is false, ! (exp 1) is true.
  • 40. Operator Precedence Following table shows the precedence and associativity of all the C++ operators. (Groups are listed in order of decreasing precedence.) Operator Associativity :: left to right -> . ( ) [ ] postfix ++ postfix -- left to right prefix ++ prefix -- ~ ! unary + unary – Unary * unary & (type) sizeof new delete right to left ->* * left to right * / % left to right + - left to right << >> left to right < <= > >= left to right == != left to right & left to right ^ left to right | left to right
  • 41. Operator Precedence Contd… Operator Associativity && left to right || left to right ?: left to right = *= /= %= += -= <<= >>= &= ^= |= right to left , left to right