0% found this document useful (0 votes)
39 views27 pages

Slide 2 - Beginning With C++

This document discusses object-oriented programming concepts in C++, including tokens, data types, type conversion, inline functions, strings, moving from C to C++, specifying classes, data hiding, accessing public data, member functions, private member functions, memory allocation, static data members, static member functions, and arrays of objects. It provides code examples to illustrate key OOP concepts in C++.

Uploaded by

NaMan SaTyavana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views27 pages

Slide 2 - Beginning With C++

This document discusses object-oriented programming concepts in C++, including tokens, data types, type conversion, inline functions, strings, moving from C to C++, specifying classes, data hiding, accessing public data, member functions, private member functions, memory allocation, static data members, static member functions, and arrays of objects. It provides code examples to illustrate key OOP concepts in C++.

Uploaded by

NaMan SaTyavana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Object Oriented Programming

(CS - 211)
Slide – 2
Beginning with C++

Prepared by –
Dr. Soumen Moulik
Department of Computer Science & Engineering
National Institute of Technology Meghalaya
Tokens
 Tokens are smallest individual units in a program
 Most of the C++ tokens are basically similar to the C tokens
with the exception of some additions and minor modifications

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Tokens
 Keywords –
◦ Reserved words that cannot be used as identifiers / variable names
◦ 32 keywords from ANSI C + Additional keywords

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Tokens
 Identifiers –
◦ Refer to the names of variables, functions, arrays, classes etc.
◦ 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 variable name
◦ C++ allows unlimited length for variable name
 ANSI C recognizes only 32 characters in a name
◦ Example – abc, my_name, a_123, retVal, _temp
 Constants –
◦ Refer to fixed values that do not change during the execution
◦ Example – 123 (decimal), 12.34 (float), “C++” (string), „A‟ (character),
037 (octal), 0X2 (hexadecimal)

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Data Types
C++ Data
Types

User-
Built-in Derived
defined

structure Integral Void Floating array

union int float function

class char double pointer

enum reference
SIZE
Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya
Type Conversion
 Waterfall model of implicit type conversion

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Implicit Type Conversion
 Implicit –
◦ When compiler encounters an expression, it divides it into sub-
expressions consisting of one or two operands
◦ In case of a binary operator, the „smaller‟ type is converted to the
„wider‟ type
◦ If one operand is an int and the other is a float, then the int is
converted into a float because a float is wider than int
◦ Integral widening conversion
◦ EXAMPLE

 Explicit – (type) expression; (type) variable_name


◦ EXAMPLE

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Inline Functions
 When a program executes general function call instructions –
◦ CPU stores the memory address of the instruction following the function call
◦ Copies the arguments of the function on the stack
◦ Then transfers control to the specified function
◦ Executes the function code
◦ Stores the function return value in a predefined memory location
◦ Then returns control to the calling function
 This is overhead if the execution time of function is less than the
switching time from the caller function to the called function
 C++ provides the feature of inline function to reduce this overhead
◦ Inline functions are expanded in line, i.e., gets inserted or substituted, at the time
of inline function call EXAMPLE
◦ This is only a request to the compiler, not a command
◦ Compiler may not perform inlining if a function contains loop, static variables,
recursion, switch or goto statements, or returns something other than void

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


String Class
 length() / size()  no. of characters in a string, including
white space
 at(index)  character at a particular index
 append(str)  adds the argument string at the end
 replace(a, b, str)  replaces b characters from index a by str
 erase(a, b)  deletes b characters at index a
 find(str)  returns index where pattern is found. If pattern is
not found then returns predefined constant npos (= -1)
 substr(a, b)  returns a substring of b length starting from
index a

EXAMPLE

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Moving from C to C++
 Scope resolution operator  :: EXAMPLE 1 EXAMPLE 2
 Variable definition at any point of use
 Reference variable  enjoys the simplicity of value variable and
power of the pointer variable EXAMPLE 1 EXAMPLE 2
 Function overloading EXAMPLE
 Default arguments are allowed EXAMPLE
 Functions are allowed as a part of a structure EXAMPLE
 Argument passing by reference is allowed
 Dynamic / Run-time memory management EXAMPLE
 Some C programs that won‟t compile in C++
[https://www.geeksforgeeks.org/write-c-program-wont-compiler-c/]

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Specifying a Class
 A class is a way to bind the data and its associated functions
 It allows the data (and the functions) to be hidden
 When defining a class, we are creating a new abstract data type
 Class specification: 1) declaration and 2) function definitions
 General form of class declaration is:

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Data Hiding
 Class members that have been declared as private cannot be
accessed from outside the class
 Only the member functions can have access to the private
data members and private functions
 Public members can be accessed from outside the class
 By default, the class members are private
 If both the labels are missing, then, by default, all the
members are private, and such a class is completely hidden
from the outside world and does not serve any purpose
 The binding of data and function together into a single class-
