4. Abstraction
❖Abstraction is one of the key concepts of object-oriented
programming (OOP) languages.
❖Abstraction means displaying only essential information and
hiding the details
5. Cont.
❖Its main goal is to handle complexity by hiding unnecessary
details from the user.
❖That enables the user to implement more complex logic on
top of the provided abstraction without understanding or
even thinking about all the hidden complexity.
8. Abstraction in OOP
❖Objects in an OOP language provide an abstraction that hides the
internal implementation details.
❖We just need to know which methods of the object are available to
call and which input parameters are needed to trigger a specific
operation.
❖But we don’t need to understand how this method is implemented
and which kinds of actions it has to perform to create the expected
result.
9. Program for Abstraction
#include <iostream>
using namespace std;
class implementAbstraction
{
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
10. Advantages of Data Abstraction:
❖Helps to avoid writing the low level code
❖Avoids code duplication and increases reusability.
❖Can change internal implementation of class independently
without affecting the user.
❖Helps to increase security of an application or program as
only important details are provided to the user.
11. Encapsulation
❖Encapsulation refers to the binding of data with the
methods that operate on that data, or the restricting of
direct access to some of an object's components
12. Program for Encapsulation
#include<iostream>
using namespace std;
class Encapsulation
{
private:
// data hidden from outside world
int x;
public:
// function to set value of
// variable x
void set(int a)
{
x =a;
}
// function to return value of
// variable x
int get()
{
return x;
}
};
// main function
int main()
{
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}
14. Implementation of Class and Object
What is Class?
❖ Class is a user defined data type which can be used, just like any
other basic data type, to declare variables. In Object Oriented
Programming, class is a collection of similar objects.
❖ A class describes a group of objects with similar properties,
common behavior, and common relationships.
What it contains?
❖ A class contains the entire set of data and code to manipulate the
data (known as function). A class is a way to bind the data and its
associated functions together. It allows the data and functions to be
hidden, if necessary, from external use. A class contains both the
data and functions. The internal data of a class is called data
member and functions are called member functions.
15. Implementation of Class and Object
Fig. : class contains data and function
Need of Class
Classes are required to represent real world
entities that not only have data type properties
but also associated operations. The class
provides support for data hiding, abstraction,
encapsulation, inheritance and polymorphism.
17. Implementation of class in C++
● A class should give a meaningful name as this name becomes a new type identifier that
can be used to declare objects of class type.
● A class should contain data member and member functions. The data members are
usually declared as private and the member functions as public.
● All members of a class are by default private, and scope of private variable is limited
only within the class.
18. Implementation of Object
What is Object?
❖ Object is an instance of a class. It is a basic run-time entity in an
object oriented system. The class variables are known as objects
or instances of class.
Example
❖ A person, place or any data item can become an object. It can be
individual item, unit or entity. Any item that program has to
handle is an object.
19. Implementation of Object
***Note:
❖ Objects are the thing, we think about first in program design, and
object is what actually runs on the computer in programming.
How many objects of a class can be created?
❖ Once a class has been defined, we can create any number of
objects belonging to that class.
20. Implementation of Object
Implementation of object in C++
● The declaration of an object is similar to that of a variable of any basic type. Objects can
be created, when a class is defined, by placing object names immediately after the
closing brace, similarly we do create tag in a structure.
Syntax: class_name object_name;
● class class_name }
● {
● data members;
● member functions;
● };
● Int main()
● {
21. Implementation of Object
Program
● #include <iostream.h>
● class IT
● {
● private:
● int i;
● public:
● void get()
● {
● cout<<"Enter i : ";
● cin>>i;
● }
● void show()
● {
● i++;
● cout<<"i after increment : "<<i;
● }
● };
● int main()
● {
● IT obj;
● obj.get();
● obj.show();
● return 0;
● }
Output
22. Class Vs Object
Sr. No. Class Object
1 A class is a collection of objects of
similar type.
An object is a basic run time entity in an object oriented
programming approach.
2 A class is a set of object Object is a variable of type class.
3 A class contains many objects. Object contains data, and code to manipulate that data.
4 Once a class has been defined, it exists till the
program is running.
Object can be created and destroyed dynamically.
5 A class has a unique name, and each
object is associated with it.
An object has behavior and identity
6 A class is a logical abstraction. An object has physical existence.
7 A class has static nature. An object has dynamic nature.
8 For example: Branch IT;
Where "Branch" is a class.
For example: Branch IT;
Where "IT" is an object.
23. Access Modifiers
❖ Private, Protected, Public are called visibility labels.
❖ The members that are declared private can be accessed only from
within the class.
❖ Public members can be accessed from outside the class also.
❖ In C++, data can be hidden by making it private, thus we can
achieved Data Encapsulation.
❖ It is important to note that private and protected members can not be
accessed directly using class member access operator (.)
24. Access Modifiers
Private
❖ The members declared in private section of a class can only be
accessed by the member function and friend function of the class.
The private members of the class are inaccessible out of the class
scope.
Protected
❖ The members declared in protected section of a class can only be
accessed by the member function and friend function of the class.
The protected members of the class are inaccessible out of the class
scope except the class derived immediately from it. It serves a
limited purpose in inheritance.
Public
❖ The members declared in public section of a class can be accessed
by all the functions present in out of the class scope.
26. Access Modifiers
Sr. No. Access Specifier Accessible from own
class
Accessible from
derived class
Accessible from
outside
1 Public Yes Yes Yes
2 Protected Yes No No
3 Private Yes No No
28. Object data type
An object can represent variable of a user defined data
type. As a variable can have its data type similarly an
object may have its type.
For example:
int i; //declares a variable i of type integer. Similarly,
IT obj; //declares an object of type IT
29. Object data type
Program
● #include <iostream.h>
● class IT
● {
● private:
● int a;
● public:
● void set(int x)
● {
● a=x;
● }
● void get()
● {
● cout<<"Enter a : ";
● cin>>a;}
● void show()
● {
● cout<<"a="<<a;
● }
● };
● int main()
● {
● IT obj1,obj2;
● obj1.set(5);
● obj2.get();
● cout<<"Value provided by
argument : ";
● obj1.show();
● cout<<"nValue input by user
: ";
● obj2.show();
● return 0;}
Output
31. Constructor
❖ A member function can be used to give values to the data items,
however an object can initialize itself when it is created, without the
need to make a separate call to a member function. This automatic
initialization is carried out using a special member function called
constructor.
❖ Constructor is a member function of a class having same name as
class and it is executed/invoked automatically whenever an object of
its associated class is created.
❖ Constructor is called constructor because it constructs the values of
data members of the class.
32. Constructor Important Points
● The name of constructor function must be same as that of its class name.
● Constructors are declared with no return type not even void.
● It should have public or protected access within the class.
● It should be declared private only in rare circumstances.
● It can be invoked for const objects.
● It cannot be declared as static, virtual or const.
34. Characteristics of Constructor
● Whenever an object is created, the constructor will be called automatically.
● We cannot refer to their addresses.
● Constructors can have default arguments.
● They cannot be inherited.
● Constructor without arguments is called a default constructor.
● For dynamic memory allocation, constructors use implicit call with new and delete
operators.
● An object with a constructor cannot be used as member function.
● Initialization of an object of the class becomes mandatory, when a constructor is
declared for a class.
● Constructor can be overloaded to accommodate many different forms of initialization.
35. Types of Constructor
Types of constructor available in C++ are:
❖ 1. Default constructors (constructor without parameter)
❖ 2. Parameterized constructor (constructor with parameter)
❖ 3. Copy constructor
36. Types of Constructor
● Default Constructors: Default constructor is the constructor which doesn’t take any
argument. It has no parameters.
● Parameterized Constructors: It is possible to pass arguments to constructors.
Typically, these arguments help initialize an object when it is created. To create a
parameterized constructor, simply add parameters to it the way you would to any other
function. When you define the constructor’s body, use the parameters to initialize the
object.
● Copy Constructor: A copy constructor is a member function which initializes an object
using another object of the same class.
37. Default Constructor
● #include <iostream>
● class IT
● {
● public:
● IT()
● {
● cout<<"Inside default constructor"<<endl;
● }
● void show()
● {
● cout<<"Inside member function"<<endl;
● }
● };
● int main()
● {
● IT obj;
● obj.show();
● cout<<"Inside main function"<<endl;
● return 0;
● }
Output
38. Parameterized Constructor
● #include <iostream.h>
● class IT
● {
● private:
● int a;
● public:
● IT(int x) //parameterized constructor
● {
● a=x;
● }
● void show()
● {
● cout<<"Value of a="<<a;
● }
● };
● int main()
● {
● IT obj(5); //object initialization
● obj.show();
● return 0;
● }
Output
39. Copy Constructor
● #include <iostream.h>
● class IT
● {
● private:
● int a;
● public:
● IT(int t)
● {
● a=t;
● }
● IT(IT &x) //copy constructor
● {
● a=x.a;
● }
● void show()
● {
● cout<<a<<endl;
● }
● };
● int main()
● {
● int x;
● cout<<"Enter x : ";
● cin>>x;
● IT obj1(x);
● IT obj2(obj1); //call to copy constructor
● cout<<"Value of a : ";
● obj1.show();
● cout<<"Value of a after copy : ";
● obj2.show();
● return 0;
● }
Output
40. Destructor
● Destructor is also a member function of a class whose name is also same as that of the
class preceded by a ~ (tilde sign). Destructor is executed at the end of the function
when objects are of no use, or goes out of scope.
Use of destructor
● The most common use of destructor is to de-allocate memory that was allocated for
the objects by the constructor.
41. Destructor
● #include <iostream.h>
● int count=0;
● class IT
● {
● public:
● IT()
● {
● count++;
● cout<<"object created using constructor :
obj"<<count<<endl;
● }
● ~IT()
● {
● cout<<"object destroyed using destructor :
● obj"<<count<<endl;
● count--;
● }
● };
● int main()
● {
● IT obj1,obj2,obj3,obj4,obj5;
● cout<<"main function
executed"<<endl;
● return 0;
● }
Output
42. Access Modifiers
❖ Private, Protected, Public are called visibility labels.
❖ The members that are declared private can be accessed only from
within the class.
❖ Public members can be accessed from outside the class also.
❖ In C++, data can be hidden by making it private, thus we can
achieved Data Encapsulation.
❖ It is important to note that private and protected members can not be
accessed directly using class member access operator (.)
43. Access Modifiers
Private
❖ The members declared in private section of a class can only be
accessed by the member function and friend function of the class.
The private members of the class are inaccessible out of the class
scope.
Protected
❖ The members declared in protected section of a class can only be
accessed by the member function and friend function of the class.
The protected members of the class are inaccessible out of the class
scope except the class derived immediately from it. It serves a
limited purpose in inheritance.
Public
❖ The members declared in public section of a class can be accessed
by all the functions present in out of the class scope.
45. Access Modifiers
Sr. No. Access Specifier Accessible from own
class
Accessible from
derived class
Accessible from
outside
1 Public Yes Yes Yes
2 Protected Yes No No
3 Private Yes No No
47. Object data type
An object can represent variable of a user defined data
type. As a variable can have its data type similarly an
object may have its type.
For example:
int i; //declares a variable i of type integer. Similarly,
IT obj; //declares an object of type IT
48. Object data type
Program
● #include <iostream.h>
● class IT
● {
● private:
● int a;
● public:
● void set(int x)
● {
● a=x;
● }
● void get()
● {
● cout<<"Enter a : ";
● cin>>a;}
● void show()
● {
● cout<<"a="<<a;
● }
● };
● int main()
● {
● IT obj1,obj2;
● obj1.set(5);
● obj2.get();
● cout<<"Value provided by
argument : ";
● obj1.show();
● cout<<"nValue input by user
: ";
● obj2.show();
● return 0;}
Output
50. Static Data Member
❖ The static data members provide access control to some shared
resources. They normally used to maintain values common to the
entire class.
❖ Each object has its separate set of data members in memory. The
member functions are created only once and all objects share the
functions.
❖ No separate copy of functions of each object is created in the
memory.
***Point to remember:
❖ Static data members are data objects of a class. They exist only
once in all objects of this class.
51. Static Data Member
Advantage of Static Data Member
● Static data members may be helpful to declare the global data which should be updated while the
program exists in the memory.
Features of Static Data Member
● Static member variables are normally used to maintain values common to the entire class.
● Static member variables are visible within the class, but its lifetime is the entire program.
● Since they are associated with the class itself rather than with any class object, they are also known as
class variables.
● Type and scope of each static member variable must be defined outside the class definition, because the
static data members are stored separately, rather than as a part of an object.
● 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.
● Static member variable is initialized to zero when the first object of its class is created, no other
initialization is permitted.
58. Static Member Function
● The static member functions used to pre-initialize private static data members before
any object is actually created. Like data members, functions can also be declared as
static.
● Static member functions can access only static data members and functions of the same
class. The non static data members are not available to these functions.
Features of static member functions
● The static member function declared in public section can be invoked using its class
name without using its objects.
● Just one copy of static member function is created in the memory for entire class. All
objects of the class share the same copy of static member function.