0% found this document useful (0 votes)
5 views

C++ Programming Unit-1(ii) - UPDATED

The document provides an overview of Object-Oriented Design and Programming (OODP) with a focus on C++ features, including encapsulation, abstraction, inheritance, and polymorphism. It explains fundamental concepts such as classes, objects, access specifiers, and member functions, along with their syntax and examples. The document serves as a foundational guide for understanding OOP principles and their implementation in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C++ Programming Unit-1(ii) - UPDATED

The document provides an overview of Object-Oriented Design and Programming (OODP) with a focus on C++ features, including encapsulation, abstraction, inheritance, and polymorphism. It explains fundamental concepts such as classes, objects, access specifiers, and member functions, along with their syntax and examples. The document serves as a foundational guide for understanding OOP principles and their implementation in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

OBJECT ORIENTED DESIGN

AND PROGRAMMING (OODP)


(CODE: 21CSC101T)
2nd Semester, B. Tech
2024-2025
UNIT-1(ii)

Dr. Bapuji Rao


Assistant Professor
Department of CSE(CORE)
UNIT – 1(ii)
2

 Features of C++
 C++ Objects

SRM IST, DELHI-NCR CAMPUS


 Abstraction
 Encapsulation
 Access Specifiers
 Member Functions (Methods)
 Object as Argument in a Member Function
 UML Diagram Introduction, Use Case Diagram, Class Diagram

DR. BAPUJI RAO, Department of CSE(CORE)


OBJECT-BASED Vs OBJECT-ORIENTED
3

OBJECT-BASED OBJECT-ORIENTED
PROGRAMMING LANGUAGE PROGRAMMING LANGUAGE

SRM IST, DELHI-NCR CAMPUS


1) Supports Encapsulation, Data Abstraction, 1) Supports all the fundamental features of
Operator Hiding, Automatic Initialization OOP.
and Cleaning Up Objects.
2) It has built-in Objects. 2) It does not have built-in Objects.
3) Examples: JavaScript, VB(Visual Basic) 3) Examples: C++, Java, C#
NOTE: Python is a Object-Based and Object-Oriented Programming Language because it
allows developers the freedom to choose the paradigm that best fits their needs.

DR. BAPUJI RAO, Department of CSE(CORE)


FUNDAMENTAL FEATURES OF OOP
4

 ENCAPSULATION
 DATA ABSTARCTION

SRM IST, DELHI-NCR CAMPUS


 INHERITANCE
 POLYMORPHISM
 MESSAGE PASSING
 DELEGATION
 GENERICITY
 EXTENSIBILITY
 PERSISTENCE (not supported by C++)

DR. BAPUJI RAO, Department of CSE(CORE)


FUNDAMENTAL FEATURES OF OOP
5

 Encapsulation:
• The wrapping up of data and functions into a single unit is called as Encapsulation.

 Data Abstraction:

SRM IST, DELHI-NCR CAMPUS


• It is a mechanism that associates the code and the data it manipulates and keeps
them safe from external interface and misuse.
• It is the act of representing essential features without including the background
details or explanations.
• Classes use the concept of abstraction and are defined a set of abstract
attributes such as size and data type and functions to operate these attributes.
• Example:
class Declaration
DR. BAPUJI RAO, Department of CSE(CORE)
FUNDAMENTAL FEATURES OF OOP
6

 Inheritance:
• It is the process of one class acquires the properties of another class.
• This concept is the idea of reusability. It means the additional features can be added to an

SRM IST, DELHI-NCR CAMPUS


existing class without modifying it.
• It is possible by deriving a new class from an existing class. So, the new class will have the
combined features of both the class.
• Examples:
Single Inheritance
Multi-level
Multiple
Hierarchical
Multipath Inheritance
DR. BAPUJI RAO, Department of CSE(CORE)
FUNDAMENTAL FEATURES OF OOP
7

 Polymorphism:
• The word polymorphism is derived from the Greek meaning many forms.

SRM IST, DELHI-NCR CAMPUS


• It allows a single name to be used for more than one related purpose, which
are technically different.
• Examples:
Function Overloading
Operator Overloading
Dynamic Binding

DR. BAPUJI RAO, Department of CSE(CORE)


FUNDAMENTAL FEATURES OF OOP
8

 Message Passing:
• It is a communication technique that allows different parts of a program or

SRM IST, DELHI-NCR CAMPUS


different programs to communicate by sending messages.
• It is the process by which objects communicate by exchanging messages in
the form of method or function calls.
• Examples:
Abstract Class
Inheritance

DR. BAPUJI RAO, Department of CSE(CORE)


FUNDAMENTAL FEATURES OF OOP
9

 Delegation:
• It means using the object of another class as a class member of another

SRM IST, DELHI-NCR CAMPUS


class.
• The delegation allows to use the properties of the particular class that is
required in the class.
• Example:
Containership / Container Class / Has-a-relationship

DR. BAPUJI RAO, Department of CSE(CORE)


FUNDAMENTAL FEATURES OF OOP
10

 Genericity:
• It allows the declaration of data items without specifying their exact data type.

