OBJECT ORIENTED PROGRAMMING LECTURE 12 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
-
Upload
gordon-cannon -
Category
Documents
-
view
214 -
download
0
Transcript of 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.
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
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 …
University Person
1
0..1
*
*
Multiplicity
Symbol Meaning
1 One and only one
0..1 Zero or one
M..N From 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
teacheremployer
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>
<w
orks
for is instance of>
is he
ld d
urin
g>
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>
<w
orks
for is instance of>
C++ code
•Association is typically implemented with a pointer or a reference instance variable.
class A{ private: B* b;};
A B
Student
Class Section
Course
Semester
Instructor
Department
takes>
is registered for>
teaches>
sponsors>
<w
orks
for is instance of>
is he
ld d
urin
g>
1..*1
1..*
1..*11
1..*
0..8
0..*
0..61..3
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?
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
Contd …
Computer
DisplayCPU
Fan Memory
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 <string> 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
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 <iostream>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 #3class 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;}