Object Oriented Programming
Techniques
Programming Paradigm
Procedural Programming
• Structured/Modular Programming
Object Oriented Programming
Programming Paradigm
• Procedural Programming
• Suitable for small size programs
• Modular approach
• Divide a problem in sub modules
• Effective for simple linear tasks
• Object Oriented Programming
• Suitable for large complex systems
• Model real world entities and their interactions
• Reusability
• Maintenance
History
Difficult to code using Machine Language
After the invention of the microprocessor, assembly
languages flourished
• Importance to functions rather than data
• Functions and Modules
• Functions
• Used for medium-sized applications
• Data is global
• Security issues
History
• These programming constructs were developed in the late
1970s and 1980s
• Issues with well structured programs
• Over generalization
• Correlation problem
• Not suitable for real time applications
Solution:
Object Oriented Approach
Object Oriented Approach
• Revolution in the programming methodology field
• Generalization
• Real-time objects creation
• Results in more organized, reusable, and maintainable code
• Languages:
• C++ (C with Classes)
• Java
• Python
• C#
Object Oriented Programming
• OOP is centered around the object, which packages together both the
data and the functions that operate on the data
• Object-oriented programming (OOP) is a programming language
model organized around objects rather than "actions" and data rather
than logic
Object-Oriented Programming
• Object-Oriented Programming
▪ In OOP, the first step in the problem-solving process is to identify the
components called objects, which form the basis of the solution, and to
determine how these objects interact with one another
▪ For example, suppose you want to write a program that automates the
University Management System
▪ The three main objects in this problem are
• Students, Faculty and Staff
OOP Terminology
• In OOP, an object’s member variables are often called its attributes
and its member functions are sometimes referred to as its behaviors
or methods
Introduction to the Class
• In C++, the class is the construct primarily used to create objects
class class-name
{
// declaration statements here
};
10
Introduction to the Class (Cont..)
11
Access Specifiers
● The key words private and public are access specifiers.
● private means they can only be accessed by the member functions
● public means they can be called from statements outside the class
○ Note: the default access of a class is private, but it is still a good idea to use the
private key word to explicitly declare private members
○ This clearly documents the access specification of the class
12
Example:
class Rectangle
{
private:
float width, length, area;
public:
void setData(float, float);
void calcArea(void);
float getWidth(void);
float getLength(void);
float getArea(void);
};
13
Defining an Instance of a Class
• Class objects must be defined after the class is declared.
• Defining a class object is called the instantiation of a class.
• Rectangle box; // box is an instance of Rectangle
14
Objects and Classes
#include <iostream>
class smallobj //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “Data is “ << somedata << endl; }
};
15
Objects and Classes Cont..
int main()
{
smallobj s1, s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display data
s2.showdata();
return 0;
}
16