0% found this document useful (0 votes)
8 views

3.objects and Class

Object and class in c++

Uploaded by

Sailesh Sailesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

3.objects and Class

Object and class in c++

Uploaded by

Sailesh Sailesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 101

Unit-3

Object and Class

SUSHANT BHATTARAI
Object and Class 2

 An object has the same relationship to a class


that a variable has to a datatype.
 Remember Narayan Gopal and Bipul Chettri?
 An object is said to be an instance of a class.
Class syntax (similarities with
structures)

SUSHANT BHATTARAI
A simple class 4
A simple class 5

 Above class smallobj has one data and two


functions.
 The first one sets the data item to a value, and the
second one displays the value.
Defining the Class 6

 Starts with the keyword class followed by the class


name.
class class_name
{

};
Don’t forget the semicolon;
Data Encapsulation (public,
private)

SUSHANT BHATTARAI
private and public 8

 Feature of OOP is data hiding.


 Data hiding means that data is concealed within
a class so that it cannot be accessed mistakenly
by functions outside the class.
private and public 9

 Private data or functions can only be accessed


from within the class.
 Public data or functions, on the other hand, are
accessible from outside the class.
 These are used to hide data from parts of
program that don’t need to access it.
Object and the member
access

SUSHANT BHATTARAI
Defining objects 11

 Definition of the class doesn’t create objects.


 It only describes how it looks when they are
created.
 Defining objects is similar to defining a
variable .
 An object is an instance of a class.
Calling Member Functions 12

 We must use objects to call the member


functions.
 A member function is always called to act on
a specific object , not on the class in general.
 .(dot) operator connects the object name and the
member functions.
C++ Objects as Physical 13

Object.
 Objects in program represent physical objects:
thing that can be felt or seen.
 Some examples are provided in the following
slides.
C++ Objects as Physical 14

Object.
C++ Objects as Physical 15

Object.
C++ Objects as Data 16

Types
C++ Objects as Data 17

Types
C++ Objects as Data 18

Types
4.4 Defining member function
(inside and outside of the class)

SUSHANT BHATTARAI
Member function within 20

class
Member function within 21

class
 Above functions are member functions within
class.
 They are definitions in the sense that the actual
code for the function is contained within the class
definition.
 This kind of functions are inline by default.
Member Functions Defined Outside the 22
Class
Member Functions Defined Outside the 23
Class
Member Functions Defined Outside the 24
Class

 We declare the function inside the class.


 But the definition is elsewhere.
 The function name is preceded by the class name
and the double colon( :: ).This is called scope
resolution operator.
 It is a way of specifying what class something is
associated with.
4.5 Constructor and Destructor

SUSHANT BHATTARAI
Constructor 26

 Constructor is a special member function that is


used to automatically initialize value as soon as
the object is created.
 It is executed automatically whenever an object
is created.
 It is always the same name as the class.
Constructor 27
Constructor 28
Constructor 29

 Single variable initialization.


class_name():variable(0)
{}
 Multiple variable initialization
class_name():var1(5),var2(7),var3(100)
{}
Constructor Example 30
Constructor Example 31
Types of Constructor 32

 Default Constructor
 Parametrized Constructor
 Copy Constructor
Default Constructor 33

 Default constructor is the constructor which


doesn't take any argument. It has no parameter.
 Syntax:
class_name()
{
body
}
Default Constructor 34
Default Constructor 35

 As soon as the object is created the constructor is


called which initializes its data members.
 A default constructor is so important for
initialization of object members, that even if we
do not define a constructor explicitly, the
compiler will provide a default constructor
implicitly.
Parameterized Constructor 36

 These are the constructors with parameter.


 Using this Constructor you can provide different
values to data members of different objects, by
passing the appropriate values as argument.
Parameterized Constructor 37
Copy Constructor 38

 It is a type of constructor that is used to create a


copy of an already existing object of a class type.
It is usually of the form X(X &) where X is the class
name.
 Syntax:
classname(classname & object_name)
{
…….
…….
}
39
Destructors 40

 Destructors are function that are called


automatically when an object is destroyed.
 A destructor has the same name as the
constructor but is preceded by a tilde(~).
Destructors 41

class something
{
private:
int data;
public:
something():data(0) //constructor
{}
~something() //destructor
{}
};
Destructors 42

 Like constructors, destructors do not have a return


value.
 They take no arguments.
 They are used to deallocate memory that was
allocated for the object.
Home work 43

 WAP to demonstrate destructor in it.


4.6 Objects as function
arguments

SUSHANT BHATTARAI
Objects as function 45

arguments
Objects as function 46

arguments
Objects as function 47

arguments
Objects as function 48

arguments
 A member function is always given access to the
object for which it was called.
 It can also access the objects supplied as
arguments.
 No result is returned. The return type is void.
 The result is automatically stored in the
designated object.
Returning objects from functions
(nameless object)

SUSHANT BHATTARAI
50
51
52
Returning objects from functions (nameless
object)
53
Returning objects from functions (nameless
object)
54
Returning objects from functions (nameless
object)
 One distance, d2 is passed to add_dist() as an
