OBJECT ORIENTED PROGRAMMING LECTURE 12 Instructor: Rashi Garg Coordinator: Gaurav Saxena.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Stereotypes Stereotypes provide the capability to create a new kind of modeling element. –They can be used to classify or mark modeling elements. –A type.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
UML Class Diagram. UML Class Diagrams2 Agenda What is a Class Diagram? Essential Elements of a UML Class Diagram Tips.
UML Class Diagram and Packages Written by Zvika Gutterman Adam Carmi.
UML Class Diagram. UML Class Diagrams2 Agenda What is a Class Diagram? Essential Elements of a UML Class Diagram Tips.
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
UML Class Diagram and Packages Written by Zvika Gutterman Adam Carmi.
UML Class Diagram and Packages Written by Zvika Gutterman Adam Carmi.
Chapter 14 (Web): Object-Oriented Data Modeling
UML Class Diagram and Packages
What is UML? A modeling language standardized by the OMG (Object Management Group), and widely used in OO analysis and design A modeling language is a.
Class Diagram & Object Diagram
7M822 UML Class Diagrams advanced concepts 15 September 2008.
Modelling classes Drawing a Class Diagram. Class diagram First pick the classes –Choose relevant nouns, which have attributes and operations. Find the.
Chapter 14: Object-Oriented Data Modeling
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Object Oriented Analysis & Design & UML (Unified Modeling Language)1 Part V: Design The Design Workflow Design Classes Refining Analysis Relationships.
Object-Oriented Programming in C++
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Lab 04.
CS3773 Software Engineering Lecture 04 UML Class Diagram.
Databases : Data Modeling 2007, Fall Pusan National University Ki-Joune Li.
Unit 1 INTRODUCTION TO MODELING AND CLASS MODEL Ref : L7-UML.PDF.
بسم الله الرحمن الرحيم ” اللهم أنت ربي لا إله إلا أنت خلقتني و أنا عبدك وأنا على عهدك ووعدك ما استطعت ، أعوذ بك من شر ما صنعت ، أبوء لك بنعمتك على و أبوء.
Unified Modeling Language © 2002 by Dietrich and Urban1 ADVANCED DATABASE CONCEPTS Unified Modeling Language Susan D. Urban and Suzanne W. Dietrich Department.
Chapter 16 Applying UML and Patterns Craig Larman
® IBM Software Group © 2006 IBM Corporation Rational Software France Object-Oriented Analysis and Design with UML2 and Rational Software Modeler 03. Classes,
CS212: Object Oriented Analysis and Design Lecture 13: Relationship between Classes.
UML Class Diagram Trisha Cummings. What we will be covering What is a Class Diagram? Essential Elements of a UML Class Diagram UML Packages Logical Distribution.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
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.
Class Diagram Classes are the most important building block of any object-oriented system. A class is a description of a set of objects that share the.
Design Model Lecture p6 T120B pavasario sem.
Object Oriented Analysis: Associations. 2 Object Oriented Modeling BUAD/American University Class Relationships u Classes have relationships between each.
Object Oriented Analysis and Design Class and Object Diagrams.
Class Modeling Design Class diagram. Classes The term “class ” refers to a group of objects that share a common attributes and common behaviour (operations).
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Database Design – Lecture 12 Object Oriented Database Design cont’d.
Class Diagram Chapter 21 Applying UML and Patterns Craig Larman.
Methodology First and Language Second -A Way to Teach Object-Oriented Programming Haibin Zhu, PhD Department of Computer Science and Mathematics Nipissing.
CS212: Object Oriented Analysis and Design Lecture 33: Class and Sequence Diagram.
UML Class Diagram notation Indicating relationships between classes SE-2030 Dr. Mark L. Hornick 1.
UML Part 1: Class Diagrams. Introduction UML stands for Unified Modeling Language. It represents a unification of the concepts and notations presented.
1 CSC241: Object Oriented Programming Lecture No 17.
Class Diagram Lecture # 1. Class diagram A Class Diagram is a diagram describing the structure of a system shows the system's classes Attributes operations.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
A First Book of C++ Chapter 12 Extending Your Classes.
Software Modelling Class Diagram. Class Diagrams The main building block in object oriented modeling They are used both for general conceptual modeling.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Unified Modeling Language (UML)
Object-Oriented Modeling
COMP 2710 Software Construction Class Diagrams
Class Diagrams.
Interface, Subclass, and Abstract Class Review
Class diagram Description
UML Class & Object Diagram II
UML Unified Modelling Language
UML Class Diagrams: Basic Concepts
Object Oriented Analysis and Design
Lec 3: Object-Oriented Data Modeling
Software Engineering Lecture #11.
UML Class Diagram.
Unified Modelling Language
Software Engineering System Modeling Extra examples Dr.Doaa Sami
Class Diagrams Class diagram is basically a graphical representation of the static view of the system and represents different aspects of the application.
Object Oriented System Design Class Diagrams
Information System Design
Presentation transcript:

