You are on page 1of 47

1

Introduction to Object
Oriented Programming
Presented by,
Rahul Raj.M
Asst. Professor
DCA,CUSAT
PROCEDURAL PROGRAMMING
• A program in a procedural language is a list of instruction where
each statement tells the computer to do something.
• It focuses on procedure (function) & algorithm is needed to
perform the derived computation.
• When program become larger, it is divided into function & each
function has clearly defined purpose.
• Ex: C, FORTAN, BASIC

3
Fig: Procedural oriented programming

4
5
Output

6
Drawback of Procedural oriented
programming
• It emphasis on doing things.
• Data is given a second class status even through data is the reason
for the existence of the program.
• Since every function has complete access to the global variables,
the new programmer can corrupt the data accidentally by
creating function.
• If new data is to be added, all the function needed to be modified
to access the data.
• Difficult to relate with real world objects

7
Object Oriented Programming
• Object-oriented programming (OOP) is a programming paradigm
based on the concept of "objects", which may contain data, in the
form of fields, often known as attributes; and code, in the form of
procedures, often known as methods.
• “Class” refers to a blueprint. It defines the variables and
methods the objects support. It is the basic unit of Encapsulation. It
also defines as the Collection of a similar types of objects.
• “Object” is an instance of a class. Each object has a class which
defines its data and behavior.

8
9
Student #340
Student #200

appno = 30 appno= 31
Name = “Manu” Name = “Anu”
Percentage = 56.0 Percentage = 70.0

readData() readData()
WriteData() WriteData()

Student #200
appno = 32
Name = “Ajay”
Percentage = 78.0

readData()
WriteData()

Student objects 10
Student

appno
Name
Percentage

readData()
WriteData()

Student class

11
12
13
14
15
Class v/s Object

16
Basic Concepts in OOPS
1. Objects
2. Classes
3. Abstraction
4. Encapsulation
5. Inheritance
6. Overloading
7. Exception Handling

17
Objects
• Objects are the basic unit of OOP. They are instances of class, which have
data members and uses various member functions to perform tasks.
Class
• It is similar to structures in C language. Class can also be defined as user
defined data type but it also contains functions in it. So, class is basically a
blueprint for object. It declare & defines what data variables the object will
have and what operations can be performed on the class's object.
Abstraction
• Abstraction refers to showing only the essential features of the application
and hiding the details. In C++, classes can provide methods to the outside
world to access & use the data variables, keeping the variables hidden from
direct access, or classes can even declare everything accessible to everyone,
or maybe just to the classes inheriting it. This can be done using access
specifiers.

18
DEMO

19
Encapsulation
• It can also be said data binding. Encapsulation is all about binding the data
variables and functions together in class.
Inheritance
• Inheritance is a way to reuse once written code again and again. The class
which is inherited is called the Base class & the class which inherits is
called the Derived class. They are also called parent and child class.
• So when, a derived class inherits a base class, the derived class can use all
the functions which are defined in base class, hence making code reusable.

20
DEMO

Encapsulation

21
DEMO

Inheritance

22
Polymorphism
• The word polymorphism means having many forms.
• It is a feature, which lets us create functions with same name but different
arguments, which will perform different actions. That means, functions
with same name, but functioning in different ways. Or, it also allows us to
redefine a function to provide it with a completely new definition. You will
learn how to do this in details soon in coming lessons.
Exception Handling
• Exception handling is a feature of OOP, to handle unresolved exceptions or
errors produced at runtime.

23
DEMO

Polymorphism

24
C++
• “Bjarne Stroustrup” develops C++ (1980’s) Brings object oriented
concepts into the C programming language
C++ I/O Streams
• C++ input and output is performed in the form of sequence of bytes
or more commonly known as streams
• Input Stream: If the direction of flow of bytes is from device(for
example: Keyboard) to the main memory then this process is
called input.
• Output Stream: If the direction of flow of bytes is opposite, i.e.
from main memory to device( display screen ) then this process is
called output.

25
Header files available in C++ for Input – Output operation are:
• iostream: iostream stands for standard input output stream. This
header file contains definitions to objects like cin, cout, cerr etc.
• iomanip: iomanip stands for input output manipulators. The
methods declared in this files are used for manipulating streams.
This file contains definitions of setw, setprecision etc.
• fstream: This header file mainly describes the file stream. This
header file is used to handle the data being read from a file as input
or data being written into the file as output.

26
Standard output stream (cout)

• Usually the standard output device is the display screen. cout is the
instance of the ostream class. cout is used to produce output on the
standard output device which is usually the display screen. The data
needed to be displayed on the screen is inserted in the standard
output stream (cout) using the insertion operator (<<).

#include <iostream.h>
void main( ) {
char place[40] = “Ootty";
cout << “Welcome to:”<< place;
cout << “\nNice to meet you”;
}
Output: Welcome to Ooty
Nice to meet you 27
standard input stream (cin)
• Usually the input device is the keyboard. cin is the instance of the
class istream and is used to read input from the standard input
device which is usually keyboard.
• The extraction operator(>>) is used along with the object cin for
reading inputs. The extraction operator extracts the data from the
object cin which is entered using the keboard.
#include<iostream.h>
void main()
{
int age;
cout << "Enter your age:";
cin >> age;
cout << "\nYour age is: "<<age; }
Input : 18
Output:
28
Enter your age: Your age is: 18
Un-buffered standard error stream (cerr)

• cerr is the standard error stream which is used to output the errors.
This is also an instance of the ostream class. As cerr is un-buffered
so it is used when we need to display the error message
immediately. It does not have any buffer to store the error
message and display later.
• #include <iostream.h>
void main( )
{
cerr << "An error occurred“<<a;
}
Output: An error occurred

