Sie sind auf Seite 1von 37

SUBJECT: Object Oriented Programming &

Methodology(OOPM)(CS305)

UNIT _II
Class and Object
Encapsulation and Data Abstraction
Taught by:Mrs. Ruchi Saxena

1
POINTS TO COVER IN UNIT_II

1. Concept of Objects: State, Behavior & Identity of an object


2. Classes: identifying classes and candidates for Classes Attributes and
Services,
3. Access modifiers,
4. Static members of a Class,
5. Instances
6. Message passing
7. Construction and destruction of Objects.
8. Encapsulation and Data Abstraction

2
Class and Object
Object:-Objects are instances of class, which holds the data variables declared
in class and the member functions work on these class objects.

Example:-

3
Object have three characteristics/Concept
• State :-the state of an object consists of a set of it’s properties with their current
value.
• Behavior:-how an object acts and reacts in terms of its state changes.
• Identity of object:-identity is that property of an object which distinguished it
from all other.
• Example:- Bank Account
State:-Id, name, balance
Behavior:-deposite,withdraw
Identity:-two people a/c is similar but it’s state has different values.

4
Fig. object Example

5
CLASS
• The classes are the most important feature of C++ that leads to Object Oriented
programming. Class is a user defined data type, which holds its own data
members and member functions, which can be accessed and used by creating
instance of that class.
• The variables inside class definition are called as data members and the
functions are called member functions.

6
Syntax of Class

7
Access Specifier
• Public - The members declared as Public are accessible from outside the Class
through an object of the class.
• Protected - The members declared as Protected are accessible from outside the
class BUT only in a class derived from it.
• Private - These members are only accessible from within the class. No outside
Access is allowed.

8
Data Member:-The variables declared inside the class are known as data
members.
• Data member may be private ,public and protected.
Member Function:- The function declared inside the class are known as
members function.
• Member function are methods or function that are defined of objects.

9
Example:-In our program, we create a class named
programming with one variable and two functions. In the main
function, we create an object of this class and call these functions.

10
11
Class method’s definition
Member functions can be defined in two places
1. Inside class definition
2. Outside class definition

1.Inside class definition:


• A member functions of a class can also be defined inside the class.
• However, when a member function is defined inside the class, the class name and
scope resolution operator are not specified in the function header.
• Moreover, the member function defined inside a class definition are by default
inline function.

12
13
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.

14
Instance

• An instance, in object-oriented programming (OOP), is a


specific realization of any object. An object may be varied in a
number of ways. Each realized variation of that object is an
instance. The creation of a realized instance is called
instantiation.
• Each time a program runs, it is an instance of that program. In
languages that create objects from classes, an object is an
instantiation of a class. That is, it is a member of a given class
that has specified values rather than variables. In a non-
programming context, you could think of "dog" as a class and
your particular dog as an instance of that class.

15
example

• I'll explain it in the simplest way possible: Say you


have 5 apples in your basket. Each of those apples is
an object of type Apple, which has some
characteristics (i.e. big, round, grows on trees).
• In programming terms, you can have a class called
Apple, which has variables size:big, shape:round,
habitat:grows on trees. To have 5 apples in your
basket, you need to instantiate 5 apples. Apple
apple1, Apple apple2, Apple apple3 etc...

16
Message Passing
• Objects communicate with one another by sending and receiving information to
each other.
• A message for an object is a request for execution of a procedure and therefore
will invoke a function in the receiving object that generates the desired results.
• Message passing involves specifying the name of the object, the name of the
function and the information to be sent.