OBJECT ORIENTED PROGRAMMING LECTURE 12 Instructor: Rashi Garg Coordinator: Gaurav Saxena

Generalization Indicates that objects of the specialized class (subclass) are substitutable for objects of the generalized class (super-class). ▫describes the inheritance relationship of the object oriented world. It is parent and child relationship. ▫“is kind of” relationship. ▫Generalization is represented by an arrow with hollow arrow head as shown below. ▫One end represents the parent element and the other end child element.

Example Shape {abstract} Circle Super Class Sub Class An abstract class Generalization relationship

Contd … A sub-class inherits from its super-class ▫Attributes ▫Operations ▫Relationships A sub-class may ▫Add attributes and operations ▫Add relationships ▫Refine (override) inherited operation

C++ Example for Generalization class 2DPoint { int x, y; }; class 3DPpoint : 2DPoint { int z; };

Associations A semantic relationship between two or more classes that specifies connections among their instances. A structural relationship, specifying that objects of one class are connected to objects of a second (possibly the same) class. ▫describes how the elements in an UML diagram are associated. In simple word it describes how many elements are taking part in an interaction. Association is represented by a solid line with (without) arrows on both sides. The two ends represent two associated elements as shown below. The multiplicity is also mentioned at the ends (1, * etc) to show how many objects are associated. Example: “An Employee works for a Company”

Contd … Connect two classes Have an optional label Have multiplicities Are directional Have optional roles

Contd … UniversityPerson * * Multiplicity SymbolMeaning 1One and only one 0..1Zero or one M..NFrom M to N (natural language) *From zero to any positive integer 0..*From zero to any positive integer 1..*From one to any positive integer teacher employer Role “A given university groups many people; some act as students, others as teachers. A given student belongs to a single university; a given teacher may or may not be working for the university at a particular time.” student

Contd… Student Class Section Course Semester Instructor Department takes> is registered for> teaches> sponsors> <works for is instance of> is held during>

Navigability Instances of the class at one end of the link can be accessed efficiently at runtime from the instances at the other ends of link. UML specification does not dictate how efficient this access should be or any other specific mechanism to achieve the efficiency. It is implementation specific. Navigation of association can be: ▫Unspecified ▫Unidirectional ▫Bidirectional Navigation is specified by arrow, not the label

Example 11 Class Section Course Instructor Department teaches> sponsors> <works for is instance of>

C++ code Association is typically implemented with a pointer or a reference instance variable. class A { private: B* b; }; AB

Contd …

Student Class Section Course Semester Instructor Department takes> is registered for> teaches> sponsors> <works for is instance of> is held during> 1..* *

Questions From the previous diagram ▫How many classes can a student take? ▫Do you have to be registered in any classes to be a student? ▫Do I need to teach this class to be an Instructor? Do I need to teach ANY classes? ▫Can a class have no students? Is that valid?

Types of Association Aggregation Composition

Aggregation Used to illustrate whole-part relationship between two classes where one class is considered as whole made up of one or more classes comprising its parts. Part classes can exist without whole but when they are aggregated to a whole they are used to compromise that class. Aggregation is represented with the normal association line, but with an empty diamond at the class representing the whole

Representation Whole Class Part Class Example Computer DisplayCPU

Contd … Computer DisplayCPU FanMemory Because Part classes of an aggregation association can exist on their own, this type of association implies that when the whole class is destroyed, the part classes will still exist. If the part classes are destroyed, the whole class will continue to exist.

