21csc101t - Oodp Unit 1 PPT - Updated
21csc101t - Oodp Unit 1 PPT - Updated
21csc101t - Oodp Unit 1 PPT - Updated
UNIT 1
Access Specifiers
•
Object-Oriented Programming in C++, 4th Edition by Robert Lafore The
C++ Programming Language, 4th Edition by Bjarne Stroustrup
9.5
The next evolution in programming came with the idea of replacing binary code for
instruction and addresses with symbols or mnemonics.
Because they used symbols, these languages were first known as symbolic
languages.
The set of these mnemonic languages were later referred to as assembly languages.
The assembly language for our hypothetical computer to replace the machine
9.7
Over the years, various languages, most notably BASIC, COBOL, Pascal, Ada, C,
C++, and Java, were developed.
Interpretation
Some computer languages use an interpreter to translate the source program into the
object program.
Interpretation refers to the process of translating each line of the source program into
the corresponding line of the object program and executing the line.
9.11
12
Today, computer languages are categorized according to the approach they use to
solve a problem.
A paradigm, therefore, is a way in which a computer language looks at the problem
to be solved.
We divide computer languages into four paradigms: procedural, object-oriented,
9.13
functional and declarative. Figure summarizes these.
9.18
04/09/2024The Object
components ofOriented Design andprogram
a procedural
Glimpse on Procedural
Programming
Procedural Programming
Using function
Function & program is divided into modules
Every module has its own data and function which can be called by other
modules.
Some Procedural Languages
FORTRAN (FORmula TRANslation)
Pascal
C
9.21 Ada
We encounter many active objects in our daily life: a vehicle, an automatic door, a
dishwasher and so on.
The action to be performed on these objects are included in the object: the objects
need to only receive the appropriate stimulus from outside to perform one of the
actions.
The program in this paradigm just sends the corresponding request to the object.
The concept of an object-oriented paradigm
Mind Map on C Programming
A Better C
C++
Improves on many of C's features
Has object-oriented capabilities
• Increases software quality and reusability
Developed by Bjarne Stroustrup at Bell Labs
• Called "C with classes“ an enhanced version of C
Superset of C
• Can use a C++ compiler to compile C programs
• Gradually evolve the C programs to C++
Hybrid language
26
C-like style
Object-oriented style
BothTowards better programming….
Easier debugging
30 classes can be tested independently
reused objects have already been tested
I am going to walk
(method)
31
• In real life, things that you see such as cars, trees, cats, mobile phones and so
on are objects. Even, you as a student is object.
41
EXAMPLE 2
42
A Siamese cat
EXAMPLE 3
How about the following cats?
•They are 3 different cats in which two of them are of the same type.
•What makes them different?
their states, behaviors and identities
43
EXAMPLE 4:
45
car
WHAT CAN BE CONCLUDED FROM
EX 3 - 5 ?
46
Basic Terminology
object
usually a person, place or thing (a noun)
method
an action performed by an object (a verb)
attribute
The state of a bank account includes its account number and its current balance
The behaviors associated with a bank account include the ability to make deposits
and withdrawals
Class Object
a thing, both tangible
a kind of template.
and intangible.
represents the common is comprised of data &
structure & behaviour operations that
shared by the same type. manipulate these data.
A collection of objects is called an instance of
of similar type. a class.
must be defined before
creating an instance of
the class.
CLASS VS OBJECT
Object ,Object and Object
A student, a professor
A desk, a chair, a classroom, a building
A university, a city, a country
A subjects such as Physics, Math, History, …
52
53
Abstraction Encapsulation
54
Inheritance Polymorphism
Abstraction and Encapsulation
56
Inheritance and class hierarchy
Capability to express the inheritance relationship which makes it ensure closeness with
real world models.
Idea of reusability.
It allows addition of additional features to an existing class without modifying it.
A subclass defines only those features which are unique to it.
Inheritance Example(cont.)
Person
Student Employee
Ex : Addition operation
Operator Overloading
Function Overloading
Polymorphism
LEARN()
Message passing
• Objects communicate with each other
• E.g employee.salary(name)
• Information to be sent.
Student Class
62
/* First C++ Program */ /*...*/ comments are used for the documentation to
understand the code to others. These comments are ignored by the compiler
main() It is an entry point of all the function where program execution
begins.
Curly Braces {...}It is used to group all statements together.
If using namespace std is placed into the program then it does not required to
write std:: throughout the code. Namespace std contains all the classes,
objects and functions of the standard C++ library.
Data Types
A type defines a set of values and a set of operations that can be applied on those
values. The set of values for each type is known as the domain for the type.
Data types in C++ is mainly divided into two types:
Primitive Data Types: These data types are built-in or predefined data types and can
be used directly by the user to declare variables. Primitive data types includes:
Integer
Character
Boolean
Floating Point
Double Floating Point
Valueless or Void
Wide Character
Also : String
Abstract or user defined data type: These data types are defined by user itself. Like,
defining a class in C++ or a structure.
Data Types
Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of
memory space and ranges from -2147483648 to 2147483647.
Character: Character data type is used for storing characters. Keyword used for character
data type is char. Characters typically requires 1 byte of memory
Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable
can store either true or false. Keyword used for boolean data type is bool.
Floating Point: Floating Point data type is used for storing single precision floating point
values or decimal values. Keyword used for floating point data type is float. Float variables
typically requires 4 byte of memory space.
Double Floating Point: Double Floating Point data type is used for storing double precision
floating point values or decimal values. Keyword used for double floating point data type is
double. Double variables typically requires 8 byte of memory space.
void: Void means without any value. void data type represents a valueless entity. Void data
type is used for those function which does not returns a value.
Wide Character: Wide character data type is also a character data type but this data type has
size greater than the normal 8-bit datatype. Represented by wchar_t. It is generally 2 or 4
bytes long.
Data Types
Datatype Modifiers: As the name implies, datatype modifiers are used with the
built-in data types to modify the length of data that a particular data type can
hold.
• A constant declaration specifies the type, the name and the value
of the constant.
• A variable declaration specifies the type, the name and possibly
the initial value of the variable.
Variables are used to store values that can be changed during the
program execution.
A variable is best thought of as a container for a value.
Syntax:
< type > < identifier >;
< type > < identifier > = < expression>;
Examples:
int sum; //single declaration
float p,q,r; //multiple declaration
char answer = 'y';
double temperature = -3.14;
Variable declarations
• A variable has a type and it can contain only values of that type.
For example, a variable of the type int can only hold
integer values.
• Variables are not automatically initialized.
For example, after declaration
int sum;
the value of the variable sum can be anything (garbage).
#include <iostream>
using namespace std;
int main ()
{
// Variable definition:
int a, b;
int c;
float f;
// actual initialization
a = 10;
b = 20;
c = a + b;
f = 70.0/3.0;
cout<<c<<endl<<f;
return 0;
}
cin and the Extraction Operator >>
Example:
cin >> a;
cin >> b >> c;
cin >> x;
cin >> my-character; 82
cout and the insertion Operator <<
When the static keyword preceded the declaration of a variable, that variable becomes a
static variable.
A static keyword serves a different purpose depending on where it has been declared.
• If it is declared inside of a class, all instances of the class (i.e. objects) will share the
same copy of the variable, and we can access it without any object by class name
outside the class.
• If it is declared inside of a function then its value is preserved between successive
calls for the lifetime of the program, but it cannot be accessd outside the scope or
function in which it is declared.
84
• Syntax
• static <type> <variable_name>;
• Static –
These variables holds their value between function calls.
#include <iostream.h>
using namespace std;
int main()
{
Using the static storage class specifier on a variable that normally would
be automatic makes the variable static
Can use the static storage class specifier with variables declared inside
functions
#include <iostream>
using namespace std;
This proves that the
void increase(){ static variable inside a
static int num = 0;
cout << ++num << endl; function is declared only
} once on the first function
call and the remaining
int main(){ successive function calls
increase();
87
increase(); use the same copy.
return 0;
}
#include<iostream> }
using namespace std;
int main() {
void func() { cout << "Calling Function :- " <<
static int count = 0; endl;
/* value of 'count' persist between func();
function calls cout << "Calling Function :- " <<
* i.e count doesn't become 0 endl;
every time func() is called; func();
* previous value of count cout << "Calling Function :- " <<
remains alive endl;
*/ func();
count++; return 0;
cout << "Function is called " << }
count << " times " << endl;
Static Variable
89 • when we want all the objects to maintain a single copy of the class variable.
• The C++ compiler converts all operands upto the type of the largest
operand, which is called type promotion.
datatype (expression);
Type Conversions
#include <iostream>
using namespace std;
void main(){
int a;float b,c;
cout << "Enter the value of a:";
cin >> a;
cout << "Enter the value of b:";
cin >> b;
c = float(a)+b;
cout << "The value of c is:" << c;
Type Cast Operator
C++ permit explicit type conversion of variables or expressions using the type cast
operator.
o (type-name) expression // C notation
continue…
p = int * ( q ); // is illegal
p = ( int * ) q; // is legal
Accessing a global variable when there is a local variable with same name
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Sizeof() Operators
Control Flow
Lab Exercise – Work Order #1
2. Prashanth was sick and writes a letter to his teacher stating that he is suffering from fever
with a temperature of 100.5 F, His teacher asks him to mention the temperature in Celsius.
Can you please help Prashanth ?
3. Greet your friend based on the Time input from the him/her.[using conditional statement]
4. Get the dateofbirth of your friend and find his lucky number (sum of all digits in the dob)
10
5
5. I have a magic number – 7365 , can you find the digit with highest value
6. Can you find the place value of the numbers 4 in 234, 456, 645
… … 100 … 1024 …
intege
pointer
r
Pointer Types
Pointer
C++ has pointer types for each type of object
Pointers to int objects
Pointers to char objects
Pointers to user-defined objects
(e.g., Student, Complex)
Even pointers to pointers
Pointers to pointers to int objects
Pointer Variable
The "address of " operator (&) gives the memory address of the
variable
Usage: &variable_name
… … 100 … … …
a
int a = 100;
//get the value,
cout << a; //prints 100
//get the memory address
cout << &a; //prints 1024
Address Operator &
… 88 100 … … …
a b
#include <iostream>
using namespace std;
Result is:
void main(){ The address of a is: 1020
int a, b; The address of b is: 1024
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
Pointer Variables
… 88 100 … 1024 …
a p
int a = 100; Result is:
int *p = &a; 100 1024
cout << a << " " << &a <<endl; 1024 1032
cout << p << " " << &p <<endl;
The value of pointer p is the address of variable a
A pointer is also a variable, so it has its own memory address
Dereferencing Operator *
… 88 100 … 1024 …
a p
int a = 100;
int *p = &a; Result is:
cout << a << endl; 100
cout << &a << endl; 1024
cout << p << " " << *p << endl; 1024 100
cout << &p << endl; 1032
Pointer Example
#include <iostream>
using namespace std;
int main (){
int value1 = 5, value2 = 15;
int *p1, *p2;
p1 = &value1; // p1 = address of value1
p2 = &value2; // p2 = address of value2
*p1 = 10; // value pointed to by p1=10
*p2 = *p1; // value pointed to by p2= value // pointed to by
p1
p1 = p2; // p1 = p2 (pointer value copied)
*p1 = 20; // value pointed to by p1 = 20
cout << "value1==" << value1 << "/ value2==" << value2;
return 0;
}
Pointer with array name Example
#include <iostream>
using namespace std;
Result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4
Reference Variables
// Program 1 // Program 2
#include <stdio.h> #include <stdio.h>
• Object Class
• Data Abstraction and Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
A class is a three-part containing the class name, data attributes (variables) and the member
functions.
• Class Name
• Member attributes (variables)
• Member functions
Main Points
12 • The Class name is an identifier, which is the user-defined data type name.
4 • The member attributes hold data just like variables which can be accessed by the member
attributes only via particular class/objects.
• The member functions may have productive behaviors or may be designed to do some
operations with member attributes just like normal functions, but the member functions can
be accessed only via the corresponding class or its objects.
•Most of the OOPS concepts are implemented based on classes. Some of them are
Inheritance, Polymorphism, Abstraction, Encapsulation.
12
5
Five values of type int can be declared as an array without having to declare five
different variables (each with its own identifier).
For example, a five element integer array foo may be logically represented as;
type name [elements];
where type is a valid type (such as int, float ...), name is a valid identifier and the
elements field (which is always enclosed in square brackets []), specifies the size of
the array.
Thus, the student array, with five elements of type int, can be declared as:
int student [7];
Character Data Types
Data Type
Description Size Typical Range
(Keywords)
Any single character. It may include a letter, a -128 to 127 or 0 to
char 1 byte
digit, a punctuation mark, or a space. 255
2 or 4
wchar_t Wide character. 1 wide character
bytes
Integer Data Types
Data Type Description Size Typical Range
(Keywords)
-2147483648 to
int Integer. 4 bytes
2147483647
Signed integer. Values may be -2147483648 to
signed int 4 bytes
negative, positive, or zero. 2147483647
-2147483648 to
long Long integer. 4 bytes
2147483647
Signed long integer. Values may be -2147483648 to
signed long 4 bytes
negative, positive, or zero. 2147483647
Data Type
Description Size Typical Range
(Keywords)
n // C++ program to sizes of data types n cout << "Size of unsigned long int : "
n #include<iostream> << sizeof(unsigned int) << " bytes" <<
n using namespace std; endl;
n int main() n cout << "Size of float : " <<
sizeof(float) << " bytes" <<endl;
n {
n cout << "Size of double : " <<
n cout << "Size of char : " << sizeof(double) << " bytes" << endl;
sizeof(char) << " byte" << endl;
n cout << "Size of wchar_t : " <<
n cout << "Size of int : " << sizeof(wchar_t) << " bytes" <<endl;
sizeof(int)
<< " bytes" << endl; n return 0;
n
cout << "Size of short int : " << n }
n sizeof(short int) << " bytes" << endl;
cout << "Size of long int : " <<
n sizeof(long int) << " bytes" << endl;
cout << "Size of signed long int : " <<
sizeof(signed long int) << " bytes" <<
endl;
Variables
Each memory
location in a
computer has an
address.
Although the
addresses are used
Variables are by the computer
Since a variable
names for internally, it is very
holds a data item, it
has a type. memory inconvenient for the
locations. programmer to use
addresses.
A programmer can
use a variable,
such as score, to
store the integer
value of a score
received in a test.
Rules for naming variables in C++
A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9,
and the underscore character.
Variable type can be bool, char, int, float, double, void or wchar_t.
Declarations
n char c;
n i = 10; // initialization
THREE TYPES:
1.GENERAL
1.STATIC
2.CONSTANT
Variable Declaration
n #include <iostream>
n using namespace std;
n int main ()
n {
n // Variable definition:
n int a, b;
n int c;
n float f;
n // actual initialization
n a = 10;
n b = 20;
n c = a + b;
n f = 70.0/3.0;
n cout<<c<<endl<<f;
Static
Static
Static Variables inside Function
Example : without static keyword
Output (without using static
keyword)
Example:with using Static Keyword
Output (with using static keyword)
Constants
Constants
Constants
refer to as
fixed values, Constant
unlike must have to It is
variables be initialized considered
whose value at the time best practice
Constants are Constants can
can be of creating to define
also called be any of the
altered, it, and new constants
literals. data types.
constants - values using only
as the name cannot be upper-case
implies does assigned names.
not change, later to it.
they remain
constant.
Constant Definition in C++
Example:
#include <iostream>
using namespace std;
int main()
{
const int SIDE = 50;
int area;
area = SIDE*SIDE;
cout<<"The area of the square with side: " << SIDE <<" is: " << area <<
endl;
return 0;
}
Constant Definition by using #define preprocessor
Syntax:
#define constant_name;
Example:
#include <iostream>
using namespace std;
#define VAL1 20
#define VAL2 6
#define Newline '\n’
int main()
{
int tot;
tot = VAL1 *
VAL2;
cout << tot;
cout << Newline;
Special Character Constant
Dereferencing Operator *
… 88 100 … 1024 …
a p
int a = 100;
int *p = &a; Result is:
cout << a << endl; 100
cout << &a << endl; 1024
cout << p << " " << *p << endl; 1024 100
cout << &p << endl; 1032
Pointer Sample
int A = 3; Q = &B;
int B; if (P == Q)
int *P = &A; printf(“1\
int *Q = P; n”); if (Q ==
int *R = R)
&B; printf(“2\
n”); if (*P ==
printf(“Enter value:“); *Q)
scanf(“%d”,R); printf(“3\n”);
printf(“%d %d\n”,A,B); if (*Q ==
printf(“%d %d %d\n”, *R) printf(“4\
*P,*Q,*R); n”); if (*P ==
*R) printf(“5\
n”);
Pointer Example
n #include <iostream>
n using namespace std;
n int main (){
n int value1 = 5, value2 = 15;
n int *p1, *p2;
n p1 = &value1; // p1 =
n address of value1
n p2
*p1==&value2;
10; // //value
p2 = pointed to by p1=10
n
address
*p2 = *p1;of value2
// value pointed to by p2= value // pointed to
by p1
n p1 = p2; // p1 = p2 (pointer value copied)
n *p1 = 20; // value pointed to by p1 = 20
n cout << "value1==" << value1 << "/ value2==" << value2;
n return 0;
n
Pointer with array name
Example
#include <iostream>
using namespace std;
return 0;
}
16
1
Pointer with array name Example
#include <iostream>
using namespace std;
void main ()
{
int a[5];
cout << "Address of a[0]: " << &a[0] << endl
<< "Name as pointer: " << a << endl;
}
Result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4
Pointers Detail
Concept Description
C++ Nu C++ supports null pointer, which is a constant with a value of zero defined
l l Pointers in several standard libraries.
C+ + p There are four arithmetic operators that can be used on pointers: ++, --, +,
oin ter -
arithmetic
C++ There is a close relationship between pointers and arrays. Let us check how?
pointers vs
arrays
C+ + You can define arrays to hold a number of pointers.
array of
pointers
C++ C++ allows you to have pointer on a pointer and so on.
pointer to
pointer
Passing Passing an argument by reference or by address both enable the
pointers to passed argument to be changed in the calling function by the
functions called function.
Return C++ allows a function to return a pointer to local variable, static variable
poi nte r and dynamically allocated memory as well.
from
functions 16
3
Null Pointer
• It is always a good practice to assign the pointer NULL to a pointer
variable in case you do not have exact address to be assigned. This is
done at the time of variable declaration. A pointer that is assigned NULL
is called a null pointer.
int main ()
{
int *ptr = NULL;
return 0;
}
16
4
C++ Pointer arithmetic
#include <iostream> Address of var[0] = 0xbfa088b0
Value of var[0] = 10
using namespace std; Address of var[1] = 0xbfa088b4
const int MAX = 3; Value of var[1] = 100
Address of var[2] = 0xbfa088b8
int main () Value of var[2] = 200
{
int var[MAX] =
{10, 100, 200};
int *ptr;
int main ()
{
int var;
int *ptr;
int
**pptr;
var =
3000;
return 0; 16
8
Type Conversions
9.114
Type Casting/ Type Conversion
A type cast is basically a conversion from one type to another.
Implicit Type
Conversion Also In such condition type conversion (type promotion)
known as ‘automatic takes place to avoid lose of data.
type conversion’.
All the data types of the variables are upgraded to the data
type of the variable with largest data type.
#include <iostream>
using namespace std;
int main()
{
int x = 10; // integer x char
y = 'a'; // character c
// y implicitly converted to
int. ASCII
// value of 'a' is 97
x = x + y;
return 0;
Type Casting/ Type Conversion
9.117
C++ program to demonstrate explicit type casting
#include <iostream>
using namespace std;
int main()
{
double x =
1.2;
9.119
Classes in C++
Class Object
A collection of
related variables and Instances of the
functions into a class are called
single structure objects.
175
Class Scope
Outside a
Class data
Within a class's scope,
members and Use dot (.)
member class's scope, class members Use arrow (->)
notation for
functions class members are referenced object and for pointer to
belong to that are references through one references. the object
class's by name. of the handles
scope. on an object.
176
Implementation of a Class
Definitions placed
outside the class
Prototypes
declaration must tell
(declarations) of compiler where the
function members corresponding
declaration/prototype is
syntax
Access Functions and Utility Functions
To create object:
classname objectname;
You can define Functions inside the class as shown in previous slides.
Member functions defined inside a class this way are created as inline functions by default.
When we define a function outside the class we cannot reference them (directly) outside of the
class.
In order to reference these, we use the scope resolution operator, :: (double colon).
Defining Member Function of Class: Outside the class
For the clarity of the code, member functions can be defined outside the class.
Then, the function definition can be defined using scope resolution operator ::.
When we define a function outside the class we cannot reference them (directly)
outside of the class.
}
Object Declaration
c1.setRadius(2.5);
Defining Class
Example
class Circle
{
private: double radius;
public: void setRadius(double r)
{ radius = r; } double
getArea()
{ return 3.14 * radius *
radius; }
};
Example
#include <iostream>
using namespace std;
class Box
{ public:
double length; // Length of a box double
breadth; // Breadth of a box
double height; // Height of a box
};
int main( )
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type
Box
double volume = 0.0; // Store the
volume of a box here
// box 1 specification
Box1.height = 4.0;
Box1.length = 6.0;
Box1.breadth = 3.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth =
12.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
Example for memberfunction inside the
class
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void)
{
return length * breadth * height;
}
};
Example program for member function outside the class:
#include <iostream>
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
Example Continuing..
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
Output:
Volume of Box1: 210
Volume of Box 2: 1560
Access Specifiers
Access-specifiers in C++
194
Access-specifiers in C++
Can be accessed
only by other Can be accessed Needed only when
by other parts of inheritance is
members of the
the program involved.
class.
195
EXAMPLE
#include <iostream>
using namespace std;
// define a class
class Sample {
// public elements
public:
int age;
void displayAge()
{
cout << "Age =
" << age <<
endl;
}
};
Continuing example…
int main() {
return 0;
}
Access Specifiers Example 1
#include <iostream> // Main function for the program
using namespace std;
int main( )
class Line {
{ Line line;
public:
double length; // set line length
void setLength( double len ); line.setLength(6.0);
double getLength( void ); cout << "Length of
}; line : " <<
line.getLength()
double Line::getLength(void) <<endl;
{
return length ; // set line length
} without member
function
void Line::setLength( double len
) line.length = 10.0; // OK: because length is public
{ Length
cout <<of line : 6
"Length of line : " << line.length <<endl;
length = len; Length of line : 10
return 0;
} }
Access Specifiers Example 2
#include <iostream>
// Main function for the program
using namespace std; int main( )
{
class Box Box box;
{
public: // set box length without member function
double length; box.length = 10.0; // OK: because length is public
void setWidth( double wid ); cout << "Length of box : " << box.length <<endl;
double getWidth( void );
// set box width without member function
private: // box.width = 10.0; // Error: because width is private
double width; box.setWidth(10.0); // Use member function to set it.
}; cout << "Width of box : " << box.getWidth() <<endl;
SYNTAX:
class class_name
{
……….
public
class_name ([parameter list]) //CONSTRUCTOR
{
……………….
}
};
TYPES OF CONSTRUCTORS:
•Default constructor:
•Dynamic constructor
•Parameterized constructor
•Copy constructor
UML
UML Diagrams stands for Unified Modeling Language.
A diagram must be clear and concise so that the viewer will readily
understand it.
THREE CATEGORIES OF UML
DIAGRAMS
Structural diagram
Behavioral diagram
Interaction diagram
STRUCTURAL DIAGRAM
UML diagrams that deals with the static part of a system are called structural
diagrams.
UML diagrams that deals with the moving or dynamic parts of the system are
called behavioral diagrams.
Activity diagram
Use case diagram
State machine diagram
INTERACTION DIAGRAMS
It is used to visualize the flow between various use case elements of a system.
Interaction diagrams are used to show an interaction between two entities and
how data flows within them.
Timing diagram
Sequence diagram
Collaboration diagram
CLASS DIAGRAM:
It represents the types of objects residing in the system and the relationships
between them.
A class consists of its objects, and also it may inherit from other classes.
It is the only diagram that is widely used for construction, and it can be mapped with
object-oriented languages.
Upper Section: The upper section encompasses the name of the class.
Some of the following rules that should be taken into account while
representing a class are given below:
Capitalize the initial letter of the class name.
Place the class name in the center of the upper section.
A class name must be written in bold format.
The name of the abstract class should be written in italics format.
MIDDLE SECTION OF CLASS
DIAGRAM
Middle Section: The middle section constitutes the attributes, which describe the
quality of the class.
The methods are represented in the form of a list, where each method is
written in a single line.
1. Dependency
2. Generalization
3. Association
DEPENDENCY
Shape
Shared Target Style
...
Polygon Ellipse Spline
GENERALIZATION
2.Aggregation
3.Composition
MULTIPLICITY
A contact book consists of multiple contacts, and if you delete the contact
book, all the contacts will be lost.
NOTATIONS
EXAMPLE
EXAMPLE FOR ASSOCIATIONS AND
MULTIPLICITY
LABELLING ASSOCIATION
MULTIPLICITY OF ASSOCIATIONS
MULTIPLICITY OF
ASSOCIATIONS
MULTIPLICITY OF
ASSOCIATIONS
Many-to-many
A secretary can work for many managers
A manager can have many secretaries
Secretaries can work in pools
Managers can have a group of secretaries
Some managers might have zero secretaries.
Is it possible for a secretary to have, perhaps temporarily, zero managers?
ABSTRACT CLASS
Abstract Classes
An abstract class is a class that is designed to be specifically used as a
base class.
The notation of the abstract class is similar to that of class; the only
difference is that the name of the class is written in italics.
ABSTRACT CLASS
Some key points that are needed to keep in mind while drawing a class diagram are
given below:
9.
Use case examples, 2
9.
Use Case Descriptions
An actor is who or what initiates the events involved in the task of the use
case. Actors are simply roles that people or objects play.
Use Case
Each use case in a use case diagram describes one
and only one function in which users interact with
the system
Relationships
Represent communication between actor and use case
Depicted by line or double-headed arrow line
Also called association relationship
Linking Use Cases
• Include relationships
• One use case (base) includes the functionality of
another (inclusion case)
• Supports re-use of functionality
• Extend relationships
• One use case (extension) extends the behavior of
another (base)
Generalization
The base use case explicitly incorporates the behavior of another use case at a
location specified in the base.
The included use case never stands alone. It only occurs as a part of some
larger base that includes it.
Example:Login UC includes User Authentication UC);
to Authorize Car Loan (standard use case), a clerk must run Check Client’s Credit
History (include use case)
extend
The base use case implicitly incorporates the behavior of another use case at
certain points called extension points.
The base use case may stand alone, but under certain conditions its
behavior may be extended by the behavior of another use case.
How to create use case
diagram
1. List main system functions (use cases) in a column:
– think of business events demanding system’s response
– users’ goals/needs to be accomplished via the system
– Create, Read, Update, Delete (CRUD) data tasks
– Naming use cases
– user’s needs usually can be translated in data tasks
2. Draw ovals around the function labels
3. Draw system boundary
4. Draw actors and connect them with use cases (if more intuitive, this can be
done as step 2)
5. Specify include and extend relationships between use cases (yes, at the end -
not before, as this may pull you into process thinking, which does not apply in
UC diagramming)
Use-Case Diagram(Appointment system)
Use-Case Diagrams
Health Care Example - Together
Online Shopping Website – Basic Use case
ATM USE CASE