SRM IST, DELHI-NCR CAMPUS


 Examples:
• Function Template
• Class Template

DR. BAPUJI RAO, Department of CSE(CORE)


FUNDAMENTAL FEATURES OF OOP
11

 Extensibility:
• The extension of the functionality of the existing software components.
• In other words, adding new functionality by inheriting new data types from the

SRM IST, DELHI-NCR CAMPUS


common base class.
 Examples:
• Dynamic Binding
• Abstract Class

 Persistence:
• The object outlives the execution time and exists between execution of a program
is known as persistence.

DR. BAPUJI RAO, Department of CSE(CORE)


What is OOP?
12

 Definition: It is a programming methodology that associates data structures

SRM IST, DELHI-NCR CAMPUS


with a set of operations which act upon it.
 Data structures are called attributes.
 An attribute is a property that associate with an object.
 Attribute serves to describe the object and holds some value that is required
for processing.

DR. BAPUJI RAO, Department of CSE(CORE)


CHARACTERISTICS OF OBJECT ORIENTED
LANGUAGES
13
OBJECTS
• Every object will have data structures called attributes and
behaviors called operations.
• An object is a self-contained unit that consists of both data and the

SRM IST, DELHI-NCR CAMPUS


Examples: methods that operate on that data.
Human Entities User-Defined Data Types Collection of Data Data Storage Constructs
Students Time A Personal File Customized Arrays
Employees Complex Number An Inventory Stacks
Customers Angles A Dictionary Queues
Sales Person Points of the Plane Sales Person Linked Lists, Binary Trees
Physical Objects Elements of the Computer Environment
Vehicles in a Traffic-Control System Windows
Aircraft in Air Traffic Control System Menus
Electrical Components in Circuit Graphics Objects(Lines, Rectangles, Circles)
Design Program Mouse, Keyboard, Printer, Disk Drives
DR. BAPUJI RAO, Department of CSE(CORE)
CHARACTERISTICS OF OBJECT ORIENTED
LANGUAGES
14

CLASS
• The class mechanism in object-oriented languages provides a way to
create objects.

SRM IST, DELHI-NCR CAMPUS


• A class is a design that can be reused any number of times to create
objects.
• A class serves as a plan, or blueprint. It specifies what data and what
functions will be included in objects of that class.
• A class definition doesn’t create any objects. It is just like existence of
data type in the program.
• A class is a description of a number of similar objects.
• An object is often called an "instance" of a class.
DR. BAPUJI RAO, Department of CSE(CORE)
C++ CLASS
15

 A class encloses both the data and function that operate on the data into a single unit.
 The variables and functions enclosed in a class are called Data members and

SRM IST, DELHI-NCR CAMPUS


Member functions.
 Member function defines the permissible operation on the data members of a
class.
 The permissible operation is achieved with the help of access specifiers (or access
modifiers).
 So, a class creates a user-defined data type.
 The size of a class data type is the total sizes of all the data members.

DR. BAPUJI RAO, Department of CSE(CORE)


SYNTAX OF CLASS
DEFINITION / DECLARATION / SPECIFICATION 16

class Class_Name

SRM IST, DELHI-NCR CAMPUS


{
Access Specifier (Modifier):
// Data Member(s) Declaration

Access Specifier (Modifier):


// Member Function(s) Declaration
(or)
// Member Function(s) Definition
};

DR. BAPUJI RAO, Department of CSE(CORE)


ACCESS SPECIFIERS / ACCESS MODIFIERS
17

 PRIVATE:
• In general, data members are declared as private specifier.
• Once the data members are declared as private, it becomes completely hidden

SRM IST, DELHI-NCR CAMPUS


from the outside of the world i.e., from the main program.
 PUBLIC:
• In general, member functions are declared as public specifier.
• The private specifier data members are only accessible through the public
specifier member functions.
 PROTECTED:
• The protected specifier data members are not visible to the main program and it
just acts as private specifier data members.
• The protected data members of the base class are only visible to the member
functions of the derived class.
• It plays an important role in inheritance programming.
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
18

class Person class Person


{ {
private:
char name[20], gender; char name[20], gender;

SRM IST, DELHI-NCR CAMPUS


int age; int age;
public: public:
void Assign(char n[ ], char g, int a) void Assign(char n[ ], char g, int a)
{ {
strcpy(name, n); OR strcpy(name, n);
gender = g; gender = g;
age = a; age = a;
} }
void Show( ) void Show( )
{ {
cout<<name<<" "<<gender<<" "<<age<<endl; cout<<name<<" "<<gender<<" "<<age<<endl;
} }
}; };
Note: The size of data type Person is 20 + 1 + 2 = 23 bytes.
DR. BAPUJI RAO, Department of CSE(CORE)
C++ OBJECT
19

 The process of declaration of variables of class data type is known as class


instantiation.

SRM IST, DELHI-NCR CAMPUS


 Such variables are called as objects.
 The object consists of all the data members of the class.
 So, the size of the object is the size of the class itself.

DR. BAPUJI RAO, Department of CSE(CORE)


