Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "OBJECT ORIENTED PROGRAMMING LECTURE 12 Instructor: Rashi Garg Coordinator: Gaurav Saxena."— Presentation transcript:

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

2 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.

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

4 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

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

6 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”

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

8 Contd … UniversityPerson 1 0..1 * * 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

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

10 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

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

12

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

14 Contd …

15

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

17 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?

18 Types of Association Aggregation Composition

19 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

20 Representation Whole Class Part Class Example Computer DisplayCPU

21 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.

22 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

23 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; }

24 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

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

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

27 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

28 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) { }

29 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; }

30 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; } };

31 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); } };

32 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; }


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

Similar presentations


Ads by Google