Introdcution to Object-Oriented Programming
Programming Languages
Machine-level Language programming
Assembly-language programming
High-level language programming
Procedural Programming
Structured programming
Object-Oriented Programming
Procedural Programming
• The original programming paradigm is:
• Decide which procedures you want; use the best algorithms you can find.
• The focus is on the processing – the algorithm needed to perform the desired
computation.
• Languages support this paradigm by providing facilities for passing
arguments to functions and returning values from functions.
• Ways to pass arguments, ways to distinguish different kinds of arguments,
different kinds of functions (e.g., procedures, routines, and macros), etc.
Procedural Programming
• Creating an account for an individual (account)
• Getting an account to deposit or withdraw funds
(getAccount, deposit, withdraw)
• Transferring funds between two different accounts
(transfer)
• Examples of Languages: ALGOL, COBOL, BASIC,
PASCAL, FORTRAN, and C
Example
double sqrt (double arg ) • From the point of view
of program
{ organization, functions
/ / code for calculating a square root are used to create order
in a maze of algorithms.
}
• The algorithms
themselves are written
void f( ) using function calls and
other language
double root2 = sqrt(2) facilities.
/ / ...
}
Structured Prgramming
• A technique devised to improve the reliability and clarity of
programs.
• Control of program flow is restricted to three structures, sequence,
IF THEN ELSE, and DO WHILE, or to a structure derivable from a
combination of the basic three.
• A structured program does not need to use GO TOs or branches.
• Sequence, Selection and Iteration
Structured Programming
Write moderately complex programs with an ease.
Characterized by stand-alone subroutines, local variables, rich control
constructs, and in general, less usage/no usage of GOTO.
Examples: C (Structured), C++, Java (Structured + OOP)
Organization of programs around the code, "code acting on data."
Example: a program written in a structured language such as C is defined by its
functions, any of which may operate on any type of data used by the program.
Modular Programming
• A set of related procedures with the data they manipulate is often
called a module.
• Decide which modules you want; partition the program so that
data is hidden within modules.
[1] Provide a user interface for the stack (e.g., functions push () and
pop ()).
[2] Ensure that the representation of the stack (e.g., an array of
elements) can be accessed only through this user interface.
[3] Ensure that the stack is initialized before its first use.
Example -Modularity
namespace Stack { // interface namespace Stack { // implementation
void push (char ); const int max _ size = 200 ;
char pop (); char v [max _ size];
} int top = 0;
void push (char c ) { /* check for
void f () // Use of the functions overflow and push c */ }
{ char pop () { /* check for underflow
Stack :: push (´c ´); and pop */ }
if (Stack :: pop () != ´c ´) }
error ("impossible ");
}
Example with Separate Compilation
#include "stack.h " // get the
namespace Stack { #include "stack .h " // interface
// interface get the interface namespace Stack { //
void push (char ); void f () representation
char pop (); { const int max _ size = 200 ;
Stack :: push (´c ´); char v [max _ size ];
}
if (Stack :: pop () != int top = 0 ;
´c ´) }
error ("impossible "); void Stack :: push (char c ) { /*
} check for overflow and push c */
}
char Stack :: pop () { /* check for
underflow and pop */ }
The key point about this Stack module is that the user code is insulated from the data
representation of Stack by the code implementing Stack :: push () and Stack :: pop()
Independent Modules
Object-Oriented Programming Concepts
Object-oriented programming:
Structured programming + several new concepts (encapsulation, abstraction,
generic programming, etc.)
Object-oriented programs are organized around data
Controlled access to data.
Define the data and the routines that are permitted to act on that data.
A data type defines what sort of operations can be applied to that data.
Encapsulation
Binding of code and the data it manipulates.
No outside interference and misuse, thus safe.
The linking of code and data together forms an object.
Within object, data, code or both private to that object or public.
Private data/code can be accessible only by another part of the object, but
not accessible to (a piece of code) from outside the object.
Public data/code can be accessible within object as well as outside the
object.
An object is a variable of a user-defined type.
Example - Encapsulation
#include<iostream>
using namespace std; // main function
class sample { int main()
private: {
// data is hidden from outside world sample obj;
int num;
public: [Link](50);
// function to set value of variable num
void set(int x) { cout<<[Link]();
num =x; } return 0;
// function to return value of variable num }
int get() {
return num;
}
};
Abstraction
Data abstraction is one of the important features of object
oriented programming.
Abstraction in simple words
For a particular functionality, what is to be done is known to the user
but how it is done is not known to the user.
Ex. If you would like send an email to your friend, then you write an
email and simply click Send button and an email gets sent to your
friend (What part)
How an email mail was sent (How part) is hidden from you. The
message is prepared in the format desired by the underlying network
before the message is actually sent.
Example- abstraction
void display(){
#include <iostream> public:
#include <string> cout<<"EmpId = "<<EId<<"\tName
setSalary(int Id, string name, =
using namespace std; double b, double a1, double
"<<Ename<<endl;
class employee{ a2)
cout<<"Employee Salary =
int EId; { "<<salary;
string Ename; EId =Id; }
double salary, basic, allowance1, Ename = name; };
allowance2; basic =b;
int main()
double computeSalary(int EId) { allowance1=a1;
{
salary = basic + allowance1+ allowance2=a2;
employee E1,E2;
allowance2; computeSalary(EId);
[Link](1, “Mahesh”, 50000,
return salary; } 10000, 5000);
} [Link]();
}
Output: EmpId=1, Name:Mahesh
Salary: 65000
Polymorphism
One interface, multiple methods
Ex. A thermostat.
No matter what type of furnace our house has (gas, oil, electric, etc.),
the thermostat works the same way.
The thermostat - same interface
Furnace - method
Programming:
A stack of integer values, character values, and one for floating-point
values.
One set of names, push( ) and pop( ), used for all 3 types of stacks.
Operators too can be overloaded : + used for all integers, floats, etc.
Operators used for user defined types.
Types: Compile time and run time.
Inheritance
One object can acquire the properties of another object.
Concept of classification - hierarchical classifications.
Ex. a Red Delicious apple - a part of apple, which is a part of the fruit class, which is a part of
the larger class food.
Without the use of classifications, each object would have to define explicitly all of its
characteristics.
An object need only define those qualities that make it unique within its class.
It is the inheritance mechanism that makes it possible for one object to be a specific instance
of a more general case.
Ex. Employee class - A general employee, special employee
Application: Code reuse
Generic Programming
• An algorithm can be expressed independently of representation
details.
• The programming paradigm is:
• Decide which algorithms you want; parameterize them so that
they work for a variety of suitable types and data structures.
• Containers
• A generalization of a stack of characters type to a stack of
anything type by making it a template and replacing the specific
type char with a template parameter.
Example
template <class T > class Stack { The template <class T > prefix
T* v; makes T a parameter of the
declaration it prefixes.
int max _ size ; The member functions might be
int top ; defined similarly:
public : template <class T > void Stack
<T >:: push (T c )
class Underflow { }; {
class Overflow { }; if (top == max _ size ) throw
Stack (int s ); // constructor Overflow ();
v[top] = c ;
~Stack (); // destructor top = top + 1 ;
void push (T ); }
T pop ();
};
Example Cont..
template <class T > T Stack <T>:: pop ()
{
if (top == 0 ) throw Underflow ();
top = top- 1;
return v[top];
}
Stack<char > sc ; // stack of characters
Stack <complex > scplx ; // stack of complex numbers
A Sample C++ Program
#include <iostream> // I/O operations, no “.h” with a Standard C++
using namespace std; // std - Standard C++ library included
A compiler directive
int main() { Note: void as an argument is not needed unlike C
int i;
cout << "This is output.\n"; // this is a single line comment
/* We can still use C style comments */
// input a number using >>
cout << "Enter a number: ";
cin >> i;
// now, output a number using <<
cout << i << "Square of i is " << i*i << "\n";
return 0; }
I/O Operators
#include <iostream>
using namespace std;
int main() {
cout and cin are objects of ostream and
float f; istream classes in C++.
char str [80];
double d;
cout << "Enter two floating point numbers: ";
cin >> f >> d;
cout << "Enter a string: ";
cin >> str;
cout << f << " " << d << " " << str;
return 0;
}
References
C++: The Complete Reference, 4th Edition by Herbert
Schildt , McGraw-Hill
Teach Yourself C++ 3rd Edition by Herbert Schildt,
The C+ + Programming Language, Third Edition by
Bjarne Stroustrup, Addison Wesley