SYNTAX OF OBJECT DECLARATION
20

 Syntax-1:
class Class_Name
{

SRM IST, DELHI-NCR CAMPUS


private:
// Data Member(s) Declaration
public:
// Member Function(s) Declaration (or) Definition
} Object-1, Object-2,………………..;

 Syntax-2:
class Class_Name Object-1, Object-2, ………...;

 Syntax-3:
Class_Name Object-1, Object-2, …………….....;
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
21

class Person // class Person OBJ, SRM;


{
char name[20], gender; // Person OBJ, SRM;
int age;

SRM IST, DELHI-NCR CAMPUS


public:
void Assign(char n[ ], char g, int a)
{ name gender age
strcpy(name, n);
OBJ
gender = g;
age = a; 20 bytes 1 byte 2 bytes
}
void Show( )
{ name gender age
cout<<name<<" "<<gender<<" "<<age<<endl; SRM
}
} OBJ, SRM; 20 bytes 1 byte 2 bytes

 Note:
The size of objects OBJ and SRM are 23 bytes each. DR. BAPUJI RAO, Department of CSE(CORE)
MEMBER ACCESS OPERATOT ( . [DOT] )
22

 The data members and member functions of a class are accessed through its
object with DOT ( . ) operator.

SRM IST, DELHI-NCR CAMPUS


 Syntax of Accessing of Data Member:
• Object_Name.Data_Member
 Syntax of Accessing of Member Function:
• Object_Name.Member_Function(Argument(s) List, If any)

DR. BAPUJI RAO, Department of CSE(CORE)


EXAMPLE
23

#include<iostream.h> void main( )


class Person {
{ Person OBJ, SRM;
char name[20], gender;

SRM IST, DELHI-NCR CAMPUS


OBJ.Assign("XYZ", 'M', 20);
int age;
public: SRM.Assign("PQR", 'F', 30);
void Assign(char n[ ], char g, int a) OBJ.Show( );
{
strcpy(name, n); SRM.Show( );
gender = g; }
age = a;
} name gender age
void Show( ) OBJ XYZ\0 M 20
{
cout<<name<<" "<<gender<<" "<<age<<endl; name gender age
}
SRM PQR\0 F 30
};
DR. BAPUJI RAO, Department of CSE(CORE)
ACCESS SPECIFIERS EXAMPLE
24

a b c
class PrivateProtected
{ obj 10 20 30
private:
void main( )
int a;

SRM IST, DELHI-NCR CAMPUS


{
protected:
PrivateProtected obj;
int b;
public: obj.a = 10; // Invalid
int c; obj.b = 20; // Invalid
void Assign(int p, int q)
{ obj.c = 30; // Valid
a = p; cout<<"A="<<obj.a<<"B="<<obj.b; // Invalid
b = q;
} cout<<"C="<<obj.c; // Valid
void Show( ) obj.Assign(10, 20, 30);
{
cout<<"A="<<a<<"B="<<"C="<<c<<endl; obj.Show( );
} }
}; DR. BAPUJI RAO, Department of CSE(CORE)
MEMBER FUNCTION DEFINITION IN A CLASS
(INLINE MEMBER FUNCTION DEFINITION) 25

• The definition of the member function is given inside the class declaration.
• Syntax:

SRM IST, DELHI-NCR CAMPUS


class Class_Name
{
private:
// data member(s);
public:
Return_Type Function_Name(Formal Argument(s) Declaration, if any)
{
// statement(s);
}
};

DR. BAPUJI RAO, Department of CSE(CORE)


EXAMPLE
26

// Sum of Two Numbers using Inline Member Function


#include<iostream.h>
class TwoNumberSum
{

SRM IST, DELHI-NCR CAMPUS


float a, b;
public:
// inline member functions definitions void main( )
void Assign(float m, float n) {
{ TwoNumberSum A;
a = m; A.Assign(4.5, 10);
b = n; A.Sum();
} }
void Sum( )
{
float c = a + b;
cout<<"Sum of "<<a<<" and "<<b<<" = "<<c<<endl;
}
}; DR. BAPUJI RAO, Department of CSE(CORE)
MEMBER FUNCTION DEFINITION IN A CLASS
(OUTLINE MEMBER FUNCTION DEFINITION) 27

• The prototype or declaration of member function is given inside the class declaration;
where as the definition is given outside the class declaration.
• Syntax:

SRM IST, DELHI-NCR CAMPUS


class Class_Name
{
private:
// data member(s);
public:
Return_Type Function_Name(Formal Argument(s) Declaration, if any);
};

Return_Type Class_Name::Function_Name(Formal Argument(s) Declaration, if any)


{
// statement(s); Scope Resolution Operator
}
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
28

// Sum of Two Numbers using Outline Member Function // outline member function definition
#include<iostream.h> void TwoNumberSum::Assign(float m, float n)
{
class TwoNumberSum a = m;

SRM IST, DELHI-NCR CAMPUS


{ b = n;
float a, b; }
public:
// outline member function definition
// outline member function declaration or prototype void TwoNumberSum::Sum( )
void Assign(float , float); // void Assign(float m, float n); {
float c = a + b;
void Sum(); cout<<"Sum of "<<a<<" and "<<b<<" = "<<c<<endl;
}; }

