0% found this document useful (0 votes)
131 views12 pages

5 - Encapsulation, Abstraction, Polymorphism

The document discusses data abstraction, encapsulation, and polymorphism in object-oriented programming. It provides examples of how data abstraction hides implementation details and exposes only essential information to users. Encapsulation prevents unintended changes to an object's internal data. Polymorphism allows the same function to operate on different types of objects through late binding at runtime based on an object's actual type. Abstract classes define common interfaces for derived classes without implementing behavior.

Uploaded by

Abdullah Rayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views12 pages

5 - Encapsulation, Abstraction, Polymorphism

The document discusses data abstraction, encapsulation, and polymorphism in object-oriented programming. It provides examples of how data abstraction hides implementation details and exposes only essential information to users. Encapsulation prevents unintended changes to an object's internal data. Polymorphism allows the same function to operate on different types of objects through late binding at runtime based on an object's actual type. Abstract classes define common interfaces for derived classes without implementing behavior.

Uploaded by

Abdullah Rayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CHAPTER 5

ENCAPSULATION, ABSTRACTION,
COURSE NAME POLYMORPHISM

INTRODUCTION TO
PROGRAMMING
CSC 1102
MD.NAZMUL HOSSAIN
(UNDERGRADUATE) LECTURER, CS, AIUB
Data Abstraction Slide-2

 Data abstraction refers to, providing only essential


information to the outside world and hiding their #include <iostream>
background details, i.e., to represent the needed using namespace std;
information in program without presenting details. int main( )
{
 Data abstraction is a programming and design
cout << "Hello C++"<<endl;
technique that relies on the separation of interface return 0;
and implementation. }
 Real life example of a TV, which you can turn on and Here, you don't need to understand
off, change the channel, adjust the volume, and add how cout displays the text on the
external components such as speakers, VCRs, and DVD user's screen. You need to only know
players, BUT you do not know its internal details, that the public interface and the
underlying implementation of cout is
is, you do not know how it receives signals over the air free to change.
or through a cable, how it translates them, and finally
displays them on the screen.
MMH
Slide-3

Benefits of Data Abstraction


 Class internals are protected from inadvertent (unintended) user-level errors,
which might corrupt the state of the object.
 Provides only the essential information to the outside world and hides the
background details, so the interface remain secure
 The class implementation may evolve over time in response to changing
requirements or bug reports without requiring change in user-level code.
 Separates the interface from implementation, so the code can be modified
afterwards without changing the interface

MMH
Slide-4
#include <iostream> Example of Data Abstraction
using namespace std;

class Adder{
public:
Data Encapsulation prevents users
int main( ) of the classes from unintentionally
// interface to outside world { altering a critical piece of data or
void addNum(int number) { Adder a; gaining access to a sensitive data
total += number;
a.addNum(10); item. This programming strategy is
}
a.addNum(20); called encapsulation or data hiding
// interface to outside world a.addNum(30);
int getTotal() {
cout << "Total “
return total;
}; << a.getTotal();
private: return 0;
// hidden data from outside world }
int total = 2; Output
}; Total 62
MMH
Slide-5
Abstract Class (Interface)
 An interface describes the behavior or capabilities of a C++ class without committing
to an implementation of that class.
 The C++ interfaces are implemented using abstract classes and these abstract
classes should not be confused with data abstraction which is a concept of keeping
implementation details separate from associated data.
class Box
{
 A class is made abstract by public:
declaring at least one of its virtual double getVolume() = 0; // pure virtual
functions as pure virtual function

function. A pure virtual private:


function is specified by placing double length; // Length of a box
"= 0" in its declaration as follows: double breadth; // Breadth of a box
double height; // Height of a box
};
MMH
Slide-6

Abstract Class (Interface)

 The purpose of an abstract class is to provide an appropriate base class from


which other classes can inherit.
 Abstract classes cannot be used to instantiate objects and serves only as an
interface. Attempting to instantiate an object of an abstract class causes a
compilation error.
 Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the
virtual functions, which means that it supports the interface declared by the ABC.
 Failure to override a pure virtual function in a derived class, then attempting to
instantiate objects of that class, is a compilation error.
 Classes that can be used to instantiate objects are called concrete classes.

MMH
Slide-7

Abstract Class (Interface)


#include <iostream>
using namespace std; class Rectangle: public Shape
{
class Shape // Base class public:
{ int getRecArea(){
public: return (width * height);
// pure virtual function providing }
interface framework. };
virtual int getArea() = 0; class Triangle: public Shape
void setWidth(int w) {
{ width = w; } public:
void setHeight(int h) int getTriArea(){
{ height = h; } return (width * height)/2;
protected: }
int width; };
int height;
};
MMH
Slide-8

int main() Abstract Class (Interface)


{
Shape sh; //will provide an error
Output
Rectangle Rect;
Triangle Tri; Total Rectangle area: 35
Total Triangle area: 17
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " << Rect.getRecArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// Print the area of the object.
cout << "Total Triangle area: " << Tri.getTriArea() << endl;
return 0;
}
MMH
Slide-9

Polymorphism
 The word polymorphism means having many forms.
 Typically, polymorphism occurs when there is a hierarchy of classes and they are
related by inheritance.
 C++ polymorphism means that a call to a member function will cause a different
function to be executed depending on the type of object that invokes the function.
 Function Overloading is also called Compile time Polymorphism

MMH
Slide-10
#include <iostream>
using namespace std; Polymorphism (run-time)
class Shape { class Rectangle: public Shape{
protected:
int width, height; public:

public: Rectangle( int a=0, int b=0)


{ width = a;
Shape( int a=0, int b=0){ height = b;
width = a; }
void area ()
height = b;
{
} cout << "Rectangle class area :"
<< width * height << endl;
virtual void area(){
}
cout << "Parent class area :" };
<< 0 <<endl;
return 0;
}
};
MMH
Slide-11
Polymorphism (run-time)
class Triangle: public Shape{ int main( ){
public: Shape sh;
Shape *shape;
// set width, height (parent constructor) Rectangle rec(10,7);
through child constructor Triangle tri(10,5);
Triangle( int a=0, int b=0):Shape(a, b) sh.area();
{ } // call rectangle area using base
void area () class object pointer
{ shape = &rec;
cout << "Triangle class area :" // store the address of Triangle
<< width*height / 2<<endl; shape->area();
} shape = &tri;
};
// call triangle area using base class
object pointer
Output (without virtual function) Output (with virtual function)
shape->area();
Parent class area: 0 Parent class area: 0
return 0;
Parent class area: 0 Rectangle class area: 70
parent class area: 0 Triangle class area: 25 }

MMH
Slide - 20
REFERENCES

 Tech Yourself C++, Herbert Schildt.


 C++ How To Program, Paul Deitel, Harvey Deitel.
 The C++ Complete Reference, Herbert Schildt.
 Thinking in C++, Volume One, Bruce Eckel.

You might also like