29
buffered standard error stream (clog)

• This is also an instance of ostream class and used to display errors but
unlike cerr the error is first inserted into a buffer and is stored in the buffer
until it is not fully filled. The error message will be displayed on the
screen too.
• #include <iostream.h>
void main( )
{
clog << "An error occurred";
}
• output: An error occurred

30
Benefits of C++ over C Language
• The major difference being OOPS concept, C++ is an object
oriented language whereas C language is a procedural language.
Apart form this there are many other features of C++ which gives
this language an upper hand on C language.
Following features of C++ makes it a stronger language than C,
1. There is Stronger Type Checking in C++.
2. All the OOPS features in C++ like Abstraction, Encapsulation,
Inheritance etc makes it more worthy and useful for programmers.
3. C++ supports and allows user defined operators (i.e Operator
Overloading) and function overloading is also supported in it.

31
4. Exception Handling is there in C++.
5. The Concept of Virtual functions and also Constructors and
Destructors for Objects.
6. Inline Functions in C++ instead of Macros in C language. Inline
functions make complete function body act like Macro, safely.
7. Variables can be declared anywhere in the program in C++, but
must be declared before they are used.

32
33
OOPS Definitions

34
Student class
#include <iostream> int main()
class Student{ {
private: Student s1;
int age; s1.setValue(20);
public: s1.getValue();
void setValue(int ag){ return 0;
age = ag; }
}
void getValue(){
cout<<"Age is:"<<age;
}
};

35
Function definition using scope resolution

#include <iostream> void Student::getValue(){


using namespace std; cout<<"Age is:"<<age;
class Student{ }
private: int main()
int age; {
public: Student s1;
void setValue(); s1.setValue(20);
void getValue(); s1.getValue();
return 0;
}; }
void Student::setValue(int ag){
age = ag;
}

36
Access Specifies
• Access specifies define how the members of the class can be
accessed. Of course, any member of a class is accessible within that
class(Inside any member function of that same class).
• private: These members are only accessible from within the class.
No outside Access is allowed.
• protected: The members declared as Protected are accessible from
outside the class BUT only in a class derived from it.
• public: public members are accessible anywhere (outside the class).

37
CONSTRUCTOR
• A constructor is a member function of a class which initializes
objects of a class. In C++, Constructor is automatically called
when object(instance of class) create. It is special member
function with same name as class.
A constructor is different from normal functions in following ways:
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object is created.
• If we do not specify a constructor, C++ compiler generates a default
constructor for us (expects no parameters and has an empty body).

38
39
40
#include <iostream> int main()
{
using namespace std; Student s1(20);
class Student{ s1.getValue();
private: return 0;
int age; }
public:
Student(int x){
age = x;
}
void getValue(){
cout<<"Age is:"<<age;
}
};

41
APPLICATIONS OF OBJECT ORIENTED
PROGRAMMING
1. Client-Server Systems
• Object-oriented Client-Server Systems provide the IT infrastructure, creating
object-oriented Client-Server Internet (OCSI) applications. Here, infrastructure
refers to operating systems, networks, and hardware. OSCI consist of three
major technologies:
• The Client Server
• Object Oriented Programming
• The Internet
2. Object Oriented Databases
• They are also called Object Database Management Systems (ODBMS). These
databases store objects instead of data, such as real numbers and integers.
Objects consist of the following:
• Attributes: Attributes are data that defines the traits of an object. This data can
be as simple as integers and real numbers. It can also be a reference to a
complex object.
• Methods: They define the behavior and are also called functions or procedures.

42
3. Real-Time System Design
• Real time systems inherit complexities that makes difficult to build them.
Object-oriented techniques make it easier to handle those complexities.
These techniques present ways of dealing with these complexities by
providing an integrated framework which includes schedulability analysis
and behavioral specifications.
4. Simulation And Modeling System
• It’s difficult to model complex systems due to the varying specification of
variables. These are prevalent in medicine and in other areas of natural
science, such as ecology, zoology, and agronomic systems. Simulating
complex systems requires modelling and understanding interactions
explicitly. Object-oriented Programming provides an alternative approach
for simplifying these complex modelling systems.

43
5. Hypertext And Hypermedia
• OOP also helps in laying out a framework for Hypertext. Basically,
hypertext is similar to regular text as it can be stored, searched, and edited
easily. The only difference is that hypertext is text with pointers to other
text as well.
• Hypermedia, on the other hand, is a superset of hypertext. Documents
having hypermedia, not only contain links to other pieces of text and
information, but also to numerous other forms of media, ranging from
images to sound.
6. Neural Networking And Parallel Programming
• It addresses the problem of prediction and approximation of complex time-
varying systems. Firstly, the entire time-varying process is split into several
time intervals or slots. Then, neural networks are developed in a particular
time interval to disperse the load of various networks. OOP simplifies the
entire process by simplifying the approximation and prediction ability of
44
networks.
7. Office Automation Systems
• These include formal as well as informal electronic systems primarily
concerned with information sharing and communication to and from people
inside as well as outside the organization. Some examples are:
• Email
• Word processing
• Web calendars
• Desktop publishing
8. CIM/CAD/CAM Systems
• OOP can also be used in manufacturing and design applications as it allows
people to reduce the effort involved. For instance, it can be used while
designing blueprints, flowcharts, etc. OOP makes it possible for the
designers and engineers to produce these flowcharts and blueprints
accurately.
45
9. AI Expert Systems
• These are computer applications which are developed to solve complex
problems pertaining to a specific domain, which is at a level far beyond the
reach of a human brain.
• It has the following characteristics:
• Reliable
• Highly responsive
• Understandable
• High-performance

46
47

You might also like