Professional Documents
Culture Documents
18CS202J Object Oriented Design and Programming: Unit - 1
18CS202J Object Oriented Design and Programming: Unit - 1
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
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.
STRUCTURE OF C++ PROGRAM
Header files
SYNTAX
cout << Expression << Expression . . . ;
• You must insert spaces inside the quotes if you want them in
the output.
SYNTAX
cin >> Variable >> Variable . . . ;
cin >> x;
cin >> y;
simple structured
address
float double long double
pointer reference
C++ Primitive Data Types
Primitive types
integral floating
unsigned
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.
• Smaller type converted into wider type eg: int to float.
– 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
int a = 25; b=35.87
Converts int to float = 25.00
float b = 35.87; Converts float to int = 35
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.
• Write a program to swap two numbers without
using third 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()
• { Output
• int *pnum 2
2
• char *pch; 2
• float *pfnum; 2
• 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”; 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.
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.
int *ptr=NULL
• You can check whether a given pointer variable stores address of
some variable or contains a null
If (ptr==NULL)
{
Sts block;
}
• You may also initialize a pointer as a NULL pointer by using a
constant 0.
int ptr;
ptr=0;
Pointers to Pointers
• C++ use pointers that point to pointers. The pointers in turn
point to data (or even to other pointers).
• To declare pointers to pointers add an asterisk (*) for each
level of reference.
int x=10;
int *px;
x px ppx
int **ppx;
10 1002 2004
px=&x;
1002 2004 4008
ppx=&px;
Cout<<**ppx;
Constant Pointer
• A constant pointer is a pointer that cannot change the
address it is storing.
• Once assigned the address of a variable the constant
pointer cannot be made to point to any other variable.
• The value at the address pointed by the constant
pointer may be changed.
datatype *const ptr_var = init address
• Char *const ptr = &c;
• *ptr =‘B’
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
• Class – A class defines the blue print i.e. structure and
functions of an object.
• Objects – Objects help us to decompose large systems and
help us to modularize our system.
• An object is the fundamental unit (building block) of a system
which is used to depict an entity.
• Inheritance – Inheritance is a mechanism by which child
classes inherit the properties of their parent classes.
• Abstraction – Mechanism by which implementation details
are hidden from user.
• Encapsulation – Binding data together and protecting it from
the outer world is referred to as encapsulation.
• Polymorphism – Mechanism by which functions or entities
are able to exist in different forms.
UML Diagrams
UML includes the following nine diagrams
• Class diagram
• Object diagram
• Use case diagram
• Sequence diagram
• Collaboration diagram
• Activity diagram
• Statechart diagram
• Deployment diagram
• Component diagram
The building blocks of UML can be defined as
• Things
• Relationships
• Diagrams
Things
Things are the most important building blocks of
UML. Things can be −
• Structural
• Behavioral
• Grouping
• Annotational
Structural Things
• Structural things define the static part of the model. They represent the physical and conceptual
elements.
• Class − Class represents a set of objects having similar responsibilities.
• Interface − Interface defines a set of operations, which specify the responsibility of a class.
• Use case −Use case represents a set of actions performed by a system for a specific goal.
• Node − A node can be defined as a physical element that exists at run time.
Behavioral Things
• Interaction − Interaction is defined as a behavior that consists
of a group of messages exchanged among elements to
accomplish a specific task.
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
• This is beneficial when the size of the data members is not the
same each time the program is executed.
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=(float)total/500*100;
• char name[30]; • }
• int rollNo; • //member function definition, outside of
• int total; the class
• float perc; • void student::putDetails(void){
• public: • cout << "Student details:\n";
• //member function to get student's • cout << "Name:"<< name<<endl << "Roll
details Number:" << rollNo <<endl<< "Total:"
• void getDetails(void); << total<<endl << "Percentage:" <<
perc<<endl;
• //member function to print student's • }
details
• void putDetails(void);
• }; • int main()
• //member function definition, outside of • {
the class • student std; //object creation
• void student::getDetails(void){ • std.getDetails();
• cout << "Enter name: " <<endl; • std.putDetails();
• cin >> name; • return 0;
• cout << "Enter roll number: "<<endl; • }
• 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