void main( )
{
TwoNumberSum A;
A.Assign(4.5, 10);
A.Sum();
}
DR. BAPUJI RAO, Department of CSE(CORE)
QUESTION-1
29

SRM IST, DELHI-NCR CAMPUS


Write a C++ program to create a class named 'Student' with a string variable 'name'
and an integer variable 'roll_no'. Assign the value of roll_no as '7' and that of name as
"SRM" by creating an object of the class Student with a function named 'Assign( )'.
Then display the contents of the object with second function named 'Display( )'.

DR. BAPUJI RAO, Department of CSE(CORE)


QUESTION-2
30

SRM IST, DELHI-NCR CAMPUS


Write a C++ program to print the area and perimeter of a triangle having sides of 3, 4
and 5 units by creating a class named 'Triangle' with a function to print the area and
perimeter named 'Result( )'. The three sides are passed as parameters to the function
named 'Assign( )'.

DR. BAPUJI RAO, Department of CSE(CORE)


QUESTION-3
31

SRM IST, DELHI-NCR CAMPUS


Write a C++ program to print the area of two rectangles having sides (4, 5) and (7, 8)
respectively by creating a class named 'Rectangle' with a function named 'Area( )'
which returns the area. The Length and breadth are passed as parameters to the
function named 'Assign( )'.

DR. BAPUJI RAO, Department of CSE(CORE)


QUESTION-4
32

SRM IST, DELHI-NCR CAMPUS


Write a C++ program to create a class named 'Student' with a string variable 'name'
and an integer variable 'roll_no'. Create two objects named 'Stud1' and 'Stud2'. Input
data into those objects and display its contents with two functions named 'Input( )' and
'Display( )'.

DR. BAPUJI RAO, Department of CSE(CORE)


QUESTION-5
33

SRM IST, DELHI-NCR CAMPUS


Write a C++ program to print the area and perimeter of a triangle by creating a class
named 'Triangle' with function named 'Input( )' to input three sides and print the area
and perimeter with second function named 'Result( )'.

DR. BAPUJI RAO, Department of CSE(CORE)


1-D ARRAY AS DATA MEMBER IN A CLASS
34

Assign {7, 1, 4, 3, 10, 66, 67} in an array and display it using class and object.
#include<iostream.h>
class Array

SRM IST, DELHI-NCR CAMPUS


