Object Oriented Programming in C++ with Classes and Objects
Explore key OOP concepts in C++ including classes, objects, constructors, member functions, static members, friend functions, and dynamic memory management for effective program development.
Object Oriented Programming in C++ with Classes and Objects
1.
Sanjivani Rural EducationSociety’s
Sanjivani College of Engineering, Kopargaon-423 603
Department of Information Technology
Prepared by
Mr. Arun S. Dalvi
Assistant Professor
Department of Information Technology
Department of Information Technology, SRES’s Sanjivani College of Engineering, Kopargaon-423 603
Object Oriented Programming
(PCIT211)
2.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
Unit-II
Classes and Object
Course Objectives : To develop object-oriented programs
using classes, objects, constructors, member functions, and
dynamic memory management.
Course Outcome(CO1) : Develop object-oriented programs
using classes, objects, constructors, member functions, and
dynamic memory management.
3.
Content
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
Class and object concepts
Access specifiers ,Member functions
Array of objects ,Objects as function arguments
Constructors and destructors
Types of constructors
Static members and static member functions
Friend functions and friend classes
This pointer
Dynamic memory allocation using new and delete
4.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
What is Class in C++?
•A class is a way to bind data and its associated function together.
•It allows the data and function to be hidden, if necessary.
5.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
Class specification has two parts:
1.Class declaration
2.Class function definition
The class declaration describes the type and scope of its
members, The class function definition describe how the class
function are implemented
6.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
Class specification has two parts:
1.Class declaration
2.Class function definition
The class declaration describes the type and scope of its
members, The class function definition describe how the class
function are implemented
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
•The keyword class specifies, that what follows is an abstract
data of type class_name
•Body of a class is enclosed within braces and terminated by
semicolon.
•Class body contains declaration of variables and functions
•These variabes are collectively called as class members
•The keyword PUBLIC and PRIVATE are known as visibility
labels
•If we don’t specify this label by default they are PRIVATE.
Creating Object
DEPARTMENT OFINFORMATION TECHNOLOGY, SCOE,KOPARGAON
Syntax
Class_name object_name;
Example:
item x;
OR
item x,y,z //multiple object
We can also create object after class declaration as we do for
structures(i.e declaring after semicolon)
12.
Defining member function
DEPARTMENTOF INFORMATION TECHNOLOGY, SCOE,KOPARGAON
Member function can be defined in two places”
1.Outside the class definition
2.Inside the class definition
The function should perform the same task irrespective of the
place of definition.
13.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
Outside definition:
•An important difference between a member function and
normal function is that a member function incorporates a
membership ‘identity label’ in the header i.e class_name::
•This ‘label’ tells the compiler which class the function belongs
to.
Syntax:
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
The member function have special characteristics that are often
used in the program development. Characteristics are:
•Several different classes can use the same function name. the
membership label will resolve their scope.
•Member function can access private data of the class
•The member function can call another member function
directly, without using the dot operator
16.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
Inside class definition:
•Another method of defining member function is to replace the
function declaration by the actual function definition.
•When function is defined inside class is called as inline
function
•Normally small functions are defined inside a class.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
when we define member function outside the class definition
and still make it inline by just using the qualifier inline in the
header line of function definition
Making outside function inline:
19.
Memory allocation forobject
DEPARTMENT OF INFORMATION TECHNOLOGY, SCOE,KOPARGAON
• The memory space for object is allocated when they are
declared and not when the class is specified.
• This is only partly true the member funaction are created and
placed in the memory space olny once when they are defined
as a part of class specification
• Since all the objects belonging to that class use the same
member function,no separate space is allocated for
member functions when the objects are created
Array of Object
DEPARTMENTOF INFORMATION TECHNOLOGY, SCOE,KOPARGAON
• Array of a Variables that are of type class are called array of
object.
Syntax:
classname objectname[size];
Example:
employee e1[3];
22.
Memory allocation forarray of a object
DEPARTMENT OF INFORMATION TECHNOLOGY, SCOE,KOPARGAON
Name
Age
Name
Age
Name
Age
EMP[0]
EMP[1]
EMP[2]
23.
Object as functionargument
DEPARTMENT OF INFORMATION TECHNOLOGY, SCOE,KOPARGAON
Object can be used as function argument and this is done by
two ways.
1. A copy of the entire object is passed to the function.
2.Only the address of the object is transferred to the function.
24.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
1. A copy of the entire object is passed to the function
The first method is called pass-by-value
A copy of a object is passed to the function any changes
made to the object inside the function do not affect the object
used to call the function
25.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
2.Only the address of the object is transferred to the
function
The second method is called pass-by-reference.
When an address of the object is passed, the called function
directly work on actual object used in the call.
Any changes made to the object inside the function will
reflect in the actual object.
26.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
Following are the characteristics of static member
It is initialized to zero when the first object of its class is
created. No other initialization is permitted
Only one copy of that member is crated for the entire class
and is shared by all the object of that class, no matter how
many objects are created
Static data members
27.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
It is visible only within class, but its lifetime is the entire
program.
Static variables are used to maintain values common to the
entire class.
Eg. This can be used as a counter that records the occurrence's
of all the objects
Syntax:
static datatype variblename;
Eg: static int a;
28.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• Like a static member variable we can also have static
member function. A member function declared with static
keyword is called as static member function. static member
function has following characteristics:
1.Static function can have access to only other static
members/functions declared in same class
2.Static member function can be called using class
name(instead of object) as
Class_name:: function_name;
Static Member function
29.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• We have seen that private members can't be accessed from
outside class. Non member function cannot have access to
the private data of a class.
• There would be situation where we would like two classes to
share particular function.
Eg consider two classes manager and scientist.
We like to use a function income_tax() to operate on object of
both the classes.
Friend Function
30.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• C++ allows the common function to be made friendly with
the both the classes to have access to the private data of
these classes
• To make an outside function “friendly” to a class, we have
to simply declare this function as a friend function of that
class.
Syntax:
Friend returntype function_name(arguements);
Eg: Friend void add();
31.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• The function that is declared with the keyword friend is
known as friend function
• This function can be defined else where in the program.
• The function definition does not use either keyword friend or
the scope operator::
• A function can be declared as a friend in any number of
classes.
• A friend function although not a member function, has full
access right to the private members of the class
32.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
A friend function has certain special characteristics:
1.It is not in the scope of class to which it has been declared as
friend
2.Since it is not in the scope of class, it cannot be called using
object of that class
3.It can be invoked like a normal function without the help of
any object.
4.Unlike member function it can’t access member names
directly and has to use an object name and dot membership
operator with each member name
33.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• It can be declared either in the public or private part of a
class without affecting its meaning
34.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• A friend class is a class that is granted permission to access
the private and protected members of another class.
• When one class is declared as a friend of another class, all
member functions of the friend class can access the private
members of the other class.
• A friend class is a class whose member functions can
access the private and protected members of another
class.
Friend Class
35.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• Syntax:
class ClassB;
class ClassA
{
private:
int value;
public:
friend class ClassB;
};
• Example:
In a Hospital Management System, a
Doctor class may be declared as a
friend of a Patient class so that
doctors can access confidential
medical records without making
them publicly available.
36.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• In C++, every non-static member function receives a hidden
pointer called the this pointer. .
• This pointer stores the address of the object that invokes the
member function. It allows the member function to access
the correct object's data members and member functions.
• Whenever an object calls a member function, the compiler
internally passes the address of that object through the this
pointer.
This Pointer
37.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• For example,
Student s1;
s1.display();
Internally, the compiler treats it as:
display(&s1);
where this points to the address of s1.
Thus, every object uses its own this pointer while executing
member functions.
38.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• Syntax of this Pointer
The keyword this is used to refer to the current object.
Syntax:
this->dataMember
or
(*this).dataMember
Both statements are equivalent.
39.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• Example:
this->rollNo = rollNo;
Here,
• Left side refers to the object's data
member.
• Right side refers to the local
parameter.
Whenever an object invokes a member
function, the compiler internally passes
the object's address using this pointer.
Consider the following object.
Student s1;
Function call
s1.setData(101);
Internally behaves like
Object s1
│
this = Address of s1
│
Member Function Executes
40.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• In dynamic memory allocation, memory is allocated during
program execution according to the program's requirements.
• Dynamic memory allocation allows programs to allocate
memory from the heap memory whenever required and
release it when it is no longer needed.
• Example:
int *ptr = new int;.
Dynamic Memory Allocation using new and delete
41.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• The new operator allocates memory dynamically from the
heap and returns the address of the allocated memory.
Syntax:
Allocating Memory for a Single Variable
pointer = new dataType;
Example:
int *ptr;
ptr = new int;
Allocating Memory with Initialization:
int *ptr = new int(100);
The new Operator
42.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• The delete operator releases dynamically allocated memory
back to the operating system.
• The delete operator deallocates memory that was previously
allocated using new.
Syntax:
delete pointer;
Example:
delete ptr;
The delete Operator
43.
DEPARTMENT OF INFORMATIONTECHNOLOGY, SCOE,KOPARGAON
• A memory leak occurs when dynamically allocated memory
is not released using the delete operator.
• Over time, memory leaks reduce the available memory and
may slow down or crash a program.
Example:
int *ptr = new int;
// delete ptr; // Missing
Memory remains allocated even after it is no longer required.
Memory Leaks