argument.
 It is added to the object d1 of which add_dist() is
a member and the result is returned from the
function and is assigned to d3
55
Returning objects from functions (nameless
object)
4.8 Array of objects

SUSHANT BHATTARAI
57
58
59
Example-2
60
Output 61
Arrays of Objects 62

 Sometimes we want to create a large number of


objects.
 Array of objects is the collection of objects in an
array.
 dist d[100] defines 100 objects that can be
referenced by d[1],d[2] etcetera.
 To access a particular member function we use
d[i].func_name.
4.10 Dynamic memory allocation for
objects and object array

SUSHANT BHATTARAI
Dynamic memory allocation for objects 64
Dynamic memory allocation for objects array 65
Dynamic memory allocation for objects 66
array
4.11 this pointer (returning object
using this pointer)

SUSHANT BHATTARAI
this pointer 68

 C++ uses keyword ‘this’ to represent an object


within a member that invokes it.
 ‘this’ keyword is a pointer that points to an object
for which the member function is called.
 ‘this’ addresses object on which the method was
called.
69
Example 1 70
Example 2 71
Example-2 72
Characteristics of this pointer 73

a) this pointer stores the address of invoking object.


b) this pointers are not accessible for static
member functions
c) this pointers are not modifiable
4.12 Static data and function
members

SUSHANT BHATTARAI
Static Data members 75

 A data member of a class can be static.


 A static member variable has the following
characteristics.
1. It is initialized to zero when the first object of it’s class is
created.
2. Only one copy of that member is created for the entire
class and it is shared by all the objects of that class,no
matter how many objects are created. It can be used
to count the number of objects of a class.
3. It is visible only within the class if declared as private, but
it’s lifetime is the entire program.
4. It is normally used to maintain values common to the
entire class.
Example 76
Example 77
Static Member Function 78

 A class may also have static function.


 We use static keyword before the member
function.
 The static functions are accessed using only class
name and scope resolution( :: ) operator.
Static Member Function 79

 Characteristics of static member functions.


a. It is independent of the class instance or objects.
b. It is invoked using ::
c. It can only access static data members.
d. It cannot have access to this pointer.
e. It cannot be declared as virtual
Example 80
Example 81
4.13 Constant data member of a
class

SUSHANT BHATTARAI
const and Classes 83

 We used const to prevent data items from being


modified and we also used it to prevent a
function from modifying a variable passed to it by
reference.
4.14 Constant member functions and
constant objects

SUSHANT BHATTARAI
const Member Functions 85

 A const member function guarantees that it will never modify any


of it’s class’s member data.
class aClass
{
private:
int alpha;
public:
void nonFunc()//non-const member function
{alpha=99;}//ok
void conFunc() const //const member function
{alpha=99;}//ERROR: can’t modify a member.
};
const Member Functions 86

 If a const Member function tries to modify


class data then a compiler error results.
 A function is made into a constant function
by placing the keyword const after the
declaration but before the function body.
const Objects 87

 We can also apply const to objects of classes.


 When an object is declared as const, you can’t
modify it.
 It means you can only use const member function
with it, because they are the only ones that
guarantee this.
88
4.15 Friend function and friend class

SUSHANT BHATTARAI
Friend Function 90

 It is a function that can access private members


of a class even though it is not a member of that
class.
 A friend function may or may not be a member of
another class.
 We use friend keyword in front of the prototype of
the function that we wish to make a friend of the
class.
Friend Function 91
Characteristics of Friend 92

Function
 The keyword friend is written only in
the function declaration of the friend
function and not in definition.
 It is not a member function and
cannot use members directly.
 You can declare a friend function in
any number of classes.
 Member function of one class can be
friend function of another class. In
such cases, friend function is declared
using scope resolution operator
Characteristics of Friend 93

Function
Class XYZ
{
int fun1();
};
Class ABC
{friend int XYZ::fun1();
};
Need of Friend Functions 94

 In C++, we know that private members cannot be


accessed from the outside class.
 Sometimes, a non member function should share
a particular private data.
Friend Class 95

 A class can also be declared as friend


of some other class.
 When a class ABC declares another
class XYZ as it’s friend, then friend class
XYZ can access to all data and
function members of the class ABC.
 In simple words, a class can be
declared as friend class of other such
that all the member functions of one
class are friend functions of the later
class.
96
97
Nested Class 98
99
10
0


10
Review Questions 1

1. What is friend function? Write a program to add private data


member of two classes using friend function?[4+6]
2. Describe parameterized constructor and explain it with
suitable example.[2+8]
3. What do you mean by constructor? Explain with a proper
example.[4+6]
4. Short notes on friend functions.[5]
5. What is friend function and friend class? Explain with
examples. [5+5]
6. What is constructor and destructor? Write a simple object
oriented program using constructor. [4+6]
7. What is object and class? Write a simple program to
implement object and class. [4+6]

You might also like