// Member Function Definition
{
private: // void Array::Assign (int *b)
int a[7]; void Array::Assign (int b[ ])
public: {
for (int i=0; i<7; i++)
// void Assign(int *); // prototype
a[i] = b[i];
void Assign (int [ ]); // prototype }
void Show(); // prototype
};

DR. BAPUJI RAO, Department of CSE(CORE)


// member function definition
35
void Array::Show ( )
{
cout<<"All 7 Numbers\n";
for (int i=0; i<7; i++)

SRM IST, DELHI-NCR CAMPUS


cout<<a[i]<<" ";
cout<<endl;
}
void main ( )
{
Array obj;
int num[ ]={7, 1, 4, 3, 10,66, 67};
obj.Assign (num);
obj.Show( );
}
DR. BAPUJI RAO, Department of CSE(CORE)
Write a C++ program to read 10 integers in an array. Then
display the numbers, sum, and average using class and object.
36

// outline member function Read( ) definition


void Array::Read ( )
{
#include <iostream.h> for (int i=0; i<10; i++)

SRM IST, DELHI-NCR CAMPUS


class Array {
{ cout<<"Enter a Number : ";
private: cin>>a[i];
int a[10]; }
public: }
// outline member functions prototypes
// outline member function Show( ) definition
void Read( ); void Array::Show ( )
void Show( ); {
cout<<"All 10 Numbers"<<endl;
void Result( ); for (int i=0; i<10; i++)
}; cout<<a[i]<<" ";
cout<<endl;
}
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
37

// outline member function Result( ) definition


void main ( )
void Array::Result ( )
{
{

SRM IST, DELHI-NCR CAMPUS


for (int i=0, sum=0; i<10; i++) Array obj;
sum = sum + a[i]; obj.Read( );
float avg = sum/10.0;
obj.Show( );
cout<<"Sum : "<<sum<<endl;
obj.Result( );
cout<<"Average : "<<avg<<endl; }
}

DR. BAPUJI RAO, Department of CSE(CORE)


Write a C++ program to read 'n' numbers in an array. Then
display the numbers, sum, and average using class and object.
38

// outline member function Read( ) definition


#include <iostream.h>
void Array::Read ( )
class Array
{
{
for (int i=0; i<n; i++)
private:

SRM IST, DELHI-NCR CAMPUS


{
int n;
cout<<"Enter a Number : ";
float a[100];
cin>>a[i];
public:
}
void Size( ) //inline member function definition
}
{
cout<<"How many numbers to be input ? "; // outline member function Show( ) definition
cin>>n; void Array::Show ( )
} {
// outline member functions prototypes cout<<"All "<<n<<" Numbers"<<endl;
void Read( ); for (int i=0; i<n; i++)
void Show( ); cout<<a[i]<<" ";
void Result( ); cout<<endl;
}; }
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
39

// outline member function Result( ) definition


void main ( )
void Array::Result ( )
{
{
Array obj;

SRM IST, DELHI-NCR CAMPUS


float sum = 0;
for (int i=0; i<n; i++) obj.Read( );
sum = sum + a[i];
obj.Show( );
float avg = sum/n;
obj.Result( );
cout<<"Sum : "<<sum<<endl;
}
cout<<"Average : "<<avg<<endl;
}

DR. BAPUJI RAO, Department of CSE(CORE)


POINTER AS DATA MEMBER IN A CLASS
40
Assign {3, 1, 4, 7, 66, 67} in a pointer and display it using class and object.

// void Pointer::Assign(int *b)


#include<iostream.h> void Pointer::Assign (int b[ ])

SRM IST, DELHI-NCR CAMPUS


class Pointer {
{ // memory allocation to pointer data member 'a'
private: a = new int[6];
int *a; for (int i=0; i<6;i++)
public:
a[i] =b[i]; // *(a+i) =*(b+i);
// member functions prototype
}
// void Assign(int *);
void Assign(int [ ]);
void Show();
};

DR. BAPUJI RAO, Department of CSE(CORE)


Continue…
41
// member function definition
void Pointer::Show ( ) // main program
{ void main( )
cout<<"All 6 Numbers\n"; {
for (int i=0; i<6; i++) Pointer obj;

SRM IST, DELHI-NCR CAMPUS


cout<<a[i]<<" "; // cout<<*(a+i)<<" "; int num[ ]={3, 1, 4, 7, 66, 67};
cout<<endl;
obj.Assign(num);
// to free the allocated memory
delete a; obj.Show( );
} }

DR. BAPUJI RAO, Department of CSE(CORE)


2-D ARRAY AS DATA MEMBER IN A CLASS
42

Write a C++ Program to read rXc numbers in a matrix. Then display it using class and object.
#include<iostream.h>
#include<iomanip.h>

SRM IST, DELHI-NCR CAMPUS


#include<conio.h> // outline member function Read( ) definition
class matrix void matrix::Read( )
{ {
int r, c; for (int i=0;i<r;i++)
float a[10][10]; {
public: for (int j=0; j<c; j++)
void Size( ) {
{ cout<<"Enter a Number : ";
cout<<"Enter number of rows and columns of the Matrix: "; cin>>a[i][j];
cin>>r>>c; }
} }
void Read( ); // outline member function prototype
void Show( ); // outline member function prototype
};
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
43

// outline member function Show( ) definition void main( )


void matrix::Show( ) {
{ matrix obj;
for (int i=0;i<r;i++)

SRM IST, DELHI-NCR CAMPUS


{ obj.Size( );
for (int j=0; j<c; j++) obj.Read( );
cout<<setw(4)<<a[i][j];
cout<<endl; cout<<"The Matrix\n";
} obj.Show( );
} }

DR. BAPUJI RAO, Department of CSE(CORE)


PASSING OBJECT(S) AS ARGUMENT(S) IN
A MEMBER FUNCTION 44

 An object is passed in a member function in three different ways.


• PASS-BY-VALUE
o A copy of the object is passed to the member function.

SRM IST, DELHI-NCR CAMPUS


o Any modification is made to the object inside the member function is
not reflected in the object used in the calling function.

 Syntax(Object as Arguments with No Return Object):


class class_name
{ private:
// data member(s);
public:
void function_name(class_name object-1, class_name object-2, ……)
{
// statement(s);
}
};
DR. BAPUJI RAO, Department of CSE(CORE)
Program: Addition of two times.
45

#include<iostream.h> void Add(Time A, Time B)


class Time {
{ ss = A.ss + B.ss;

SRM IST, DELHI-NCR CAMPUS


int hh, mm, ss; mm = A.mm + B.mm + ss/60;
public: ss = ss%60;
void Get( )
hh = A.hh + B.hh + mm/60;
{
cin>>hh>>mm>>ss; mm = mm%60;
} }
void Show( ) };
{
cout<<hh<<" : "<<mm<<" : "<<ss<<endl;
}

DR. BAPUJI RAO, Department of CSE(CORE)


Program: Addition of two times.
46

void main( )
{
Time T1, T2, T3;

SRM IST, DELHI-NCR CAMPUS


cout<<"Enter 1st times Hour, Minute, and Second : ";
T1.Get( );
cout<<"Enter 2nd times Hour, Minute, and Second : ";
T2.Get ();
T3.Add(T1, T2);
cout<<"1st Time : ";
T1.Show( );
cout<<"2nd Time : ";
T2.Show( );
cout<<"Addition of Times : ";
T3.Show( );
}

DR. BAPUJI RAO, Department of CSE(CORE)


PASSING OBJECT(S) AS ARGUMENT(S)
IN A MEMBER FUNCTION WITH 47
OBJECT AS RETURN VALUE
 Syntax(Object as Arguments with Object As Return Value):
class class_name

SRM IST, DELHI-NCR CAMPUS


{
private:
// data member(s);
public:
class_name function_name(class_name object-1, class_name object-2, ……)
{
// statement(s);
return Object;
}
};

DR. BAPUJI RAO, Department of CSE(CORE)


Program: Multiplication of matrices mat1 = {{1, 2}, {3, 4}}
and mat2 = {{3, 1}, {4, 7}}. 48

// outline member function’s definition


void Matrix::Get (int a[2][2])
#include <iostream.h> {
#include <iomanip.h>

SRM IST, DELHI-NCR CAMPUS


for (int i=0, j; i<2; i++)
// Size of class Matrix = (2X2)X2 bytes = 8bytes for (int j=0; j<2; j++)
class Matrix mat[i][j] = a[i][j];
{ }
int mat[2][2];
public: void Matrix::Display ( )
// outline member function’s prototype {
void Get (int [][]); for (int i=0; i<2; i++)
{
void Display( ); for (int j=0; j<2; j++)
Matrix Multiply(Matrix); cout<<setw(4)<<mat[i][j];
cout<<endl;
};
}
}
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
49

