3.objects and Class
3.objects and Class
SUSHANT BHATTARAI
Object and Class 2
SUSHANT BHATTARAI
A simple class 4
A simple class 5
};
Don’t forget the semicolon;
Data Encapsulation (public,
private)
SUSHANT BHATTARAI
private and public 8
SUSHANT BHATTARAI
Defining objects 11
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
SUSHANT BHATTARAI
Constructor 26
Default Constructor
Parametrized Constructor
Copy Constructor
Default Constructor 33
class something
{
private:
int data;
public:
something():data(0) //constructor
{}
~something() //destructor
{}
};
Destructors 42
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
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
SUSHANT BHATTARAI
Static Data members 75
SUSHANT BHATTARAI
const and Classes 83
SUSHANT BHATTARAI
const Member Functions 85
SUSHANT BHATTARAI
Friend Function 90
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
10
Review Questions 1