type variable is referred to as encapsulation

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Data Hiding

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Accessing Public Data

 A variable declared as
public can be accessed by
the objects directly.

EXAMPLE

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Member Functions
 Member functions that are declared inside a class have to be
defined separately outside the class
 Several different classes can use the same function name. The
„membership label‟ will resolve their scope
 Member functions can access the private data of the class. A non-
member function cannot do so
◦ Exception – friend function
 A member function can call another member function directly,
without using dot operator
 We can define a member function outside the class definition and
still make it inline by just using the qualifier inline
◦ inline void item :: getdata (int a, float b) { … }
 A member function can be called by using its name inside another
member function of the same class

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Private Member Function

 The function read() can


be called by the function
update() to update the
value of m

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Memory Allocation
Since all objects belonging to
that class use the same
member functions, no
separate space is allocated for
them when objects are
created

Separate memory locations for the objects are essential,


as the member variables will hold different data values
for different objects
Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya
Static Data Members
 Static data member is initialized to zero when the first object
of its class is crated.
 No other initialization is permitted
 Only one copy of that member is created for the entire class
and is shared by all the objects of that class, no matter how
many objects are created
 Normally used to maintain values common to the entire class.
Ex – counter, that records the occurrences of all objects
 Type and scope of each static variable must be defined
outside the class definition
◦ Necessary because the static data members are stored separately rather
than as a part of an object
EXAMPLE

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Static Member Functions
 A static function can have access to only other static
members (functions or variables) declared in the same class
 A static member function can be called using the class name
(instead of its objects) as follows:
◦ class-name :: function-name;

EXAMPLE

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Arrays of Objects
 Arrays of variables that are of the type class

EXAMPLE

 The array manager contains three objects (managers), namely


manager[0], manager[1] and manager [2] of type employee
 manager[i].putdata() will display the data of the i-th element
of the array manager

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Friendly Functions
 A non-member function cannot have an access to the private data of a class
 However, there could be a situation where we would like two classes to
share a particular function
 The function declaration should be preceded by the keyword friend
 The function definition does not use either the keyword friend or the
scope operator ::
 It accesses class variables by using dot operator and the objects passed to
it as arguments
 Since it is not in the scope of the class – to which it has been declared as
friend, it cannot be called using the object of that class
 A function can be declared as a friend in any number of classes
EXAMPLE 1 EXAMPLE 2

Note: A function also can return objects EXAMPLE

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Pointers to Members
 Address of a member can be obtained by applying the
operator „&‟ to a “fully qualified” class member name
 A class member pointer can be decaled using the operator
„::*‟ with the class name

Pointer to the member m

EXAMPLE

 A::* means “pointer-to-member” of A class


 &A::m means “address of the m member class A”

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Local Classes
 Classes can be defined and used inside a function or a block.
Such classes are called local classes

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Constructors and Destructors
 Motivation –
◦ In general, there is no provision to initialize data members.
◦ We need to pass the initial values as arguments to some function,
where these values are assigned to the private variables
◦ All these functions are called with the appropriate objects that
have already been created
◦ These functions cannot be used to initialize the member variables
at the time of creation of their objects
◦ When a variable of built-in type goes out of scope, the compiler
automatically destroys the variable. However, in case of objects it
is not applicable
◦ How to destroy the objects when they are no longer required?

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Constructors
 Special member function – to initialize the objects of its class.
 Special because its name is the same as the class name
 It is invoked whenever an object of its associated class is created
 It is called constructor because it constructs the values of data
members of the class
 Should be declared in the public section EXAMPLE
 Invoked automatically when the objects are created
 Do not have return types
 Default constructors – accepts no parameters
 Parameterized constructors – accepts arguments
 Constructor functions can also be defined as inline functions

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Constructors
 Cannot be inherited, though a derived class can call base class
constructor
 We cannot refer to their addresses
 Multiple constructors or overloaded constructors are allowed
EXAMPLE
 Constructors with default arguments allowed
 Objects can be initialized dynamically through constructors
 Copy constructor is used to declare and initialize an object from
another object EXAMPLE
 Constructors can also be used to allocate memory while creating
objects EXAMPLE
◦ Enable the system to allocate the right amount of memory for each object when
the objects are not of the same size, thus resulting in saving memory

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya


Destructors
 Used to destroy the objects that have been created by a constructor
 Destructor name is same as the class name, but preceded by a tilde
 A destructor never takes any argument nor does it return any value
 It will be invoked implicitly by the compiler upon exit from the
program (or block or function)
EXAMPLE
 Purpose is to clean up storage that is no longer accessible
 When pointers to objects go out of scope, destructor is not called
implicitly.
◦ We need to use delete operator in such cases

Dr. Soumen Moulik, Dept. of CSE, NIT Meghalaya

You might also like