Matrix Matrix::Multiply(Matrix A)
{
Matrix B;

SRM IST, DELHI-NCR CAMPUS


for (int i=0, j, k; i<2; i++)
for (j=0; j<2; j++)
{
B.mat[i][j] = 0;
for (k=0; k<2; k++)
B.mat[i][j] = B.mat[i][j] + mat[i][k]*A.mat[k][j];
}
return B;
}

DR. BAPUJI RAO, Department of CSE(CORE)


Continue…
50

void main ( )
{
int mat1[][2] = {{1, 2}, {3, 4}} ;

SRM IST, DELHI-NCR CAMPUS


int mat2[][2] = {{3, 1}, {4, 7}};
Matrix obj1, obj2, res;
obj1.Get(mat1);
obj2.Get(mat2);
res = obj1.Multiply(obj2);
cout<<"1st Matrix\n";
obj1.Dsiplay ( );
cout<<"2nd Matrix\n";
obj2.Dsiplay ();
cout<<"Matrix Multiplication\n";
res.Dsiplay ( );
}
DR. BAPUJI RAO, Department of CSE(CORE)
PASSING OBJECT(S) AS ARGUMENT(S) IN
A MEMBER FUNCTION 51

• PASS-BY-REFERENCE
o The value object is passed to the member function.

SRM IST, DELHI-NCR CAMPUS


o Any changes are made to the object inside the member function, that is
directly reflected in the actual object.

 Syntax:
class class_name
{ private:
// data member(s);
public:
return_type function_name(class_name &object-1, class_name &object-2, ……)
{
// statement(s);
}
};
DR. BAPUJI RAO, Department of CSE(CORE)
Program: Add arr1 = {1, 3, 4, 7, 67} and arr2 = {2, 5, 6, 7, 1}
and store in arr2 using reference objects in a member 52
function.
void Array::Assign(int b[])
{
for (int i=0; i<5; i++)

SRM IST, DELHI-NCR CAMPUS


#include <iostream.h> a[i] = b[i];
class Array }
{
int a[5]; void Array::Change(Array &obj)
public: {
for (int i=0; i<5; i++)
void Assign(int []);
obj.a[i]=obj.a[i] + a[i];
void Change(Array &); }
void Show( ); void Array::Show( )
}; {
for (int i=0; i<5; i++)
cout<<a[i]<<" ";
cout<<endl;
} DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
53

void main ( )
{
int arr1[] = {1, 3, 4,7, 67};

SRM IST, DELHI-NCR CAMPUS


int arr2[] = {2, 5, 6, 7, 1};
Array A, B;
A.Assign(arr1);
B.Assign(arr2);
cout<<"1st Array Elements\n";
A.Show ();
cout<<"2nd Array Elements\n";
B.Show ( );
A.Change(B);
cout<<"The 2nd Array Elements After change \n";
B.Show ( );
}

DR. BAPUJI RAO, Department of CSE(CORE)


PASSING OBJECT(S) AS ARGUMENT(S) IN
A MEMBER FUNCTION 54

• PASS-BY-ADDRESS / POINTER
o An address of the object is passed to the member function.

SRM IST, DELHI-NCR CAMPUS


o Any changes are made to the object inside the member function, that is
directly reflected in the actual object.

 Syntax:
class class_name
{ private:
// data member(s);
public:
return_type function_name(class_name *object-1, class_name *object-2, ……)
{
// statement(s);
}
};
DR. BAPUJI RAO, Department of CSE(CORE)
Program: Add arr1 = {1, 3, 4, 7, 67} and arr2 = {2, 5, 6, 7, 1}
and store in arr2 using pointer objects in a member function. 55

void Array::Assign(int b[])


