Inheritance Allows extension and reuse of existing code Derived class can access base class properties and still the derived class has its own properties
A generic Example
Inheritance in Railway Reservation System Passenger is a person Employee is a person E-ticket is a ticket I-ticket is a E-Ticket (Multi level Inheritance)
Look alike but exhibit different characters
Polymorphism Derived from the Greek many forms Single name can be used for different purposes Different ways of achieving the polymorphism: 1. Function overloading 2. Operator overloading 3. Dynamic binding
Polymorphism Languages exhibit polymorphism through operators "+" for addition of reals and integers or "-" for unary negation or arithmetic subtraction. Therefore the same operation is performed on different data types with out changing the basic meaning of the operation. ‘*’ is overloaded for multiplication and as dereference operator
Static Vs Dynamic binding Routing a call to a landline/mobile phone Binding->Tie up of a call to the address code to be executed in response to a call Landline – Static routing Mobile Phones – Dynamic binding Behavior of the object determined only at runtime Also called as late binding
Polymorphism in Railway Reservation System Search a train – Search by train number, name etc – Function Overloading [], -, + - Operators shall be overloaded for knowing details of a coach in a train, add or remove a coach from a train
Polymorphism in Railway Reservation System ComputeSalary function shall be made as pure virtual function and implemented in dervied classes regular employee, daily wages employee etc
Message passing Sending and receiving of information by the objects same as people exchange information Invoking an operation on an object To invoke an operation is based on the name of the operation and the class of the object being operated. Shall be interpreted as a request for the execution of a function.
Message Passing in Railway Reservation System Know the availability of seats in a coach during reservation Interaction is between the ticket object and the coach object Ticket makes a request and coach returns the availability
Persistance Phenomena by which object outlives the program execution and exists between executions of a program Not supported in C++
Delegation Receiving object may delegate operations to other objects Eg: Child class can delegate the request to parent class Age of a passenger Requested will be delegated to parent class - person
Genericity Defines the software with more than one interpretation depending on the data type of parameters Thus, it allows the declaration of data items without specifying their exact data type. Such data types are solved at the time of their usage. This can be realized through the “function template” and “class template”
Genericity in C++ Function templates – Sort coaches by capacity, by class (First, Second, third), coach position Class templates – Stack ADT, linked list, Queues etc
There are two kinds of Relationships OO Relationships There are two kinds of Relationships Generalization (parent-child relationship) Association (student enrolls in course) Associations can be further classified as Aggregation Composition
OO Relationships: Generalization Supertype Example: Customer Regular Customer Loyalty Customer Subtype1 Subtype2 or: Customer - Generalization expresses a parent/child relationship among related classes. - Used for abstracting details in several layers Regular Customer Loyalty Customer
OO Relationships: Association Represent relationship between instances of classes Student enrolls in a course Courses have students Courses have exams Coach is connected to a train Association has two ends Role names (e.g. enrolls, connected) Multiplicity (e.g. One course can have many students, one train may have many coached)
How to create the class “Account” in dia? accno:int acctype:String name:String balance:float deposit(a:int):void withdraw(a:int):int enquire():String
Choose the option “UML” From the select box
Select the class from the tool bar and Drag and drop it in to the canvas Select “class”
Double click the compartment “Class”
Edit the class name as “Account”
Now choose the Tabbed button “Attributes” to name the attribute
Press the button “New” to enable the “Attribute data” fields and edit the fields(name,type and visibility)
Now press the button “New” to add more attributes and finally press “ok”
Similarly, choose the Tabbed button “Operations” to add the operations Press “New” to enable the parameter data fields
Adding parameters and type and press ok
Use the same procedure to add other methods
A sample showing the “Association” relationship Double click the association link for setting the properties
A sample showing the generalization relationship