0% found this document useful (0 votes)
12 views25 pages

CST 2nd Year OOP E-Contents - Unit 2 - Classes & Objects

Object Oriented Programming Chapter: Objects & Classes (Unit: 2)

Uploaded by

halumsona
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)
12 views25 pages

CST 2nd Year OOP E-Contents - Unit 2 - Classes & Objects

Object Oriented Programming Chapter: Objects & Classes (Unit: 2)

Uploaded by

halumsona
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/ 25

K. G.

Engineering Institute
Bishnupur – Bankura – 722122
Department of Computer Science & Technology
2nd Year – 4th Semester
e-Contents : Object Oriented Programming
Chapter : Objects & Classes (Unit : 2)

Developed By :
Raj Kumar Datta (Lecturer & HOD)
Computer Science & Technology
KGEI, Bishnupur, Bankura-
Bankura- 722122
1
Contents 2 Objects & Classes
2.1 Specifying a Class
Designing a member Functions
Arrays within a Class
Creating Objects
Memory Allocation for Objects
Static Data & Member Function
Array of Objects
Objects as function arguments
Returning objects from member function
Friend Function

2.2 Class Specifier and their Uses


Distinction between structure of C and Class
Object Oriented Programming : Objects & Classes –
2
KGEI – Bishnupur - Bankura
2.1 Specifying Class
Object :
- Basic runtime real life entities.
- An Instance or variable of a Class.
- contain data and code to manipulate the data.
Class :
- An user-defined datatype.
- A collection of objects of similar type.
- Encapsulating data and functions into a single unit.
- Another term, a way to bind data and its associated functions together.
Object Oriented Programming : Objects & Classes –
3
KGEI – Bishnupur - Bankura
2.1 Specifying Class
Specifying a Class: Example of Class Declaration:
- Class Declaration
- Class Function Definition
class Item{
General Form of Class Declaration: private:
class <class_name>{ int number; // member variable
private: float cost; // declarations
variable declarations; public:
function declarations; void getData(int a, float b);
public: void putData();
variable declarations; // function prototype declaration
function declarations;
}; }; // end with semicolon
Object Oriented Programming : Objects & Classes –
4
KGEI – Bishnupur - Bankura
2.1 Specifying Class
Creating an Object :
Syntax : class_name object_name;
Example : Item i1, i2, i3;

Accessing the Class member:


Syntax : object_name.function_name(actual_argument(s));
Example : i1.getData(10, 99.99);
i1.putData();

Public member can be accessed with the help of an object (i.e. i1 or i2 etc.)

Private data member of a class can be accessed through the member function of
that class.
Object Oriented Programming : Objects & Classes –
5
KGEI – Bishnupur - Bankura
2.1 Defining member function
Member functions can be defined in two places:
- Inside the class definition - Outside the class definition.
Inside the class definition:
class Item{
private:
int number; // member variable declarations;
float cost;
public:
void getData(int a, float b) { number = a; cost = b;}
void putData(){
cout<< number << endl << cost << endl;
}
}; // end with semicolon
Object Oriented Programming : Objects & Classes –
6
KGEI – Bishnupur - Bankura
2.1 Defining member function : (Inside the Class)

Object Oriented Programming : Objects & Classes –


7
KGEI – Bishnupur - Bankura
2.1 Defining member function
Member functions can be defined in two places:
- Inside the class definition - Outside the class definition.

Outside the class definition:

Syntax :
return_type class_name :: function_name(argument(s)){
// function body
}

Example :
void Item :: getData(int a, float b){
number = a;
cost = b;
}

Object Oriented Programming : Objects & Classes –


8
KGEI – Bishnupur - Bankura
2.1 Defining member function : (Outside the Class)

Object Oriented Programming : Objects & Classes –


9
KGEI – Bishnupur - Bankura
2.1 Defining member function : (Nesting Member Function)

Object Oriented Programming : Objects & Classes –


10
KGEI – Bishnupur - Bankura
2.1 Defining member function : (Private Member Access)
A private member variable or function
can only be accessed by another
function that is a member of its class.

Declaring an object can not invoke a


private member using dot operator.

Object Oriented Programming : Objects & Classes –


11
KGEI – Bishnupur - Bankura
2.1 Arrays with in a class
- The Arrays can be used as a member
variable for a class.
- Array can be declared as public or private
member of its class.
- Public data member can be accessed either
from outside the class with the help of an
object or from inside the class by accessing
public member function of its class.
- Private data member can be accessed by
the public member function of its class.

Object Oriented Programming : Objects & Classes –


12
KGEI – Bishnupur - Bankura
2.1 Memory Allocation for Object
- Data member of the class can contain different value
for the different object, memory allocation is
performed separately for each data member for
different object at the time of creating an object.

- Member function remains common for all object.


Memory allocation is done only once for member
function at the time of defining it.

Object Oriented Programming : Objects & Classes –


13
KGEI – Bishnupur - Bankura
2.1 Static Data Member
In Object Oriented Programming :
Memory is allocated separately for each data member for different objects. Changing the
value of data member of class using one object will not affect
for other object of the same class. Static data member is used for sharing the
common variables for all the objects of the same class.