{
for (int i=0; i<5; i++)

SRM IST, DELHI-NCR CAMPUS


#include <iostream.h> a[i] = b[i];
class Array }
{
int a[5]; void Array::Change(Array *obj)
public: {
for (int i=0; i<5; i++)
void Assign(int []);
obj→a[i]=obj→a[i] + a[i];
void Change(Array *); }
void Show( ); void Array::Show( )
}; {
for (int i=0; i<5; i++)
cout<<a[i]<<" ";
cout<<endl;
} DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
56

void main ( )
{
int arr1[] = {1, 3, 4,7, 67};

SRM IST, DELHI-NCR CAMPUS


int arr2[] = {2, 5, 6, 7, 1};
Array A, B;
A.Assign(arr1);
B.Assign(arr2);
cout<<"1st Array Elements\n";
A.Show ();
cout<<"2nd Array Elements\n";
B.Show ( );
A.Change(&B);
cout<<"The 2nd Array Elements After change \n";
B.Show ( );
}

DR. BAPUJI RAO, Department of CSE(CORE)


Unified Modeling Language ( UML )
57

 INTRODUCTION
• Unified Modeling Language (UML) is used for documentation of the systems and

SRM IST, DELHI-NCR CAMPUS


programs.
• UML is the standard for documenting OBJECT-ORIENTED SYSTEMS (OOS).
• UML provides a pictorial or graphical notation for documenting the artefacts such as
classes, objects and packages that make up an object-oriented system.
• Grady Booch was one of the first researchers to publish work on the Object-Oriented
Design (OOD) of Ada programs.
• James Rumbaugh developed the Object Modeling Technique (OMT).
• Ivar Jacobson developed the Object Oriented Software Engineering (OOSE) approach.

DR. BAPUJI RAO, Department of CSE(CORE)


Unified Modeling Language ( UML )
58

 UML diagrams can be divided into THREE categories.


 Structure Diagrams:
• It shows the static architecture of the system irrespective of time.

SRM IST, DELHI-NCR CAMPUS


• For example, structure diagrams for a university system may include
diagrams that depict the design of classes such as Student, Faculty, etc.
 Behaviour Diagrams:
• It depicts the behaviour of a system or business process.
• For a university system, a possible behaviour diagram would show how a
student registers for a course.
 Interaction Diagrams:
• It shows the methods, interactions and activities of the objects.
DR. BAPUJI RAO, Department of CSE(CORE)
STRUCTURE DIAGRAMS
59

SRM IST, DELHI-NCR CAMPUS


CLASS COMPONENT PACKAGE DEPLOYMENT
DIAGRAM DIAGRAM DIAGRAM DIAGRAM

COMPOSITE STRUCTURE OBJECT


DIAGRAM DIAGRAM

DR. BAPUJI RAO, Department of CSE(CORE)


BEHAVIOUR DIAGRAMS
60

SRM IST, DELHI-NCR CAMPUS


ACTIVITY DIAGRAM STATE MACHINE DIAGRAM

USE CASE DIAGRAM

DR. BAPUJI RAO, Department of CSE(CORE)


CLASS DIAGRAMS
61

 It is useful both for abstract modelling and for designing the actual programs.
 It provides a graphic notation for modelling classes and their relationships, thereby describing possible
objects.

SRM IST, DELHI-NCR CAMPUS


 A class is represented by a box, which is divided into three rectangles.
 The name of the class is given in the top rectangle.
 The attributes are shown with their names and types in the second rectangle.
 The third rectangle shows the methods with their return types and parameters (names and types).
 The access specifier is prefixed by the field name and operation / method name.
 The three types of access specifiers are – (minus), + (plus) and # (hash).
 – (minus) symbol indicates private access, + (plus) symbol indicates public access and # (hash)
symbol indicates protected access.
DR. BAPUJI RAO, Department of CSE(CORE)
CLASS DIAGRAMS
62

 Class Name:
Class Name Mandatory
• Every class must have a unique name.
• Classes are used to describe a part of the real world, or part of

SRM IST, DELHI-NCR CAMPUS


the system under design. Attribute-1
Attribute-2
 Attributes: --------------- Optional
• A class can have attributes which represent useful information ---------------
about instances of a class. Attribute-N
• Each attribute has a type.

 Operations: Operation-1
• Operations describe what can be done with the instances of a Operation-2
--------------- Optional
class.
---------------
• Each operation has a signature, which specifies the types of its Operation-N
parameters and the type of the value it returns (if any).

DR. BAPUJI RAO, Department of CSE(CORE)


EXAMPLE
63

Person
- name : string

SRM IST, DELHI-NCR CAMPUS


- gender : char
- age : int
+ Assign(n : string , g : char , a : int) : void
+ Show( ) : void

DR. BAPUJI RAO, Department of CSE(CORE)


OBJECT DIAGRAMS
64

 The UML symbol for an object is a box with an object name followed by a

SRM IST, DELHI-NCR CAMPUS


colon and the class name.
 The object name and class name are both underlined.
 Examples:
• Objects OBJ, SRM, and an anonymous person are instances of class Person.
OBJ:Person SRM:Person :Person

DR. BAPUJI RAO, Department of CSE(CORE)


OBJECT'S ATTRIBUTES and VALUES
65

 Each attribute name is unique within a class.


 Each attribute has a value for each object.