C++ code Typically use pointer variables that point to an object that lives outside the scope of the aggregate class Can use reference values that point to an object that lives outside the scope of the aggregate class Not responsible for creating/destroying subclasses

Example #include using namespace std; class Teacher { private: string m_strName; public: Teacher(string strName) : m_strName(strName) { } void GetName() {cout<<m_strName; } }; class Department { private: Teacher *m_pcTeacher; // This dept holds only one teacher public: Department(Teacher *pcTeacher=NULL) : m_pcTeacher(pcTeacher) { } void disp(){cout<<"Hello"<<endl; } }; int main() { // Create a teacher outside the scope of the Department Teacher *pTeacher = new Teacher("Bob"); // create a teacher { // Create a department and use the constructor parameter //to pass the teacher to it. Department cDept(pTeacher); cDept.disp(); } // cDept goes out of scope here and is destroyed // pTeacher still exists here because cDept did not destroy it cDept.disp(); p-Teacher->GetName(); delete pTeacher; }

Composition A form of aggregation association where the part classes used to make up the whole class cannot exist on their own. Whole class is made up of part classes and part classes need whole class to exist. Destruction of whole class means destruction of the part classes. Composition is represented with the single-line association with the addition of solid diamond at the whole class end of the line

Contd … Because association indicated that the part classes are mandatory, multiplicity of atleast one is always implied for the whole class WholeClass PartClass 1

Example Database QueryTable * 1..* Database QueryTable Records * 1..* 0..*

C++ code Typically use normal member variables Can use pointer values if the composition class automatically handles allocation/deallocation Responsible for creation/destruction of subclasses

Example #1 #include "CPU.h" #include "Motherboard.h" #include "RAM.h" class PersonalComputer { private: CPU m_cCPU; Motherboard m_cMotherboard; RAM m_cRAM; }; PersonalComputer::PersonalComputer(int nCPUSpeed, char *strMotherboardModel, int nRAMSize) : m_cCPU(nCPUSpeed), m_cMotherboard(strMotherboardModel), m_cRAM(nRAMSize) { }

Example #2 #include using namespace std; class A { int i; public: A() { cout<<"A's Constructor"<<endl; } ~A() { cout<<"A's Destructor"<<endl; } }; class B { public: B() { A a; cout<<"B's Constructor"<<endl; } ~B() { cout<<"B's Destructor"<<endl; } }; int main() { B obj; return 0; }

Example #3 class Point2D { private: int m_nX; int m_nY; public: // A default constructor Point2D() : m_nX(0), m_nY(0) { } // A specific constructor Point2D(int nX, int nY) : m_nX(nX), m_nY(nY) { } // An overloaded output operator friend std::ostream& operator<<(std::ostream& out, const Point2D &cPoint) { out << "(" << cPoint.GetX() << ", " << cPoint.GetY() << ")"; return out; } // Access functions void SetPoint(int nX, int nY) { m_nX = nX; m_nY = nY; } int GetX() const { return m_nX; } int GetY() const { return m_nY; } };

Contd … class Creature { private: std::string m_strName; Point2D m_cLocation; // We don't want people to create Creatures with no name or location // so our default constructor is private Creature() { } public: Creature(std::string strName, const Point2D &cLocation) : m_strName(strName), m_cLocation(cLocation) { } friend std::ostream& operator<<(std::ostream& out, const Creature &cCreature) { out << cCreature.m_strName.c_str() << " is at " << cCreature.m_cLocation; return out; } void MoveTo(int nX, int nY) { m_cLocation.SetPoint(nX, nY); } };

Contd … int main() { using namespace std; cout << "Enter a name for your creature: "; std::string cName; cin >> cName; Creature cCreature(cName, Point2D(4, 7)); while (1) { cout << cCreature << endl; cout << "Enter new X location for creature (-1 to quit): "; int nX=0; cin >> nX; if (nX == -1) break; cout << "Enter new Y location for creature (-1 to quit): "; int nY=0; cin >> nY; if (nY == -1) break; cCreature.MoveTo(nX, nY); } return 0; }