Unit - Ii: Class and Object Encapsulation and Data Abstraction
Unit - Ii: Class and Object Encapsulation and Data Abstraction
Unit - Ii: Class and Object Encapsulation and Data Abstraction
Methodology(OOPM)(CS305)
UNIT _II
Class and Object
Encapsulation and Data Abstraction
Taught by:Mrs. Ruchi Saxena
1
POINTS TO COVER IN UNIT_II
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
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
15
example
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
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
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