Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

OOPS

Chapter 3
~ Tokens, Expressions
and Control Structures
Tokens
The smallest individual units in a program are known as tokens

Keywords Identifiers

Constants Strings

Operators

A C++ program is written using these tokens, white spaces and


syntax of the language.
•Explicitly reserved identifiers
•Cannot be used as names for the program
Keywords variables or other user defined program
elements
•There are 63 keywords in C++

•Identifiers refer to name of variables,


functions, arrays, classes etc. created by the
Identifiers and programmer
Constants •Constants(literals) refer to the fixed values
that do not change during the execution of
the program

“wchar_t” type is a wide character literal intended for character sets that cannot fit a
character into a single byte
Basic Data Types
C++ data
types

User defined Derived


Built-in type
type Type

structure Integral type Void Float type arrays

class int float functions

unions char double pointers

enumeration references
Uses of void
 to specify return type of a function when it is not returning any
value
 to indicate an empty argument list to a function
 declaration of generic pointers
void *gp; gp is a generic pointer

To assign a void pointer to other type pointer in C++, we


use cast operator
Eg: void *ptr1;
char *ptr2;
ptr2=(char *)ptr1;
• used for grouping together elements
Structures
with dissimilar types

• are conceptually similar to structures as


they allow us to group together dissimilar
Unions type elements

 We can say,
Unions are memory-efficient alternatives of structures
particularly in situations where it is not required to access the
different member elements simultaneously
Structures Unions
1. defined with ‘struct’ keyword 1. defined with ‘union’ keyword
2. all members can be 2. only one member can be
manipulated simultaneously manipulated at a time
3. size of a structure object is 3. size of union is equal to the size
equal to the sum of individual of largest member object
sizes of the member objects 4. union members share
4. allocated distinct memory common memory space for
locations their exclusive usage
5. are not memory efficient when 5. memory efficient when
compared to unions members are not required to
6. structures in C++ behave just be accessed simultaneously
like a class 6. retain their core functionality
Enumerated Data Type:
 provides a way for attaching names to members
 keyword used – enum
 syntax is similar to structure:
eg. enum shape {circle, square, triangle}
0 1 2
Here shape becomes a new data type using which we can
declare new variables
 By default, enumerators are assigned integer values starting
from 0 and so on
 An enum within a class is local to that class only
Storage Classes:
Automatic External Static Register

Lifetime function block entire program entire program function block

Visibility local global local local

Initialisation garbage 0 0 garbage

Storage stack segment data segment data segment CPU registers

Purpose used by single used local variables variables using


function throughout the retaining their CPU registers for
program values storage
throughout the purposes
program

Keyword auto extern static register


Derived data types:
 Arrays: for a character array it is compulsory to declare an
array with the size one greater than the intended size for the
null character
C C++
e.g. char string[3]=“xyz” ✓ Χ
char string[4]=“xyz” ✓ ✓

 Pointers: C++ adds the concept of ‘constant pointer’ and


‘pointer to a constant’.
char *const ptr1 = “GOOD”; //constant pointer
we cannot modify the address that ptr1 is initialised to.
int const* ptr2=&m; //pointer to a constant
ptr2 is declared as a pointer to a constant. It can point to any
variable of correct type, but the contents of what it points to
cannot be changed.

We can also declare both the variables and pointers as constants


const char* const cp = “xyz”;

 Functions :
(Chapter 4)
Symbolic Constants
 There are two ways of creating symbolic constants:
~ using qualifier ‘const’ and
~ defining a set of integer constants using enum keyword
➢ const in C++ are local, however they can be made global using
keyword extern
➢ enum { X=100, Y=50, Z=200};

❑ C++ is very strict with regard to type compatibility as compared


to C
Declaration of variables:
~ C++ allows declaration of variables anywhere in the scope
~ the only disadvantage is that we cannot see all the variables used
in a scope at a glance

Dynamic Initialisation of variables:


~ C++ permits initialisation of variables at run-time, this is referred to
as dynamic initialisation.
~ i.e. both declaration and initialisation can be done simultaneously
at the place where the variable is first used
Reference Variables
 A reference variable provides an alias for a previously defined
variable.
Syntax: data_type &refernce_name = variable_name
e.g. float total = 100;
float &sum = total;

A reference variable must be initialised at the time of declaration


. This establishes the correspondence between reference and
data object which it names.

❖ Here ‘&’ is not an address operator. The notation ‘float &’


means reference to float.
 The major application of reference variable is on passing arguments
to functions.
e.g. void f(int &x)
{
x = x+10;
}
int main()
{
int m=10;
f(m);
}
When the function call f(m) is executed, following initialisation occurs:
int &m = x;
Such function calls are known as ‘call by reference’.
Scope Resolution Operator:
 Used to uncover a hidden variable
syntax ~ :: variable_name

 This operator allows access to global version of a variable


:: m will always refer to global m

Member Dereferencing Operator:


 Operators which permit us to access class members through pointers ~
i. ::* - to declare a pointer to a member of class
ii. * - to access a member using object name and a pointer to that
member
iii. ->* - to access a member using a pointer to the object and a
pointer to that member
Memory Management Operators:
 We use dynamic allocation techniques when it is not known in
advance how much of memory space is needed
 Two unary operators ‘new’ and ‘delete’ perform the task of
allocating and freeing the memory
➢ The new operator can be used to create object of any type
syntax~ pointer_variable = new data_type;
e.g. p = new int;
• Here p holds the address of memory space allocated and p is a
pointer of type int.
• Also, the memory can be initialised as: int *p = new int(25);
 Similarly, a class object can be created as:
class_name *ptr = new class_name;
 When a data object is no longer needed, it can be destroyed
using delete. Hence, the object will remain in existence until it is
explicitly destroyed using delete. Thus the lifetime of an object is
directly under our control.
 Syntax to use delete:
delete pointer_variable;
 The pointer variable is the pointer that points to a data object
created using new.
 bad_alloc is used instead of null pointer for handling allocation
failure
Manipulators: Used to format data display

endl setw fixed and


• Causes a linefeed to be • Used to right justify the setprecison()
inserted number • We may control the
• e.g. : • Specifies a field width precision of floating
cout << “m= “ << m << • e.g. : point members by using
endl << “n= ” << n << cout << setw(5) << sum ‘fixed’ and
endl; << endl; ‘setprecision()’
• Character strings are manipulators
also printed right justified • e.g.
cout << fixed
<<setprecision(2) <<
average;
• Type Cast Operator:
➢ explicit type conversion of variable or expressions
➢ Syntax: type_name(expression)
➢ e.g. average = sum/float(i);

• Implicit Conversions:
➢ Whenever data types are mixed in an expression, C++ performs the
conversion automatically. This process is known as implicit or
automatic conversion.

• Operators that can’t be overloaded:


1. Member-access operators ( . and .*)
2. Conditional Operator (?:)
3. Scope Resolution Operator (::)
4. Sizeof operator (sizeof)
Source:
Object Oriented Programming with C++ by E. Balagurusamy

Nandini Sidana

You might also like