SRM IST, DELHI-NCR CAMPUS


 A value is a piece of data.
 Examples:
• name, gender, and age are attributes of Person objects.
OBJ:Person SRM:Person :Person

name = "XYZ" name = "PQR" name = "ABC"


gender = 'M' gender = 'F' gender = 'T'
age = 20 age = 30 age = 40

DR. BAPUJI RAO, Department of CSE(CORE)


USE CASE DIAGRAMS
66

 It describes how a system interacts with outside actors.


 It is the graphical representation of the interaction among the elements and systems.
 It identifies the functionality of the system.

SRM IST, DELHI-NCR CAMPUS


 It represents a user’s interaction with the system and depicts the specification of the
use case.
 A use case diagram contains four components.
 System Boundary: It defines the system of interest in relation to the world around it.
 Actor: It could be a human or a piece of software or hardware.
 Use Case: It describes a certain piece of desired functionality of an application system.
 Relationship: It is the relationship between the actor and use case.
DR. BAPUJI RAO, Department of CSE(CORE)
USE CASE DIAGRAMS
67

Sl. No. Name Notation Description


System Name  Use cases are placed inside the system boundary.
1. System Boundary

SRM IST, DELHI-NCR CAMPUS


Use Case Name
 It describes the desired functionality of the system.
2. Use Case

 It could be a human or a piece of software or hardware


3. Actor
Actor Name interacts with the system.
 It shows the sequence of actions between the actor and
4. Association use case.
 A generalization relationship is used to represent
5. Generalization
inheritance relationship between elements of same type.

DR. BAPUJI RAO, Department of CSE(CORE)


USE CASE DIAGRAMS
68

Sl. No. Name Notation Description


 An include relationship specifies how the behaviour of the
<<Include>> inclusion use case is inserted into the behaviour defined
6. Include

SRM IST, DELHI-NCR CAMPUS


for the base use case.

 An include relationship specifies how the behaviour of the


7. Extends <<Extends>> extension use case is inserted into the behaviour defined
for the base use case.

8. Constraint  Shows a condition exist between actor and use case.


{Condition}

 It combines a group of classes.


9. Package Package
Name

DR. BAPUJI RAO, Department of CSE(CORE)


USE CASE DIAGRAMS
69

Sl. No. Name Notation Description


 It is used to connect between the packages and use case.
10. Interface

SRM IST, DELHI-NCR CAMPUS


 It is used to write note for the use case.
11. Note Note

12. Anchor  It is used to connect between the use case and note.

DR. BAPUJI RAO, Department of CSE(CORE)


MOBILE BILL MANAGEMENT SYSTEM
70

MOBILE BILL
MANAGEMENT SYSTEM

SRM IST, DELHI-NCR CAMPUS


Handle Message

Actor
Mobile Phone Handle Call

Actors
Bill Mobile Company
Management

Customer

System Boundary Association


Use Cases
DR. BAPUJI RAO, Department of CSE(CORE)
ATM SYSTEM
71

ATM SYSTEM

Check Balance

SRM IST, DELHI-NCR CAMPUS


Cash Deposit

Customer
Cash
Withdrawal
Bank
Money Transfer

Maintenance
Technician

Repair

DR. BAPUJI RAO, Department of CSE(CORE)


ONLINE FOOD ORDERING SYSTEM
72

ONLINE FOOD ORDERING SYSTEM


Search
Restaurant

SRM IST, DELHI-NCR CAMPUS


View Menu

Process Order
Customer Place Order

Manage
Make Payment
Inventory

Company
Receive Order Manage
Payments

Restaurant Prepare Order

Notify
Customer
DR. BAPUJI RAO, Department of CSE(CORE)
GENERALIZATION
73

 A generalization relationship is used to represent inheritance relationship


between elements of same type.

 Example-1:

SRM IST, DELHI-NCR CAMPUS


• The child use case inherits the behaviour and meaning of the parent use case.
• The child may add to or override the behaviour of its parent.

Parent

Child

DR. BAPUJI RAO, Department of CSE(CORE)


GENERALIZATION
74

 Example-2:
• The Graduate use case inherits the behaviour and meaning of the Registration

SRM IST, DELHI-NCR CAMPUS


use case.
• The Under Graduate use case inherits the behaviour and meaning of the
Registration use case.

Registration

Graduate Under Graduate


DR. BAPUJI RAO, Department of CSE(CORE)
GENERALIZATION
75

 Example-3:
• The actor Order Registry Clerk can instantiate the general use case Place
Order.

SRM IST, DELHI-NCR CAMPUS


• The Place Order can also be specialized by the use cases Phone Order or
Internet Order.

Place Order

Order Registry
Clerk

Phone
Internet Order
Order
Internet Customer
Customer

DR. BAPUJI RAO, Department of CSE(CORE)


PATIENT REGISTRATION PROCESS
76
Patient Registration Process

SRM IST, DELHI-NCR CAMPUS


DR. BAPUJI RAO, Department of CSE(CORE)
SRM IST, DELHI-NCR CAMPUS
77

THANK YOU

You might also like