Unit3 Classes and Objects
Presented By : Mukeswar Shah
Introduction
• A class is a blueprint(model) for the object. Object is an instance
(example/variable)of a class
– i.e., It is a template from which objects are created.
– E.g., Sketch of a house and house.
• Once a class has been defined , we can create any number of
objects of the same class.
• So, we can say that class is a user defied data type.
Mukeswar Shah 2
Contd..
• Defining (Specifying) a class:
– A class is defined in C++ using keyword class followed by the name of
class(a valid identifier).
– The body of class is defined inside the curly brackets and terminated by a
semicolon at the end.
Mukeswar Shah 3
Contd..
• General format of defining class:
Mukeswar Shah 4
Contd..
• The body of a class encapsulates data and functions called the
class members.
• The data components of the class are called data members and
the function components are called member functions.
Mukeswar Shah 5
Contd..
• Private, Protected, Public is called visibility labels/access
specifiers.
• In C++, data can be hidden by making it private.
• The members that are declared private can be accessed only from
within the class.
• When access specifiers are not specified, members will be
private by default.
• Public members can be accessed from outside the class also.
• Protected members can be accessed from the child class also.
• Usually within a class data are private and functions are public
Mukeswar Shah 6
Contd..
• The access specifiers can occur more than once in the class
definition:
• E.g.,:
Mukeswar Shah 7
Contd..
• Example: Declaring a class rectangle
Mukeswar Shah 8
Contd..
• Creating objects:
– The class declaration does not define any objects but only specifies what
they will contain.
– Once class has been declared, we can create variables(objects) of that type
as
– Syntax: class_name object_name; // instantiating object
• E.g.,: rectangle r; // it creates a variable(object) r of type rectangle.
– We can create any number of objects form the same class
• E.g.,: rectangle r1, r2,….;
Mukeswar Shah 9
Contd..
• Alternative way to create objects:
– Objects can also be created when a class defined by placing their names
immediately after the closing brace.
– E.g..,:
– Note: size of the object is the sum of the size of the all the data members
declared in the class.
Mukeswar Shah 10
Contd..
• Accessing class members:
– When an objects of the class is created then the members are
accessed using the ‘.’ dot operator(also called member access
operator) except the private members.
– Syntax: object_name.data_member_name;
object_name.function_member_name(argements….);
– For example: r.setData(4, 2);
Mukeswar Shah 11
Example: Program to find the area and perimeter of rectangle
Mukeswar Shah 12
Contd..
• Output:
Mukeswar Shah 13
Contd..
• Example2: simple program to set and show the data by using the concept of class
Mukeswar Shah 14
Contd..
• Output:
Mukeswar Shah 15
Contd..
• Example3: Program to demonstrate the class and object
Mukeswar Shah 16
Contd..
• Output:
Mukeswar Shah 17
Contd..
• Class work1: Write a program designing a class to represent
Item information. Include the following members
– Data members
• Itemid
• cost
– Member functions
• Setdata
• showdata
Mukeswar Shah 18
Contd..
• Program:
Mukeswar Shah 19
Contd..
• Class work2: Write a program designing a class to represent
Item information. Include the following members
– Data members
• Itemid
• cost
– Member functions
• showdata
Mukeswar Shah 20
Contd..
• Program:
Mukeswar Shah 21
Defining Member function
• The data member of a class is declared within the body of class.
However, the member functions can be defined in one of the two
places:
– Inside the class defining: Definition and declaration is placed in the same
place i.e., inside the class definition.
– Outside the class definition:
• Declaration is placed inside the class definition and
• But the definition is provided outside of the class by using scope resolution operator as:
Mukeswar Shah 22
Contd..
– Defining member function Outside the class definition
• Declaration is placed inside the class definition and
• But the definition is provided outside of the class by using scope
resolution operator as:
• Syntax:
Return_type class_name::function_name(argument declaration)
{
//Function body
Mukeswar Shah 23
• Example:
Contd..
Mukeswar Shah 24
Contd..
• Output:
Mukeswar Shah 25
Making outer function inline
• In C++, all the member functions defined inside the class are
treated as inline where as the function defined outside the class
are not inline.
• The member function defined outside the class can be made
inline too. It is made inline by prefixing the keyword inline to the
function definition as follows:
Mukeswar Shah 26
Contd..
• Syntax:
Mukeswar Shah 27
Nesting of member Functions
• A member function can be called by using its name from another
member function of the same class. This is known as nesting of
member functions.
Mukeswar Shah 28
• Example:
Contd..
Mukeswar Shah 29
Contd..
• Output:
Mukeswar Shah 30
Memory Allocation For Objects
• For each object, the memory space for data members is allocated
separately because the data members will hold different data values
for different objects.
• However, all the objects in a given class use the same member
functions.
• Hence, the member functions are created and placed in memory only
once when they are defined as a part of class specification and no
separate space is allocated for member functions when objects are
created.
Mukeswar Shah 31
Contd..
Mukeswar Shah 32
Contd..
• Class work: Write a program designing a class to represent student.
Include the following members
– Data members
• Roll
• Name
• phone
– Member functions
• Getdata
• showdata
• Define member function inside class
• Define member function outside class
• Make the functions inline also
Mukeswar Shah 33
Example: Contd..
Mukeswar Shah 34
Contd..
• Output:
Mukeswar Shah 35
• Example:
Contd..
Mukeswar Shah 36
Contd..
• Output:
Mukeswar Shah 37
Constructors and Destructors
• When an object is created all the members of the object are
allocated memory spaces.
• Each objects has its individual copy of member variables.
• However the data members(member variables) are not initialized
automatically.
Mukeswar Shah 38
Contd..
• For initialization of data members we need to make member
functions like init( ) as follows:
• Problem: If the programmer forgets to initialize the variable by
calling the function init(), the program will produce unusual
result.
• Solution: Constructor!
Mukeswar Shah 39
Contd..
• What is constructor?
– A constructor is a special member function that is executed
automatically whenever an object is created.
– It is used for automatic initialization.
• Automatic initialization is the process of initializing object’s data
members when it is first created, without making a separate call to a
member function.
Mukeswar Shah 40
Contd..
• Some characteristics of constructors are:
– The name of the constructor is same as the class name.
– Constructors should be defined or declared in the public section
– They do not have return types.
– Like functions they can have default arguments.
Mukeswar Shah 41
Contd..
• Syntax:
• Example:
Mukeswar Shah 42
Types of constructor
• Default constructor
• Parameterized constructor
• Copy constructor
• Default copy constructor
Mukeswar Shah 43
Contd..
• Default Constructor:
– A constructor that takes no arguments is called default
constructor.
• E.g.,:
Mukeswar Shah 44
Contd..
• A default constructor is called automatically at the time of
object creation (if no arguments are supplied) and does
nothing more than initializing the data variables of the object
to valid initial values.
• Syntax: class_name object_name; // default constructor is invoked
• E.g.,: rectangle r1; // default constructor rectangle of the class
rectangle is invoked.
• Note: Compiler generates the default constructor if none of the
constructors are defined.
Mukeswar Shah 45
Contd..
• Parameterized constructors:
– The constructor that can take arguments are called
parameterized constructors.
– E.g.,:
Mukeswar Shah 46
Contd..
• Parameterized constructors are used if it is necessary to initialize
the various data elements of different objects with different
values when they are created.
• This can be achieved by passing arguments to the constructor
when the objects are created.
• Two ways:
– By calling the constructor explicitly: e.g., rectangle r1 = rectangle(5,6)
– By calling the constructor implicitly: e.g., rectangle r1(5, 6);
Mukeswar Shah 47
Contd..
• Copy constructors:
– Copy constructor allows an object to be initialized with another
object of the same class.
– It implies that the values stored in data members of an existing
object can be copied into the data members of the object being
constructed, provided the objects belong to the same class.
– A copy constructor has a single parameter of reference type that
refers to the class itself as shown below:
Mukeswar Shah 48
Contd..
• E.g.,:
• Once a copy constructor is defined, we can use copy constructor as :
rectangle r2(r1) ;
• It creates new object r2 and perform member-by-member copy of r1
into r2 .
Mukeswar Shah 49
Contd..
• Default copy constructor:
– If you don’t define copy constructor, the compiler creates a default copy
constructor for each class which does a member-wise copy between
objects.
– But it will do shallow copy, if class contains pointers than default copy
constructor can not copy value pointed by pointer variable of one object
into pointer variable of another object rather both of them will point to the
same place.
– To handle this situation we need to create copy constructor.
– Default copy constructor can be invoked as below:
rectangle r2 = r1;
It creates new object r2 and perform member-by-member copy of r1 into r2.
Mukeswar Shah 50
• Example: Contd..
Mukeswar Shah 51
Contd..
Mukeswar Shah 52
Contd..
• Output:
Mukeswar Shah 53
Contd..
• Constructor overloading:
– A class can have multiple constructors.
– If more than one constructor is used in a class, it is known as
constructor overloading.
– Constructors can be overloaded either by:
• different number of argument or
• with different type of argument
Mukeswar Shah 54
Contd..
• Example:
Mukeswar Shah 55
Contd..
Mukeswar Shah 56
Contd..
• Output:
Mukeswar Shah 57
Destructors
• A destructor is a special member function that is executed
automatically just before lifetime of an object is finished.
• The most common use of destructor is to destroy the memory that was
allocated for the object by the constructor.
• In many cases program do not need user supplied destructors. In that
case compiler supplies a default destructor.
• However, if program creates dynamic objects, then destructors should
be defined explicitly in the program by the programmer
Mukeswar Shah 58
Contd..
• Destructors have the following characteristics:
– A Destructor has the same name as the constructor(which is the same as
the class name) but is preceded by a tilde(~).
– Like constructors, destructors do not have a return type.
– Destructors takes no arguments. Hence we can use only one destructor in a
class(i.e., we can not overload destructors).
– Objects are destroyed in the reverse order of their creation.
– Syntax:
Mukeswar Shah 59
• Example: Contd..
Mukeswar Shah 60
Contd..
• Output:
Mukeswar Shah 61
Static data members
• Static data members declared inside class by using static
keyword and must be defined outside the class using scope
resolution operator.
• Syntax:
• Only one copy of static member is created for the entire class and
is shared by all the objects of that class.
• Used to maintain values common to entire class .
Mukeswar Shah 62
Contd..
• Example:
Mukeswar Shah 63
Contd..
Mukeswar Shah 64
Contd..
• Output:
Mukeswar Shah 65
Static member functions
• Static member function can be defined by using static keyword
and called by using scope resolution operator.
• Syntax:
Mukeswar Shah 66
Contd..
• Features:
– A static member function can be called even if no objects of the class exist .
– The static functions are accessed using the class name and the scope
resolution operator ::.
– A static member function can only access static data member, and other
static member functions.
– A static member function cannot access ordinary data members and member
functions
Mukeswar Shah 67
Contd..
• Example:
Mukeswar Shah 68
Contd..
Mukeswar Shah 69
Contd..
• Output:
Mukeswar Shah 70
Objects as a function arguments
• Like any other variables, an object may be used as a function
argument in three ways:
– Pass- by-value
– Pass-by-reference, and
– Pass-by-pointer
Mukeswar Shah 71
Contd..
• Pass by value:
– In pass by value a copy of the object is passed to the function. so, the
changes made to the object inside the function do not affect the actual
object.
– since all values of objects need to be copied into arguments of method
invoked, it makes program slower when larger objects are used.
– An object can be passed by value to the function as below:
Mukeswar Shah 72
Contd..
• Example:
Mukeswar Shah 73
Contd..
Mukeswar Shah 74
Contd..
• Pass-by-reference:
– In pass by reference, an address of the object is passed to the
function, it makes program faster when using larger objects.
– The function works directly on the actual object used in the
function call.
– So any changes made to object inside the function will reflect
in the actual object.
Mukeswar Shah 75
Contd..
• Example:
Mukeswar Shah 76
Contd..
Mukeswar Shah 77
Contd..
• Pass-by-pointer:
– Also works directly on the actual object used in the function call.
Mukeswar Shah 78
Contd..
Mukeswar Shah 79
Returning objects from the functions
• A function not only receives objects as arguments but also can
return them.
• A function can return objects also in three ways:
– Return-by- value
– Return-by- reference, and
– Return-by-pointer.
Mukeswar Shah 80
• Return-by-value: A copy ofContd..
the object is returned to the function call.
Mukeswar Shah 81
Contd..
Mukeswar Shah 82
Contd..
• Return-by-reference: An address of the object is returned to the function call.
Mukeswar Shah 83
Contd..
Mukeswar Shah 84
Contd..
• Return-by-pointer: It also returns address of the object to the function call.
Mukeswar Shah 85
Contd..
Mukeswar Shah 86
Home Work
• What do you mean by object and class? List the differences between
structure and class.
• How data hiding can be achieved in class?
• What are the different ways of defining member functions? Which one
of them is better way and why?
• Write a program to read and display 10 objects of item class
containing data member item, name ,code and price.
• Describe the different methods of returning objects from function with
their merits and demerits.
• Write a program designing a class to represent a bank account. Include
the following members
– Data members: name of depositor, account number, type of account
– Member functions: To assign initial values, To display name and account type
Mukeswar Shah 87
Home Work
• Write a meaningful program to illustrate the use of static data member
and static function members.
• Construct a class named intamount with the following members to
calculate interest and amount separately on the basis of given principle
, rate and time.
– Data members: principle, time, rate
– Function members: getdata, interest, amount.
• Define a class circle with the following data members and member
functions:
– Data members: radius and area
– Member functions: getdata, calcArea and display.
Mukeswar Shah 88
Thank You !
Mukeswar Shah 89