Static Data Member Characteristics :

- static keyword is used.


- initialized to zero when the first object of its class is created.
- Only one copy of that member is created for the entire class and is shared by all the objects of that
class.
- It is visible with in the class and its lifetime is throughout the program.

Object Oriented Programming : Objects & Classes –


14
KGEI – Bishnupur - Bankura
2.1 Static Data Member (Example)
Note:
Static data member is declared in the
class but it must be defined outside the
class using class_name and scope
resolution operator(::) because memory
allocation for static data member of the
class is performed different than normal
data member of the class and it is not
the part of class object.

Syntax :
datatype class_name :: variable;

Example :
int item:: count; // count = 0

Static variable count will be initialized as


zero when the object is created.

Object Oriented Programming : Objects & Classes –


15
KGEI – Bishnupur - Bankura
2.1 Static Member Function (Example)
Member function of the class can also be
declared as static.

Characteristics of Static Member Function:


- Static member function can access
static member of the class in which it is
declared.

- Static member function is not a part of


the class object, so it can not be called or
invoked using object of the class.

Note:
Static member function can be called using
class name and scope resolution operator
as shown below:

CLASS_NAME :: FUNCTION_NAME();

Object Oriented Programming : Objects & Classes –


16
KGEI – Bishnupur - Bankura
2.1 Array of Objects
Array of Objects :

- Like array of other user-defined data types, an array of type class can also be created.

- The array of type class contains the objects of the class as its individual elements.

- Thus, an array of a class type is also known as an array of objects.

- An array of objects is declared in the same way as an array of any built-in data type.

Syntax :
class_name array_name [size];

Example:
student std[MAX]; // MAX is integer constant

Object Oriented Programming : Objects & Classes –


17
KGEI – Bishnupur - Bankura
2.1 Array of Objects(Example)

Object Oriented Programming : Objects & Classes –


18
KGEI – Bishnupur - Bankura
2.1 Passing Object as Function Argument
Like any other data type, an object may be used as an function argument. This can be done in two
ways:

 A copy of the entire object is passed to the function

 Only the address of the object is transferred to the function

The first method is called pass-by-value. Since a copy of the object is passed to the function.
Any changes made to the object inside the function do not affect the object to call the function.

The second method is called pass-by-reference. When an address of the object is passed, the called
function works directly on the actual object used in the call, means that any changes made to the
object inside the function will reflect. It is better to pass the address of the object in spite of
passing the entire object.

Object Oriented Programming : Objects & Classes –


19
KGEI – Bishnupur - Bankura
2.1 Passing Object as Function Argument (Example)

Object Oriented Programming : Objects & Classes –


20
KGEI – Bishnupur - Bankura
2.1 Returning Objects (Example)
A function can not only receive
objects as arguments but also
return them from where it is
called.

The example here showing how


an object can be passed to a
member function and it will
return that from that function.

Object Oriented Programming : Objects & Classes –


21
KGEI – Bishnupur - Bankura
2.1 Friend Function (Definition)
The function that are declared with the keyword friend that are known as friend function. So, the function
declaration must be preceded by the keyword “friend”. The function definition does not use either the keyword
friend or scope resolution operator(::). A function can be declared as friend for any number of classes. A
function can be treated as friend function when that is not a member of a class but that can access private and
protected member of that class in which it is declared as friend. Sometime it is required that private member of
the class can be accessed outside the class as the time of using friend function.
Syntax : friend return_type function_name(argument(s));
Example : friend void myFunction(void);

Characteristics of Friend Function:


- Not in the scope of the class to which it has been declared as friend.
- Can not be called using the object of that class as it is not in the scope of the class.
- Can be invoked like a normal function with out creating any object.
- Can not access the member names directly and has to use an object and dot membership operator with
each member to access i.e. Object_name.memeber_variable.
- Declaration may be done either in the public or private part of the class.
- Usually, Objects is passes as arguments.

Object Oriented Programming : Objects & Classes –


22
KGEI – Bishnupur - Bankura
2.1 Friend Function (Example)

Object Oriented Programming : Objects & Classes –


23
KGEI – Bishnupur - Bankura
2.2 Class Specifier and their Uses
 C++ access specifiers are used for determining the boundary for the availability of class members
i.e. data members and member functions of that class.

 C++ access specifiers are private protected and public. These keywords are called access
specifiers which define the accessibility or visibility level of class members.

 By default the class members are private i.e. if the visibility modes are not mentioned explicitly,
then by default all the class members become private.

In C++, there are three access specifiers:

public - members are accessible from outside the class as well as from that class.
private - members cannot be accessed from outside the class and are accessible from inside
class itself where it is declared.
protected - members cannot be accessed from outside the class, however, they can be accessed
in inherited classes i.e. from derived classes.
Object Oriented Programming : Objects & Classes –
24
KGEI – Bishnupur - Bankura
2.2 Distinction between structure of C and Class in C++

Object Oriented Programming : Objects & Classes –


25
KGEI – Bishnupur - Bankura

You might also like