17
#include <iostream.h>
Class Swaping{
Call by value
// function declaration
void swap(int x, int y){if(x>y)
Cout <<“x is greater”;
Else
Cout <<“y is greater”;};
void main () {
// local variable declaration:
int a,b;
cout<<“enter value of a and b”;
Cin>>a>>b;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
Swaping s;
// calling a function to swap the values.
s.swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
18
return 0; }
#include <iostream.h>
Class Swaping{
Call by refrence
// function declaration
void swap(int *x, int *y)
{if(x>y)
Cout <<“x is greater”;
Else
Cout <<“y is greater”;};
void main () {
// local variable declaration:
int a,b;
cout<<“enter value of a and b”;
Cin>>a>>b;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
Swaping s;
// calling a function to swap the values.
s.swap(&a, &b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0; }
19
Constructor and Destructor
Constructor:-In C++, Constructor is automatically called when an object ( a
instance of the class) create. It is a special member function of the class.
• It has the same name of the class.
• It must be a public member.
• No Return Values.
• Default constructors are called when constructors are not defined for the classes.

20
Syntax of constructor

21
Program of constructor

22
23
There are 3 types of constructors:
• Default constructors
• Parameterized constructors
• Copy constructors

24
• Default Constructor
A default constructor does not have any parameters or if it has
parameters, all the parameters have default values.
Eg. Int abc(int a,int b){}

• Parameterized Constructor
If a Constructor has parameters, it is called a Parameterized
Constructor. Parameterized Constructors assist in initializing values
when an object is created.
Eg. Int abc(int a,int b){}

• Copy Constructor
A copy constructor is a like a normal parameterized Constructor, but
which parameter is the same class object. copy constructor uses to
initialize an object using another object of the same class.
Eg. Int abc(int a,int b){}//

25
Destructor
A destructor is a special member function which is called
automatically when the object goes out of scope.
• It is used for, Releasing the memory of objects.
• Closing files and resources.
• Should start with a tilde(~) and must have the same name as that of
the class.
• Destructors do not have parameters and return type.

26
Syntax of Destructor

Class class_name
{
public:
~class_name() //Destructor
{
}
}

27
Program of Destructor

28
29
Void display ()
#include<iostream.h>
{
#include<conio.h>
Cout<<”\n name :”<< name;
Class student
Cout<<”\n roll no”<<roll;
{
Cout<<”\n Height”<<height;
Private: Cout<<”\n weight”<<weight;
Char name [25]; }
Int roll; {
Float height, weight; Cout<<”\n destroying object”;
Public: }
Student () };
{ Void main ()
Strcpy (name, “ram”); {
roll=0; Student obj;
height=0; Obj. Student ();
weight=0; getch ();
} }
Encapsulation

Encapsulation is defined as wrapping up of data and information under a single


unit. In Object Oriented Programming, Encapsulation is defined as binding
together the data and the functions that manipulate them.
Consider a real-life example of encapsulation, in a company, there are different
sections like the accounts section, finance section, sales section etc. The finance
section handles all the financial transactions and keeps records of all the data
related to finance. Similarly, the sales section handles all the sales related activities
and keeps records of all the sales. Now there may arise a situation when for some
reason an official from the finance section needs all the data about sales in a
particular month. In this case, he is not allowed to directly access the data of the
sales section. He will first have to contact some other officer in the sales section and
then request him to give the particular data. This is what encapsulation is. Here the
data of the sales section and the employees that can manipulate them are wrapped 31
under a single name “sales section”.
• Data encapsulation is a mechanism of bundling the
data, and the functions that use them and data
abstraction is a mechanism of exposing only the
interfaces and hiding the implementation details from
the user.
• C++ supports the properties of encapsulation and data
hiding through the creation of user-defined types,
called classes. We already have studied that a class
can contain private, protected and public members.
By default, all items defined in a class are private.

32
class Box {
public:
double getVolume(void)
{
return length * breadth * height;
}
private:
double length;
// Length of a box
double breadth;
// Breadth of a box
double height;
// Height of a box 33
};
Data abstraction

• Data abstraction refers to providing only essential


information to the outside world and hiding their
background details, i.e., to represent the needed
information in program without presenting the
details.
• Data abstraction is a programming (and design)
technique that relies on the separation of interface and
implementation.

34
Example

• Let's take one real life example of a TV, which you can turn on
and off, change the channel, adjust the volume, and add
external components such as speakers, VCRs, and DVD
players, BUT you do not know its internal details, that is, you
do not know how it receives signals over the air or through a
cable, how it translates them, and finally displays them on the
screen.
• Thus, we can say a television clearly separates its internal
implementation from its external interface and you can play
with its interfaces like the power button, channel changer, and
volume control without having any knowledge of its internals.

35
#include <iostream.h>
class Adder
{
public:
// constructor
Adder(int i = 0)
{ total = i;
}
// interface to outside world
void addNum(int number)
{ total += number;
}
// interface to outside world
int getTotal() { return total;
};
private:
// hidden data from outside world
int total;
};
int main() {
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() <<endl;
return 0;
}
36
THANKYOU

37

Das könnte Ihnen auch gefallen