Object oriented design
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Object oriented design

on

  • 1,033 views

 

Statistics

Views

Total Views
1,033
Views on SlideShare
1,033
Embed Views
0

Actions

Likes
0
Downloads
36
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Object oriented design Presentation Transcript

  • 1. Prepared by:Ms. Sherry B. Naz
  • 2.  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.
  • 3.  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
  • 4.  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.
  • 5.  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.
  • 6.  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.
  • 7.  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, :.
  • 8.  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?
  • 9. class ClockType{ int hours; int minutes; int seconds;public: void setTime(); void printTime(); void incrementHours(); void incrementMinutes(); void incrementSeconds();};
  • 10.  Syntax: classType classObjectname; Example: ClockType yourClock; ClockType, myClock, yourClock;
  • 11.  Syntax: classObjectName.memberName Example: myClock.hours = 5; // if hours is public myClock.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.
  • 12.  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: myClock yourClock  Member access operator (.) hours 2 hours ▪ myClock.printTime(); minutes 26 minutes  Assignmner operator (=) seconds 47 seconds ▪ yourClock = myClock;
  • 13. yourClock = myClock; myClock yourClock hours 2 hours 2 minutes 26 minutes 26 seconds 47 seconds 47
  • 14.  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.
  • 15. 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; sum main}int main(){ a x 3 int x,y; x = 3;y=2; b y 2 cout<<x<<“ “<<y<<endl; sum(x,y); cout<<x<<“ “<<y<<endl;}
  • 16. 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; sum main}int main() a x 3{ int x,y; x = 3;y=2; b y 2 cout<<x<<“ “<<y<<endl; sum(x,y); cout<<x<<“ “<<y<<endl;}
  • 17.  How to pass object as a parameter to a function?
  • 18.  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.
  • 19.  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.
  • 20.  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.
  • 21.  What if printTime() is called right after creating an object of ClockType()?int main(){ ClockType myClock; myClock.printTime();}
  • 22.  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)
  • 23.  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.
  • 24.  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
  • 25. ClockType::ClockType(){ hours = 0; minutes = 0; seconds = 0;}
  • 26. ClockType::ClockType(int hr, int min, int sec){ hours = hr; minutes = min; seconds = sec;}
  • 27. ClockType::ClockType(int hr, int min, int sec){ setTime(hr,min,sec);}
  • 28.  Syntax: className classObjectName; Example: ClockType myClock;
  • 29.  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.
  • 30.  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.
  • 31.  Using ClockType class, we can replace both constructors with: ClockType(int=0, int=0, int=0);
  • 32. class TestClass{ public: void print() const; TestClass(int =0, int =0, double =0.0, char =‘*’); private: int x,y; double z; char ch;};
  • 33. 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;}
  • 34. 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();}
  • 35.  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.
  • 36.  ClockType arrivalTimeEmp[100]; arrivalTImeTemp arrivalTImeTemp[0] arrivalTImeTemp[1] arrivalTImeTemp[2] yourClock hours minutes seconds arrivalTImeTemp[99]
  • 37. 1. 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.