OOP Chapter 2 Lecture Notes 2021

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Object

Oriented
Programming
Using C++

Presented By

Milind Vijayrao Lande


Lecturer in computer Engg
Government Polytechnic,Gadchiroli.
Chapter 2
Unit I : Classes and Objects
 Course Outcome
 Develop a C++ Programs to solve problem using
Procedure Oriented Programming.
 Develop C++ Programs using Classes and Objects.
 Implement Inheritance in C++ Programs.
 Use Polymorphism in C++ Programs.
 Develop C++ Programs To Performs file operation.

 Unit Outcome
 Develop relevant Friends functions to solve the given problem.
 Write C++ Program to use array of given objects.
 Write C++ Programs to create the given object using constructor.
 Write program to delete the given object using destructor in C++ program.
Introduction
• Class: A class in C++ is the building block, that leads to Object-
Oriented programming. It is a user-defined data type, which
holds its own data members and member functions, which can
be accessed and used by creating an instance of that class.
• A C++ class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many
cars with different names and brand but all of them will share
some common properties like all of them will have 4 wheels,
 Speed Limit, Mileage range etc. So here, Car is the class and
wheels, speed limits, mileage are their properties.
• A Class is a user defined data-type which has data members
and member functions.
• Data members are the data variables and member functions are
the functions used to manipulate these variables and together
these data members and member functions defines the
properties and behavior of the objects in a Class
An Object is an instance of a Class. When a class is
defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
Defining Class and Declaring Objects
A class is defined in C++ using keyword class followed by
the name of class. The body of class is defined inside the
curly brackets and terminated by a semicolon at the end.

Declaring Objects: When a class is defined, only the specification


for the object is defined; no memory or storage is allocated. To use
the data and access functions defined in the class, you need to
create objects.
Syntax:
ClassName ObjectName;
Accessing data members and member functions: The data
members and member functions of class can be accessed
using the dot(‘.’) operator with the object. For example if
the name of object is obj and you want to access the
member function with the name printName() then you will
have to write 
obj.printName() .
Accessing Data Members
The public data members are also accessed in the same
way given however the private data members are not
allowed to be accessed directly by the object. Accessing a
data member depends solely on the access control of that
data member.
This access control is given by Access modifiers in C++.
There are three access modifiers : public, private and
protected.
Member Functions in Classes

There are 2 ways to define a member function:


 Inside class definition
 Outside class definition
To define a member function outside the class definition we
have to use the scope resolution :: operator along with class
name and function name.
Defining Member Function Inside a Class
The following example shows that how a member function can be
declared and defined within the class
Static Data Member
A data member of a class can be qualified as static. The
properties of a static member variable are similar to that of
Cs static variable. A static data member has certain special
characteristics.
They are:-
 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 created for the entire
class and is shared by all the objects of that class, no
matter how many objects are created.
 It is visible only within the class, but its lifetime is the
entire program.
A static variable is normally used to maintain value
common to the entire class. For e.g, to hold the count of
objects created. Note that the type and scope of each static
member variable must be declared outside the class
definition. This is necessary because the static data
members are stored separately rather than as a part of an
object.
Difference Between Class and Structure
Declaration
static data_type member_name;

Defining the static data member

It should be defined outside of the class following this


syntax:
data_type class_name :: member_name =value;
If you are calling a static data member within a member function,
member function should be declared as static (i.e. a static
member function can access the static data members)
For Example :
#include <iostream>
using namespace std;
class Demo
{
public:
static int ABC;
};
//defining
int Demo :: ABC =10;
int main()
{
cout<<"\nValue of ABC: "<<Demo::ABC;
return 0;
}

Output : Value of ABC: 10


Static Member Function
like a static member variable, we can also have static member
functions. A member function that is declared static has the
following properties:-
 A static function can have access to only other static members
(function or variable) declared in the same class.
 A static member function can be called using the class name
(instead of its object)
as follows- Class_name::Function_name();
Consider the example, here static data member is accessing
through the static member function:

#include <iostream>
using namespace std;class Demo
{
private:
static int X; public:
static void fun()
{
cout <<"Value of X: " << X << endl;
}
};//defining
int Demo :: X =10;
int main()
{
Demo X; X.fun();

return 0;
}

Output :
Value of x : 10
Friend Function
A friend function of a class is defined outside that class'
scope but it has the right to access all private and
protected members of the class. Even though the
prototypes for friend functions appear in the class
definition, friends are not member functions.
Friend Function

#include <iostream>
using namespace std;
class Box
{ double width;
public:
friend void printWidth( Box box );
void setWidth( double wid ); }; // Member function definition
void Box::setWidth( double wid )
{ width = wid;
} // Note: printWidth() is not a member function of any class.
void printWidth( Box box )
{
/* Because printWidth() is a friend of Box, it can directly access any member of this class */
cout << "Width of box : " << box.width <<endl;
} // Main function for the program
int main()
{ Box box; // set box width without member function
box.setWidth(10.0);
// Use friend function to print the wdith.
printWidth( box );
return 0; }

Out Put : Width of Box : 10


inheritance
Advantage of inheritance
 If we develop any application using this concept than that
application have following advantages,
 Application development time is less.
 Application take less memory.
 Application execution time is less.
 Application performance is enhance (improved).
 Redundancy (repetition) of the code is reduced or minimized so
that we get consistence results and less storage cost
Tpyes of Inheritance
Based on number of ways inheriting the feature of base class into
derived class it have five types they are:
1.Single inheritance
2.Multiple inheritance
3.Hierarchical inheritance
4.Multiple inheritance
5. Hybrid inheritance
Dynamic binding
Connecting a method call to the method body is known as
binding.
There are two types of binding
• Static Binding (also known as Early Binding).
• Dynamic Binding (also known as Late Binding).
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy