Object oriented design
-
Upload
lykado0dles -
Category
Technology
-
view
1.158 -
download
5
Transcript of Object oriented design
Object-Oriented Design
Prepared by:Ms. Sherry B. Naz
Object-Oriented Design (OOD)
is a widely used programming methodology First step in the problem-solving process is to
identify the components called objects, which form the basis of the solution and to determine how these objects interact with one another.
Example: write a program that automates the video rental process for a local video store.
Identify Object: video and customer Specify for each object the relevant data and
operations to be performed on that data.
Object-Oriented Design (OOD)
Video Data▪ movie name▪ starring actors▪ producer▪ production company▪ number of copies in stock
Operations▪ checking the name of the movie▪ reducing the number of copies in stock by one after a
copy is rented▪ incrementing the number of copies in stock by one after
a customer returns a particular video
Object-Oriented Design (OOD)
An object combines data and operations into a single unit
In OOD, the final program is a collection of interacting objects
A programming language that implements OOD is called an object-oriented programming language.
Class
is collection of fixed number of components components of a class are called members of
the class Syntax:
class classIdentifier{
classMemberList};
where: classMemberList consists of variable declarations and/or functions. That is a member of a class can be either a variable (to store data) or a function.
Members of a Class
If a member of a class is a variable, you declare it just like any other variable. Also, in the definition of the class, you cannot initialize a variable when you declare it.
If a member of a class is a function, you typically use the function prototype to declare that member.
If a member of a class is a function, it can (directly) access any member of the class – member variables and member functions. That is, when you write the definition of a member function, you can directly access any member variable of the class without passing it as a parameter. The only obvious condition is that you must declare an identifier before you can use it.
Members of a Class
Members of a class are classified into three categories: private, public, protected
private, public and protected are reserved words and are called member access specifiers
Facts about private and public members of a class :
By default, all members of a class are private If a member of a class is private, you cannot access it
outside the class A public member is accessible outside the class To make a member of a class public, you use the
member access specifier public with a colon, :.
Example: Clock
Define a class to implement the time of day in a program. Because a clock gives the time of day, let use call this class ClockType
Data members to represent time: hours, minutes, and seconds
Some operations on time Set the time. Print the time. Increment the time by one second. Increment the time by one minute. Increment the time by one hour.
How many members are there in ClockType?
Class Definition
class ClockType{
int hours;int minutes;int seconds;
public:void setTime();void printTime();void incrementHours();void incrementMinutes();void incrementSeconds();
};
Object Declaration
Syntax:classType classObjectname;
Example:ClockType yourClock;ClockType, myClock, yourClock;
Accessing Class Members Syntax:
classObjectName.memberName Example:
myClock.hours = 5; // if hours is publicmyClock.printTime();
The class members that a class object can access depend on where the object is declared.
If the object is declared in the definition of member function of the class, then the object can access both the public and private members.
If the object is declared elsewhere, then the object can access only the public members of the class.
Built-in Operations on Classes Not Allowed
You cannot use arithmetic operations on class objects▪ Example: myClock + yourClock
You cannot use relational operators to compare two class objects for equality▪ Example: if(myClock == yourClock)
Allowed: Member access operator (.)▪ myClock.printTime();
Assignmner operator (=)▪ yourClock = myClock;
22647
hours
minutesseconds
myClock
hours
minutesseconds
yourClock
Assignment on Class Objects
yourClock = myClock;
2
26
47
hours
minutesseconds
myClock
2
26
47
hours
minutesseconds
yourClock
Functions and Classes
The following rules describe the relationship between functions and classes: Class objects can be passed as parameters to
functions and returned as function values As parameters to functions, classes can be
passed either by value of by reference If a class object is passed by value, the
contents of the member variables of the actual parameters are copied into the corresponding member variables of the formal parameter.
Review: Pass- by-Value
Note: If a variable is passed by values, the formal parameter copies the values of the actual parameters.
void sum(int a, int b){
a = a + 5;b = b * a;
}int main(){
int x,y;x = 3;y=2;cout<<x<<“ “<<y<<endl;sum(x,y);cout<<x<<“ “<<y<<endl;
}
a
b
sum
2
x
y
main
3
Review: Pass- Reference
Note: If a variable is passed by reference, the formal parameter receives only the address of the actual parameters.
void sum(int& a, int& b){
a = a + 5;b = b * a;
}int main(){
int x,y;x = 3;y=2;cout<<x<<“ “<<y<<endl;sum(x,y);cout<<x<<“ “<<y<<endl;
}
a
b
sum
2
x
y
main
3
Explore………………….
How to pass object as a parameter to a function?
Order of private and public Members
C++ has no fixed order in which you declare public and private members; you can declare them in any order. The only thing you need to remember is that, by default, all members of a class are private.
If you decide to declare private members after the public members , you must use the member access specifier private to begin the declaration of the private member.
ClockType Class Program
ClockType Class Program
Accessor and Mutator Functions
Categories of Member Functions Mutator Function – a member function of a class
that modifies the value(s) of the member variable(s). Accessor Function – a member function of a class
that only accesses (that is, does not modify the value(s) of the member variable(s).▪ As a safeguard, a reserved word const is added at the
end of the headings of the accessor function.▪ Note: A constant member function of a class cannot modify the
member variables of that class.
▪ A member function of a class is called constant function if its heading contains the reserved word const at the end.
▪ A constant member function of a class can only call other constant member functions of that class.
Let’s try
Add another member function that simply returns the values of the data members of the class(hours, minutes and seconds).
This member function should prevent user from modifying the values of member variables of the class.
Be reminded that a function returns at most one value.
Question?
What if printTime() is called right after creating an object of ClockType()?
int main(){
ClockType myClock;myClock.printTime();
}
Constructor
Is a special function whose main purpose is to initialize the data members of a class
There are two types of constructors: Constructors with parameters
(parameterized constructor) Constructors without parameters
(default constructor)
Properties of Constructor The name of a constructor is the same
as the name of the class A constructor, even though it is a
function, has no type. That is, it is neither a value-returning function nor a void function.
A class can have more than one constructor. However, all constructors of a class have the same name.
Properties of Constructor(cont.)
If a class has more than one constructor, the constructors must have different formal parameter lists. That is, either they have a different number of formal parameters or, if the number of formal parameters is the same, then the data type of the formal parameters, in the order list, must differ in at least one position.
Constructors execute automatically when a class object enters its scope. Because they have no types, they cannot be called like other functions.
Which constructor executes depends on the types of values passed to the class object when the class is declared
Default Constructor
ClockType::ClockType(){
hours = 0;minutes = 0;seconds = 0;
}
Parameterized Constructor
ClockType::ClockType(int hr, int min, int sec)
{hours = hr;minutes = min;seconds = sec;
}
Parameterized Constructor
ClockType::ClockType(int hr, int min, int sec)
{setTime(hr,min,sec);
}
Invoking a Default Constructor
Syntax:className classObjectName;
Example:ClockType myClock;
Invoking a Parameterized Constructor
Syntax:className classObjectName(argument list);
Example:ClockType myClock(7,19,11);
Note: The number of arguments and their type( in the
argument list ) should match the formal parameters(in the order given) of one of the constructors.
If the type of the argument does not match the formal parameters of any constructor(in the order given), C++ used type conversion and looks for the best match. For example, an integer value might be converted to a floating-point value with a zero decimal part. Any ambiguity will result in a compile-time error.
Constructors & Default Parameters
A constructor can also have default parameters. In such cases, rules for declaring formal parameters are the same as those for declaring formal parameters in a function. Moreover, actual parameters to a constructor with default parameters are passed according to the rules for functions with default parameters.
Constructors & Default Parameters
Using ClockType class, we can replace both constructors with:
ClockType(int=0, int=0, int=0);
class TestClass{
public:void print() const;TestClass(int =0, int =0, double =0.0, char =‘*’);
private:int x,y;double z;char ch;
};
TestClass::TestClass(int tX, int tY, double tZ, char tCh){
x = tX;y = tY;z = tZ;ch = tCh;
}TestClass::print()const{
cout<<“x = “ <<x<<endl;cout<<“y = “ <<y<<endl;cout<<“z = “ <<z<<endl;cout<<“ch = “ <<ch<<endl;
}
int main(){
testClass one;testClass two(5,6);testClass three (5,7,4.5);
testClass four(4,9,12);testClass five(0,0,3.4, ‘D’);one.print();two.print();three.print();four.print();five.print();
}
Reminders
If a class has no constructor(s), C++ automatically provides the default constructor. However, the object declared is still uninitialized.
If a class includes constructor(s) with parameter(s) and does not include the default constructor, C++ does not provide the default constructor for the class. Therefore, when an object of the class is declared, we must include the appropriate arguments in the declaration.
Array of Class Objects
ClockType arrivalTimeEmp[100];arrivalTImeTemp
arrivalTImeTemp[0]
arrivalTImeTemp[1]
arrivalTImeTemp[2]
arrivalTImeTemp[99]
hours
minutesseconds
yourClock
Exercise1. Design and implement a class DayType that implements the
day of the week in a program. The class should store the day such as Sun for “Sunday”. The program should be able to perform the following operations on an object of type DayType:
1. Set the day. 2. Print the day. 3. Return the day. 4. Return the next day.5. Return the previous day.6. Calculate and return the day by adding certain days to the
current day. For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday, and we add 13 days, the day to be returned is Monday.
7. Add the appropriate constructors.2. Write the definitions if the functions to implement the
operations for the class DayType as defined in #1. Also, write a program to test various operations on this class.