1 CSC241: Object Oriented Programming Lecture No 17.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
Introduction to Programming Lecture 39. Copy Constructor.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
UML Class Diagram. UML Class Diagrams2 Agenda What is a Class Diagram? Essential Elements of a UML Class Diagram Tips.
Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page: Lecture.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
7M822 UML Class Diagrams advanced concepts 15 September 2008.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
The Unified Modeling Language (UML) Class Diagrams.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
OBJECT ORIENTED PROGRAMMING LECTURE 12 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
1 CSC241: Object Oriented Programming Lecture No 13.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
C++ Review (3) Structs, Classes, Data Abstraction.
1 CSC241: Object Oriented Programming Lecture No 06.
1 CSC241: Object Oriented Programming Lecture No 22.
Inheritance One of the most powerful features of C++
Chapter 11 More about classes and OOP. Relationships Between Classes Possible relationships – Access ("uses-a") – Ownership/Composition ("has-a") – Inheritance.
1 CSC241: Object Oriented Programming Lecture No 16.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Lecture 1: UML Class Diagram September 12, UML Class Diagrams2 What is a Class Diagram? A class diagram describes the types of objects in the system.
Object-Oriented Programming in C++ More examples of Association.
Object Oriented Analysis and Design Class and Object Diagrams.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
1 CSC241: Object Oriented Programming Lecture No 02.
Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
CS212: Object Oriented Analysis and Design Lecture 33: Class and Sequence Diagram.
Inheritance and Composition Reusing the code and functionality Unit - 04.
Association / Aggregation / Composition and inheritance
1 CSC241: Object Oriented Programming Lecture No 05.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
CSIS 123A Lecture 10 Inheritance. Organizing Code Why should programs be organized? –Humans are "complexity challenged" Need simplicity, clarity, efficiency.
©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
A First Book of C++ Chapter 12 Extending Your Classes.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Pointers and Dynamic Arrays
Unified Modeling Language (UML)
EKT472: Object Oriented Programming
Object-Oriented Programming (OOP) Lecture No. 45
CSC241: Object Oriented Programming
Chapter 5 Classes.
Object Oriented Analysis and Design
Software Engineering Lecture #11.
UML Class Diagram.
Software Engineering System Modeling Extra examples Dr.Doaa Sami
Understand and Use Object Oriented Methods
Destructor CSCE 121 J. Michael Moore.
Destructor CSCE 121.
Introduction to Classes and Objects
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 17

2 Previous lecture Multiple inheritance – Example program: employ and student class Constructor in multiple inheritance – Example program Composition

3 Today’s lecture Example program – inheritance Relationship between class – Association – Aggregation – Composition Memory management – String class

4 Example program Problem statement Various situations require that pairs of numbers be treated as a unit. For example, each screen coordinate has an x (horizontal) component and a y (vertical) component. Derive a new class pairStack from Stack that allow to store pair variables on a stack using a single call to a push() – push(st); // st is a structure type variable retrieve using a single call to pop() function, – st = pop(); Go to program

5 Association It is a simple connection or channel between classes It is a relationship where all objects have their own lifecycle and there is no owner Example: Department and student – Multiple students can associate with single department – Single student can associate with multiple departments – No ownership – Both can create and delete independently

6 Aggregation It is a form of association where all object have their own lifecycle but there is a ownership like parent and child Represents as “has a” relationship Implementation details: – Use pointer variables that points to object of other class – Can use a reference variables that points to object of other class – Not responsible for creation/destruction of subclass

7 Cont. Example: Employee and Company – A single employee cannot belong to multiple company. – If we delete the company, Employee object will not destroy Employee e(6, “ali”); ID6 nameali e Company C(“MS”, &e); c *emp nameali Go to program

8 Composition It is a specialize form of aggregation. It is a strong composition Parent and child objects have coincident lifetimes Child object dose not have it's own lifecycle If parent object gets deleted, then all of it's child objects will also be deleted

9 Cont. Composition gives us 'part-of‘ relationship It is a stronger form of aggregation It has all the characteristics of aggregation plus two more – The part may belong to only one whole – The life time of the part is same as life time of the whole For example, it make sense to say that an engine is part-of a car. – The lifetime of the part (Engine) is managed by the whole (Car), in other words, when Car is destroyed, Engine is destroyed along with it.

10 Cont. Implementation details – Use normal member variables – Can use pointer values if composition class automatically handles allocation/deallocation – Responsible for creation/destruction of subclass

11 UML representation of Composition

12 Composition – Example Relationship between houses and rooms – House can contains multiple room – There is no independent life for room and any room cannot belong to different houses. – If house is deleted room will also be deleted automatically

13 Implementation details house 0 name Length Name Width Length Name Width 1rooms[2] class House{ char name[30]; Room rooms[2]; …. }; class House{ char name[30]; Room rooms[2]; …. }; class Room{ char Name[30]; int Length; int Width; …. }; class Room{ char Name[30]; int Length; int Width; …. }; Go to program

14 Summary – inheritance A class, called derived class can inherit feature of another class called a base class Derived class can add other feature of its own Class can be derived publically or privately from base class A class can be derived from more than one base class – multiple inheritance Level of inheritance

15 Cont. Aggregation is a “has a” or “part-whole” relationship Aggregation in C++: One class contains object of another class Composition represents 'part-of‘ relationship

16 A String Class Using new class String { private: char* str; public: String(char* s) { int length = strlen(s); str = new char[length+1]; strcpy(str, s); } ~String() { cout << “Deleting str.\n”; delete[ ] str; } void display() { cout << str << endl; } }; main() { String s1 = “Information Technology.”; cout << “s1=”; //display string s1.display(); } Program Output s1=Information Technology. Deleting str S1 *str Information Technology.

17 A problem with String class If you copy one String object to another, say with a statement like s2 = s1, Both objects now point to the same string in memory. But if you delete one string, the destructor will delete the char* string, leaving the other object with an invalid pointer S1 *str Information Technology. S2 *str S1 *str S2 *str

18