Oodp Unit - I
Oodp Unit - I
Unit - 1
Comparison between procedural and object oriented
programming
float marks;
public:
get_details();
Operations/ Member function
show_details();
};
2. Objects
•The wrapping up of data and functions into a single unit is known as encapsulation.
•The data is not accessible to the outside world and only those functions which are
Wrapped in the class can access it.
•The insulation of the data from direct access by the program is called data hiding or
Information hiding.
4. Data Abstraction
•Abstraction refers to the act of representing essential features without including the
background details and explanations.
5. Inheritance
The inheritance is the process by which objects of one class acquire the properties of objects of
another class.
Derived class/
Child/ Sub class
Scooter Motor Bike Auto Rickshaw Car Bus
Types of Inheritance
• Single level
• Multi level
• Multiple
• Hierarchical
• Hybrid
6. Polymorphism
• Ability to take more than one form.
• It includes
Function Overloading
Operator Overloading
7. Function Overloading
• Function Overloading refers to use of same function
name for different purposes. These functions differ from
each other by no. and types of arguments.
Draw()
Inheritance
composition
11. Genericity
• To reduce code duplication and generate short simpler code c+
+ supports the use of generic codes (or templates) to define the
same code for multiple data types.
• Data Security.
• Reusability of existing code.
• Creating new data types.
• Abstraction.
• Less development time.
• Reduce Complexity.
• Better Productivity.
Differences between C and C++
SYNTAX
cout << Expression << Expression . . . ;
• You must insert spaces inside the quotes if you want them in
the output.
Syntax for input statements
SYNTAX
cin >> Variable >> Variable . . . ;
cin >> x;
cin >> y;
• We can use the << operator multiple times in the same line.
This is called cascading.
• Similar to cout, cin can also be cascaded. For example,
Reading and Writing Characters and Strings
string name;
getline(cin, name);
cout<“Welcome “<<name;
Formatted Input and Output Operations
Formatting with flags
simple structured
address
float double long double
pointer reference
What is a Variable?
Types of C constant:
Numeric Constant
Integer constants ex. 10 20 -30
Real or Floating point constants ex 10.34 -90.30 0.6e4
Character Constant
Character constants ex. ‘3’ ‘a’
String constants ex.”Chennai” “preethi”
Type Conversions
• Concept of converting the value of one type into
another type.
• Two types of conversion
– Implicit Conversion
• Conversion done automatically
• Loss of data will be there, no control in our side.
– Explicit Conversion
• We have control of converting it from one type to another.
• We can prevent loss of data.
int main()
{
int a;
double b=12.5;
a=b;
Implicit Type Conversion
cout<<a;
a=int (b);
Explicit Type Conversion
cout<<a;
}
Explicit type conversion
int main()
Output
{ a=25
b=35.87
int a = 25; Converts int to float = 25.00
Converts float to int = 35
float b = 35.87;
cout<<“a=“<<a;
cout<<“b=“<<b;
cout<<“ converts int to float”<<float(a);
cout<<“converts float to int”<<int(b);
return 0;
}
Pointers
• A pointer is a variable whose value is the address of
another variable.
• type *var-name;
• int *ip; // pointer to an integer
• double *dp; // pointer to a double
• float *fp; // pointer to a float
• char *ch // pointer to character
(a) We define a pointer variable.
(b) Assign the address of a variable to a pointer.
(c) Finally access the value at the address available in the
pointer variable.
Classes
Access specifier
• Access modifiers or Access Specifiers in a class are used to
set the accessibility of the class members.
• There are 3 types of access modifiers available in C++
– Public
– Private
– Protected
Public - The members declared as Public are accessible from
outside the Class through an object of the class.
Protected - The members declared as Protected are accessible
from outside the class BUT only in a class derived from it.
Private - These members are only accessible from within the
class. No outside Access is allowed.
Member Functions in Classes
• { • fun2();
• ….. • }
Nested Member function
• A member function can be called by using its name inside another
member function of the same class is known as nesting of member
functions.
class rectangle
{
private:
float length;
float breadth;
public:
void get_data() { cin>>length>>breadth;}
voidshow_data()
{cout<<“length”<<length<<“breadth”<<breadth<<“area=“<<area(); }
int area()
{ return length*breadth;}
};
Inline Member Function
• A member function that is defined inside its class member list is called
an inline member function. Member functions containing a few lines of code
are usually declared inline.
class rectangle
{
private:
float length;
float breadth;
public:
void get_data() { cin>>length>>breadth;}
void show_data();
};
inline void rectangle :: show_data(void)
{
cout<<“length”<<length<<“breadth”<<breadth;
}
Pointers
• int main()
• {
• int *pnum
• char *pch;
• float *pfnum;
• double *pdnum;
• cout<<sizeof(pnum);
• cout<<sizeof(pch);
• cout<<sizeof(pfnum);
• cout<<sizeof(pdnum);
Pointers
• int main()
• { Output
8
• int *pnum 8
• char *pch; 8
8
• float *pfnum;
• double *pdnum;
• cout<<sizeof(pnum);
• cout<<sizeof(pch);
• cout<<sizeof(pfnum);
• cout<<sizeof(pdnum);
Dereferencing Pointers
int main()
{
int num,*pnum;
pnum=#
cout<<“Enter the number”;
cin>>num;
cout<<*pnum;
cout<<#
}
Dereferencing Pointers
int main()
{
int num,*pnum;
pnum=#
cout<<“Enter the number”; Output
cin>>num; Enter the number : 15
15
cout<<*pnum; FFDC
cout<<#
}
Rules for Pointer Operations
• A pointer variable can be assigned the address of another
variable (of the same type).
• A pointer variable can be assigned the value of another
pointer variable (of the same type).
• A pointer variable can be initialized with a NULL (or 0) value.
• Prefix and postfix increment and decrement operators can be
applied on a pointer variable.
• An integer value can added or subtracted from a pointer
variable.
• A pointer variable cannot be multiplied by a constant.
• A pointer variable cannot be added to another pointer
variable.
• A pointer variable can be compared with another pointer
variable of the same type using relational operators.
Example Program for Pointers
• #include <iostream>
• using namespace std;
• int main()
• {
• int p,*pv=NULL;
• int *pv2=0,*pv3=0;
• cin>>p;
• pv=&p;
• cout<<*pv<<endl;
• cout<<pv;
• pv2=pv;
• cout<<*pv2<<endl;
Example Program for Pointers
• #include <iostream>
• using namespace std;
• int main()
• {
• int p,*pv=NULL; //Rule 3 pointer variable INITIALIZED to NULL
• int *pv2=0,*pv3=0; // Rule 3 pointer variable initialized to zero
• cin>>p;
• pv=&p; //"1.Pointer variable assigned to the address of another variable“
• cout<<*pv<<endl;
• cout<<pv;
• pv2=pv; //pv2 is assigned with the address of p
• //"2.Pointer variable assigned to the value of another ptr variable“
• cout<<*pv2<<endl;
• cout<<"4.Pointer variable can use ++and -- operators"<<pv2--<<endl;
• cout<<"4.Pointer variable can use ++and -- operators"<<*pv2--<<endl;
• cout<<"4...Pointer variable can use ++and -- operators"<<++pv<<endl;
• cout<<"5... Added or subtracted from the pointer variable"<<pv+5<<endl;
• cout<<"5... Added or subtracted from the pointer variable"<<pv-3<<endl;
• cout<<"6... multiplied to a constant"<<*pv**pv2<<endl;
• const int z=5;
• cout << pv2*z; //is not possible
• cout<<pv+pv2; //is not possible
• return 0;
• }
Null Pointer
• Null pointer which is a special pointer value that is known not
to point anywhere.
• Null pointer does not point to any valid memory address.
Class:
71
• Static data member is declared using the static keyword.
• There is only one copy of the static data member in the
class. All the objects share the static data member.
• The static data member is always initialized to zero when
the first class object is created.
Syntax:
static data_type datamember_name;
Here
static is the keyword.
data_type – int , float etc…
datamember_name – user defined
72
Static Data Member
Rectangle r1;
class Rectangle Rectangle r2;
{ Rectangle r3;
private:
int width;
int length; count
• Used to
– access the values of the data members (accessor)
– perform operations on the data members
(implementor)
• Are declared inside the class body
• Their definition can be placed inside the class
body, or outside the class body
• Can access both public and private members of
the class
• Can be referred to using dot or arrow member
access operator
74
Member function
keyword Class Name
class Rectangle
{ int main()
private: {
int width, length; Rectangle r2; Object creation
public: r1.set(5,8); Inside the
void set (int w, int l); int x=r1.area(); function
member
int area() cout<<"x value in r1 "<<x;
function
{ r2.set(4,9);
inside the class return width*length; x=r2.area();
} class name cout<<"x value in r2 "<<x;
}r1; Member function }
Object void Rectangle :: set (int w, int l)
creation {
width = w; outside the class
length = l;
}
scope operator
Const Member Function
• The const member functions are the functions which are declared as constant in
the program.
• The object called by these functions cannot be modified.
• It is recommended to use const keyword so that accidental changes to object
are avoided.
• A const member function can be called by any type of object.
• Note: Non-const functions can be called by non-const objects only.
User defined
Syntax: keyword
public :
void Write ( ) const ;
function definition
};
77
What is an object?
OBJECT
set of methods
Operations (member functions)
78
Object:
• Objects are instances of class, which holds the data variables declared in
class and the member functions work on these class objects.
• Each object has different data variables.
• Objects are initialized using special class functions called Constructors.
• Destructor is special class member function, to release the memory reserved
by the object.
class Rectangle
main()
{ {
private: Rectangle r1;
Rectangle r2;
int width;
int length; r1.set(5, 8);
public: cout<<r1.area()<<endl;
80
Another Example
81
Declaration of an Object
class Rectangle
{
private:
int width;
int length; main()
{
public: Rectangle r1;
void set(int w, int l); r1.set(5, 8);
int area(); }
};
r1
width = 5
length = 8
void Rectangle :: set(int w, int l)
{
width=w;
length=l;
}
82
Declaration of an Object
int area();
r1 r2
}; width = 8
5
length = 10
8 5000
???
5000 6000
83
Object Initialization
1. By Assignment
#include <iostream.h>
• Only work for public data
members
class circle
{ • No control over the operations
public: on data members
double radius;
};
int main()
{
circle c1; // Declare an instance of the class circle
c1.radius = 5; // Initialize by assignment
84
Object Initialization
2. By Public Member Functions
#include <iostream.h>
class circle
{
private:
double radius;
public:
void set (double r)
{radius = r;}
double get_r ()
{return radius;}
};
int main(void) {
circle c; // an object of circle class
c.set(5.0); // initialize an object with a public member function
cout << "The radius of circle c is " << c.get_r() << endl;
// access a private data member with an accessor
}
85
UML diagram Introduction
• Unified Modeling Language (UML) is a general purpose
modelling language. The main aim of UML is to define a
standard way to visualize the way a system has been designed.
• UML is not a programming language, it is rather a visual
language. We use UML diagrams to portray the behavior and
structure of a system.
Diagrams in UML can be broadly classified as:
• Structural Diagrams – Capture static aspects or structure of
a system. Structural Diagrams include: Component Diagrams,
Object Diagrams, Class Diagrams and Deployment Diagrams.
• Behavior Diagrams – Capture dynamic aspects or behavior
of the system. Behavior diagrams include: Use Case Diagrams,
State Diagrams, Activity Diagrams and Interaction Diagrams.
Object Oriented Concepts Used in UML
• The top name compartment holds the class name, other general properties of
the class, such as attributes, are in the middle compartment, and the bottom
compartment holds a list of operations.
Person
marriedTo
Class Interface notation
• Class interface notation is used to describe the externally visible
behavior of a class; for example, an operation with public visibility.
• The UML notation for an interface is a small circle with the name of
the connected to the class.
• A class that requires the operations in the interface may be
attached to the circle by a dashed arrow .
• The dependent class is not required to actually use all of the
operations.
• For example, a person object may need to interact with the
BankAccount object to get the balance; this relationship is depicted
with UML class interface notation.
Person BankAccount
Object Diagram
• A static object diagram is an instance of a class
diagram.
• It shows a snapshot of the detailed state of
the system at a point in time.
• Notation is the same for an object diagram
and a class diagram.
Object Notation
• The object is represented in the same way as the
class. The only difference is the name which is
underlined.
• As the object is an actual implementation of a class,
which is known as the instance of a class. Hence, it
has the same usage as the class.
Output
X=3938 (garbage value)
Default Constructors
• Default constructor is the constructor which doesn’t
take any argument. It has no parameters.
Output
a: 10
b: 20
Parameterized Constructors
Output
Mean value: 32.5
Friend Class
Output
Sum:55
Use Case diagram
• The purpose of use case diagram is to capture the dynamic aspect of a
system.
• Used for describing a set of user scenarios
• Mainly used for capturing user requirements
• Work like a contract between end user and software developers.
• Use case Core components
• Actors: A role that a user plays with respect to the system, including
human users and other systems. e.g.,inanimate physical objects (e.g. robot);
an external system that needs some information from the current system.
• Use case: A set of scenarios that describing an interaction between a user
and a system, including alternatives.
Borrow
Employee
Client
Order Title
Fine Remittance
Supervisor
(TogetherSoft, Inc)
Class and objects
• class student "<<endl;
• { • cin >> total;
• private: • perc=total/500*100;
• char name[30]; • }
• int rollNo; • //member function definition, outside of the
• int total; class
• float perc; • void student::putDetails(void){
• public: • cout << "Student details:\n";
• //member function to get student's details • cout << "Name:"<< name<<endl << "Roll
• void getDetails(void); Number:" << rollNo <<endl<< "Total:" <<
total<<endl << "Percentage:" << perc<<endl;
• //member function to print student's details
• }
• void putDetails(void);
• };
• int main()
• //member function definition, outside of the
class • {
• void student::getDetails(void){ • student std; //object creation
• cout << "Enter name: " <<endl; • std.getDetails();
• cin >> name; • std.putDetails();
• cout << "Enter roll number: "<<endl; • return 0;
• cin >> rollNo; • }
• cout << "Enter total marks outof 500:
Output
Enter name: Ram
Enter roll number: 123
Enter total marks out of 500: 455
Student details:
Name: Ida
Roll Number:123
Total:455
Percentage:91
GCD of two numbers
Output
62 8
GCD of 62 and 8 is 2