10/21/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01b Title: C++ as O-O Prog Lang (Review) Reference: COS240 Syllabus.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
7/2/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter 11.
Inheritance and Polymorphism CS351 – Programming Paradigms.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 CSC241: Object Oriented Programming Lecture No 07.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)
1 CSC241: Object Oriented Programming Lecture No 13.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 CSC241: Object Oriented Programming Lecture No 12.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
1 CSC241: Object Oriented Programming Lecture No 16.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 10 Inheritance and Polymorphism
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
1 CSC241: Object Oriented Programming Lecture No 02.
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
1 CSC241: Object Oriented Programming Lecture No 05.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
1 CSC241: Object Oriented Programming Lecture No 17.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 CSC241: Object Oriented Programming Lecture No 08.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
A First Book of C++ Chapter 12 Extending Your Classes.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Reference: R.Sebesta, Chapters 11, 12
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Visit for more Learning Resources
Reference: R.Sebesta, Chapter 12
CSC241: Object Oriented Programming
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
CSC241: Object Oriented Programming
7. Inheritance and Polymorphism
Reference: COS240 Syllabus
Review: Two Programming Paradigms
Introduction to Classes
Introduction to Classes
Object Oriented Programming Using C++
Introduction to Classes and Objects
CIS 199 Final Review.
Presentation transcript:

10/21/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01b Title: C++ as O-O Prog Lang (Review) Reference: COS240 Syllabus

10/21/2015Assoc. Prof. Stoyan Bonev2 Lecture Contents C++ as O-O Prog Lang –Brief review C++ as O-O Prog Lang –Comprehensive review Data Encapsulation and Data Hiding Inheritance Polymorphism

10/21/2015Assoc. Prof. Stoyan Bonev3 Reminder - What is an object? OBJECT Operations Data set of methods (member functions, methods) internal state (values of private data members)

10/21/2015Assoc. Prof. Stoyan Bonev4 Example #include class circle { private: double radius; public: void store(double); double area(void); void display(void); }; int main(void) { circle c; // an object of circle class c.store(5.0); cout << "The area of circle c is " << c.area() << endl; c.display(); }

10/21/2015Assoc. Prof. Stoyan Bonev5 #include class circle { private: double radius; public: void store(double); double area(void); void display(void); }; // member function definitions void circle::store(double r) { radius = r; } double circle::area(void) { return 3.14*radius*radius; } void circle::display(void) { cout << “r = “ << radius << endl; } int main(void) { circle c; // an object of circle class c.store(5.0); cout << "The area of circle c is " << c.area() << endl; c.display(); }

10/21/2015Assoc. Prof. Stoyan Bonev6 When declaring an instance of a class, its data members are all uninitialized. C++ allows objects to initialize themselves when they are created. This automatic initialization is performed by the use of a constructor function. A constructor function is a special function that is a member of class and has the same name as that class. The opposite of the constructor is the destructor. The destructor is called when the object is “destroyed”, allowing any final “house-keeping” activities to be performed. The destructor function has the same name as the constructor but preceded by the tilde (~) symbol.

10/21/2015Assoc. Prof. Stoyan Bonev7 OOP terminology Member functions are referred to as methods. Data items are referred to as attributes or instance variables. Calling an object’s member function is referred to as sending a message to the object.

10/21/2015Assoc. Prof. Stoyan Bonev8 Defining the Class General syntax Access qualifiers: private and public Data members –Usually data is private Member functions –Usually functions are public

10/21/2015Assoc. Prof. Stoyan Bonev9 Class SmallObj class SmallObj { private: int somedata; public: void SetData(int d) { somedata = d; } void ShowData() { cout << "\nData is =" << somedata; } };

10/21/2015Assoc. Prof. Stoyan Bonev10 SmallObj – UML class diagram SmallObj somedata -somedata SetData(int) +SetData(int) ShowData() +ShowData()

10/21/2015Assoc. Prof. Stoyan Bonev11 Using Class SmallObj void main() { SmallObj s1, s2; // defining objects s1.SetData(67); // sending messages s2.SetData(123); s1.ShowData(); s2.ShowData(); }

10/21/2015Assoc. Prof. Stoyan Bonev12 SmallObj – UML object diagrams Unlike classes, objects are underlined. Colon ( : ) serves to separate the object name and the class name. s1:SmallObj somedata=15 s2:SmallObj somedata=173

10/21/2015Assoc. Prof. Stoyan Bonev13 Class Part /modern version only/ class Part { private: int modelnumber; int partnumber; double cost; // modern model public: void setModelNumber(int mdl) { modelnumber = mdl; } int getModelNumber() const { return modelnumber; } void setPartNumber(int mdl) { partnumber = mdl; } int getPartNumber() const { return partnumber; } void setCost(double cst) { cost = cst; } double getCost() const { return cost; } };

10/21/2015Assoc. Prof. Stoyan Bonev14 Class Part /modern version only/ void main() { Part p2; p2.setModelNumber(8124); p2.setPartNumber(516); p2.setCost(314.52); cout << "\n\nPart components:“ << p2.getModelNumber() << " " << p2.getPartNumber() << " " << p2.getCost(); cout << '\n' << '\n'; system("pause"); }

10/21/2015Assoc. Prof. Stoyan Bonev15 Evolution of the class concept Methods whose type is like ShowData() are considered obsolete and therefore not recommended to be used. Instead, a pair of methods associated to each one of the data fields is introduced. They serve for bi-directional (in, out) access to the data field. Method Accessor, also ‘getter’ (getX()) Method Mutator, also ‘setter’ (setX()) Microsoft extends the getX/setX methods to the property concept

10/21/2015Assoc. Prof. Stoyan Bonev16 C++ Modern Object – native code using namespace std; class ModernObject { private: int x; // constructors public: ModernObject() { x = 0; } public: ModernObject(int par) { x = par; } // obsolete method Show...type() public: void showModernObject() { cout << endl << "Data = " << x ; } // method accessor public: int getModernObject() { return x; } // methods mutators public: void setModernObject(int par) { x = par; } public: void setModernObject(int par1, int par2) { x = par1 + par2; } public: void setModernObject(int par1, int par2, int par3) { x = par1 + par2 + par3; } // properties not supported in native code };// end of class ModernObject

10/21/2015Assoc. Prof. Stoyan Bonev17 C++ Modern Object – native code void main() { ModernObject a; ModernObject b(15); ModernObject c; a.showModernObject();b.showModernObject(); // test accessor and mutators methods c.setModernObject(20); cout << endl << c.getModernObject(); c.setModernObject(20,50); cout << endl << c.getModernObject(); c.setModernObject(20,30,40); cout << endl << c.getModernObject(); /* // test property // no properties, =>>,no statements to test properties */ }

10/21/2015Assoc. Prof. Stoyan Bonev18 C++ objects as Data Types class Distance

10/21/2015Assoc. Prof. Stoyan Bonev19 Class Distance class Distance { private: int feet; float inches; public: void SetDist(int ft, float in) { feet = ft; inches = in; } void GetDist() {cout > feet; cout > inches; } void ShowDist() { cout <<“Feet=" << feet <<" Inches="<< inches; } }; //============================================================= void main () { Distance d1, d2; d1.SetDist(3, 5.6); cout << "\nDistance components: "; d1.ShowDist(); d2.GetDist(); cout << "\nDistance components: "; d2.ShowDist(); }

10/21/2015Assoc. Prof. Stoyan Bonev20 C++ objects as general purpose programming elements class Counter

10/21/2015Assoc. Prof. Stoyan Bonev21 Class Counter class Counter { private: unsigned count; public: void SetCount(int val) { count = val; } void GetData() { cout > count; } void ShowData() { cout <<"\nData count is:" << count;} void IncCount() { count++; } unsigned GetCount() { return count; } }; //===================================== void main() { Counter c1; c1.GetData(); c1.ShowData(); c1.IncCount(); c1.ShowData(); cout << "\n" << c1.GetCount(); c1.ShowData(); Counter c2; c2.SetCount(100); cout << "\n\nCounter c2 =" << c2.GetCount(); c2.IncCount(); }

10/21/2015Assoc. Prof. Stoyan Bonev22 C++ objects Constructors and Destructors Class Counter class Distance

10/21/2015Assoc. Prof. Stoyan Bonev23 Constructors and Destructors Class Counter class Counter { private: unsigned count; public: Counter() { count = 0; } Counter(int val) { count = val; } void SetCount(int val) { count = val; } void GetData() { cout > count;} void ShowData() { cout <<"\nData count is:" << count;} void IncCount() { count++; } unsigned GetCount() { return count; } };

10/21/2015Assoc. Prof. Stoyan Bonev24 Constructors and Destructors Class Counter void main() { Counter c1; c1.GetData(); c1.ShowData(); c1.IncCount(); c1.ShowData(); cout << "\n" << c1.GetCount(); c1.ShowData(); Counter c2(100); cout << "\n\nCounter c2 =" << c2.GetCount(); c2.IncCount(); cout << "\n\nCounter c2 =" << c2.GetCount(); }

10/21/2015Assoc. Prof. Stoyan Bonev25 Constructors and Destructors Class Distance class Distance { private: int feet; float inches; public: Distance() { feet = 0; inches = 0.0; } Distance(int ft, float in) { feet = ft; inches = in; } void SetDist(int ft, float in) { feet = ft; inches = in; } void GetDist() {cout >feet; cout > inches; } void ShowDist() { cout <<“Feet=" << feet <<" Inches="<< inches; } };

10/21/2015Assoc. Prof. Stoyan Bonev26 Constructors and Destructors Class Distance void main () { Distance d1, d2; d1.SetDist(3, 5.6); cout << "\nDistance components: "; d1.ShowDist(); d2.GetDist(); cout << "\nDistance components: "; d2.ShowDist(); Distance d3(4, 5.45), d4(7, 8.9); cout << "\nDistance components: "; d3.ShowDist(); cout << "\nDistance components: "; d4.ShowDist(); }

More on Constructors/Destructors

10/21/2015Assoc. Prof. Stoyan Bonev28 Constructors & Destructors Given class X with two data components class X { private: int x,y; public: X(int, int);... }; There are 3 ways to define constructor X() X::X(int a, int b) { x=a; y=b; } X::X(int a, int b) : x(a) { y=b; } X::X(int a, int b) : x(a), y(b) { }

10/21/2015Assoc. Prof. Stoyan Bonev29 Copy constructor and assignment constructor If you specify a class with no-constructor, then a no-arg system supported constructor is available X::X() {... }CL::CL() {... } If you define an object whose data components are initialized (depend on) data components of other objects of the same class, then a system defined (built-in) copy constructor or its overloaded version called assignment constructor is to be used X::X(X&)CL::CL(CL&) How to pronounce: X of X ref

10/21/2015Assoc. Prof. Stoyan Bonev30 Copy constructor demo class CL {private: int x, y; public:CL();... }; // user specified constructor CL::CL(){cout >x>>y;} void main() { CL obj1;// user specified constructor CL obj2=obj1;// default copy constructor CL obj3(obj1);... }

10/21/2015Assoc. Prof. Stoyan Bonev31 Assignment constructor demo class CL {private: int x, y; public: CL(); CL(CL&);... }; // user specified constructor CL::CL(){cout >x>>y;} // user specified assignment constructor CL::CL(CL& p) {x = p.x +1; y = p.y +2; } void main() { CL obj1;// user specified constructor CL obj2=obj1;// user specified assignment constructor CL obj3(obj1);... }

10/21/2015Assoc. Prof. Stoyan Bonev32 Objects as Function Arguments Class Distance Distance dist1(5, 6.8), dist2(3, 4.5), dist3; Task: To add two distances using a method: dist3.AddDist1(dist1, dist2);

10/21/2015Assoc. Prof. Stoyan Bonev33 Objects as Function Arguments // void AddDist1(Distance d1, Distance d2); // void AddDist1(Distance d1, Distance d2) { feet = d1.feet +d2.feet; inches = d1.inches + d2.inches; if (inches >= 12.) { inches -= 12.; feet++; }

10/21/2015Assoc. Prof. Stoyan Bonev34 Returning Objects from Functions Class Distance Distance dist1(5, 6.8), dist2(3, 4.5), dist4; Task: To add two distances using alternate method: dist4 = dist1.AddDist2(dist2);

10/21/2015Assoc. Prof. Stoyan Bonev35 Returning Objects from Functions // first version of source text Distance AddDist2(Distance d1) { Distance temp; temp.feet = feet + d1.feet; temp.inches = inches + d1.inches; if (temp.inches >= 12.) { temp.inches -= 12.; temp.feet++; } return temp; }

10/21/2015Assoc. Prof. Stoyan Bonev36 Returning Objects from Functions // second alternate version of source text Distance AddDist2(Distance d1) { int ft; float in; ft = feet + d1.feet; in = inches + d1.inches; if (in >= 12.) { in -= 12.; ft++; } return Distance(ft, in); // anonymous, nameless object }

Overloaded Operators

10/21/2015Assoc. Prof. Stoyan Bonev38 Introduction Why do we need operator overloading? –For better readability Examples: –Class Counter –Class Distance

10/21/2015Assoc. Prof. Stoyan Bonev39 Introduction Example: the Distance class... Distance d1(5,3.6), d2(6,4.5), d3; d3.AddDist1(d1,d2); d3 = d1.AddDist2(d2); OR d3 = d1 + d2;// OK

10/21/2015Assoc. Prof. Stoyan Bonev40 Overloading binary operators

10/21/2015Assoc. Prof. Stoyan Bonev41 Distance overloaded operator + One more method: Distance operator+(Distance d2) { int ft = feet + d2.feet; float in = inches + d2.inches; if (in >=12.) { ft++; in-=12.;} return Distance (ft, in); } Distance d1(6, 5.18), d2=3.5, d3, d4, d5; d3 = d1 + d2; d3 = d1.operator+(d2);

10/21/2015Assoc. Prof. Stoyan Bonev42 Distance overloaded comparison operators class Distance { private: int feet; float inches; public: Distance() { feet = 0; inches = 0.0; } Distance (int ft, float in) { feet = ft; inches = in; } void ShowDist() {cout <<"\nDistObject= " << feet <<" "<< inches; } bool operator < (Distance) const; }; bool Distance::operator < (Distance d2) const { float bf1 = feet + inches/12; float bf2 = d2.feet + d2.inches/12; return (bf1 < bf2) ? true : false; } }; void main() { Distance dist1(5, 6.8), dist2(3, 4.5); if ( dist1<dist2 ) cout << “\n dist1 object is less than dist2 object”; // OR if ( dist1.operator<(dist2) ) cout << “\n dist1 is less than dist2”; }

10/21/2015Assoc. Prof. Stoyan Bonev43 Overloading unary operators

10/21/2015Assoc. Prof. Stoyan Bonev44 Counter overloaded operator ++ class Counter { void IncCount() { count++; } void operator++() {count++;} }; Counter c(100); c.IncCount(); ++c; c.operator++(); //OK

10/21/2015Assoc. Prof. Stoyan Bonev45 Counter overloaded operator ++ class Counter { void IncCount() { count++; } void operator++() {count++;} }; Counter c(100); c.IncCount(); ++c; c.operator++(); //OK Counter d(300), e;++d; //OK e = ++d; // NOT OK

10/21/2015Assoc. Prof. Stoyan Bonev46 Counter overloaded operator ++ class Counter { private: unsigned count; public: Counter() { count = 0; } Counter(int val ) { count = val; } void IncCount() { count++; } void GetData() { cout > count;} void ShowData() { cout <<"\nData count is:" << count; } unsigned GetCount() { return count; } // overloaded unary operator ++, first version Counter operator++() { count++; return Counter(count); } };

10/21/2015Assoc. Prof. Stoyan Bonev47 Overloaded operators as member functions of a class: The Rule: Overloaded operator always requires one less argument than its number of operands, since one operand is the object of which the operator is a member function. This rule not valid for friend functions.

10/21/2015Assoc. Prof. Stoyan Bonev48 INHERITANCE Base class and Derived classes; Access control; Class hierarchies; Multiple inheritance.

10/21/2015Assoc. Prof. Stoyan Bonev49 The Concept of Inheritance Inheritance is the process of creating new classes, called derived classes from existing or base classes. The derived class inherits the capabilities of the base class but can add refinements of its own. The base class stays unchanged with this process. Usually the derived class is functionally more powerful or specialized compared to the base class.

10/21/2015Assoc. Prof. Stoyan Bonev50 Relations classified A-Kind-OfrelationshipA-Kind-Ofrelationship Is-A relationshipIs-A relationship

10/21/2015Assoc. Prof. Stoyan Bonev51 A-Kind-Of relationship Illustration of ''a-kind-of'' class level relationship In this figure, classes are drawn using rectangles. Their name always starts with an uppercase letter. The arrowed line indicates the direction of the relation, hence, it is to be read as ”Circle is a-kind-of Point”.

10/21/2015Assoc. Prof. Stoyan Bonev52 Is-A relationship In this figure, objects are drawn using rectangles with round corners. Their name only consists of lowercase letters. The arrowed line indicates the direction of the relation, hence, it is to be read as ”circle is a point”. Illustration of ''is-a'' instance level relationship

10/21/2015Assoc. Prof. Stoyan Bonev53 A SubClass inherits all the attributes and behaviors of the SuperClass, and may have additional attributes and behaviors..

10/21/2015Assoc. Prof. Stoyan Bonev54 Specifying Derived class (C++) class Base {... }; // Derived1 class publicly derived from Base class class Derived1: public Base {... }; // Derived2 class privately derived from Base class class Derived2: private Base {... };

10/21/2015Assoc. Prof. Stoyan Bonev55 Accessing base class members The protected access specifier class Base { private:... protected:... public:... };

10/21/2015Assoc. Prof. Stoyan Bonev56 Substituting Base class members DecrementCounter (1 st version) class CountDn : public Counter {... };

10/21/2015Assoc. Prof. Stoyan Bonev57 Class CountDn (1 st idea) class Counter { protected: unsigned count; public:void GetData() { cout > count; } void ShowData() { cout <<"\nData count is:" << count;} void IncCount() { count++; } }; class CountDn : public Counter { public:void DecCount() { count--; } }; void main() { Counter c1; c1.GetData(); c1.ShowData(); c1.IncCount(); c1.ShowData(); CountDn c3; c3.GetData(); c3.incCount(); c3.IncCount(); c3.ShowData(); c3.DecCount(); c3.ShowData(): }

10/21/2015Assoc. Prof. Stoyan Bonev58 Generalization in UML Class diagrams Counter #count +GetData() +ShowData() +IncCount() CountDn +DecCount()

10/21/2015Assoc. Prof. Stoyan Bonev59 Class CountDn (working demo) class Counter { protected: unsigned count; public: Counter() { count = 0; } Counter(int val ) { count = val; } void IncCount() { count++; } void GetData() { cout > count;} void ShowData() { cout <<"\nData count is:" << count; } unsigned GetCount() { return count; } Counter operator++(int ) { count++; return Counter(count); } Counter operator++( ) { count++; return Counter(count); } }; class CountDn : public Counter { public: Counter operator--() { count--; return Counter(count); } };

10/21/2015Assoc. Prof. Stoyan Bonev60 Derived class constructors Class CountDn class Counter { protected: unsigned count; public: Counter() { count = 0; } Counter(int val ) { count = val; } void IncCount() { count++; } void GetData() { cout > count;} void ShowData() { cout <<"\nData count is:" << count; } unsigned GetCount() { return count; } Counter operator++(int ) { count++; return Counter(count); } Counter operator++( ) { count++; return Counter(count); } }; class CountDn : public Counter { public:CountDn() : Counter() { } CountDn(int val) : Counter(val) { } Counter operator--() { count--; return CountDn(count); } };

10/21/2015Assoc. Prof. Stoyan Bonev61 Multiple Inheritance Example: 2 Base classes, 3+1 Derived classes Base classes:Derived classes: class Employee class Manager class Student class Scientist class Laborer class Foreman

10/21/2015Assoc. Prof. Stoyan Bonev62 Real Multiple Inheritance StudentEmployee diplomaname, id Manager Scientist Laborer titlepublications

10/21/2015Assoc. Prof. Stoyan Bonev63 Ambiguity in multiple inheritance- problem 1 class A { public: void Show() { cout<<“A”; } }; class B { public: void Show() { cout<<“B”; } }; class C : public A, public B {... }; void main() { C objc; objc.Show(); // ambiguous – will not compile objc.A::Show(); // OK objc.B::Show(); // OK }

10/21/2015Assoc. Prof. Stoyan Bonev64 Containership Classes within classes. Possible relations: B is a kind of A. | B has an object of |class A. | A is a part-of B. Relation based on |Relation within Inheritance | independent classes | class A {... }; |class A {... }; class B : public A | class B {... }; | {... | A obja; | };

10/21/2015Assoc. Prof. Stoyan Bonev65 Containership Classes within classes. Has-a relation: Aggregation is called a “has a” relation. We say: Library has a book. Invoice has a line item. Aggregation is also called a “part-whole” relation. The book is part of the library. In OOP, aggregation may occur when an object is an attribute of another. See previous slide. In UML, aggregation is considered a special kind of association. It’s safe to call a relation an association but if class A contains object of class B, and is organizationally superior to class B, it’s a good candidate for aggregation.

10/21/2015Assoc. Prof. Stoyan Bonev66 Aggregation Aggregation is shown in the same way as association in UML class diagrams except that the “whole” end of the association line has an open diamond-shaped arrowhead. Library Books Staff

10/21/2015Assoc. Prof. Stoyan Bonev67 Composition: a stronger Aggregation Composition has characteristics of aggregation plus: –The part may belong to only one a whole –The lifetime of the part is the same as the lifetime of the whole Car Doors Engine

10/21/2015Assoc. Prof. Stoyan Bonev68 Polymorphism Early binding and Late binding; –Regular member functions; –virtual member functions; –Methods accessed with pointers.

10/21/2015Assoc. Prof. Stoyan Bonev69 What is polymorphism? Giving different meanings to the same thing). Early binding operates on regular /normal/ member functions (methods) accessed with pointers. Late binding operates on virtual member functions (methods) accessed with pointers.. Regular /normal/ and virtual methods accessed via pointers. Examples: Base Derived1, Derived2 classes Person Professor, Student classes

10/21/2015Assoc. Prof. Stoyan Bonev70 Early binding / Late binding virtual vs. non virtual methods First example on Polymorphism: method Show() class Base {... }; class Derived1 : public Base {... }; class Derived2 : public Base {... }; Derived1 drv1;Derived2 drv2;Base *ptr; ptr = &drv1;ptr->Show(); ptr = &drv2;(*ptr).Show();

10/21/2015Assoc. Prof. Stoyan Bonev71 Early Binding (at compile time) Normal, regular, non virtual methods class Base {public: void Show(){ cout << "\n Base:" ;}}; class Derived1 : public Base {public: void Show(){ cout << "\n Derived1:" ;} }; class Derived2 : public Base {public: void Show(){ cout << "\n Derived2:" ;} }; Major factor/condition: the type of the ptr pointer – object of Base class Derived1 drv1;Derived2 drv2;Base *ptr; ptr = &drv1;ptr->Show(); ptr = &drv2;(*ptr).Show(); OOP3a.cppOOP3aEarly.exe

10/21/2015Assoc. Prof. Stoyan Bonev72 Early Binding (at compile time) The compiler ignores the contents of the pointer ptr and selects the member function that matches the type of the pointer. See figure on next slide.

10/21/2015Assoc. Prof. Stoyan Bonev73 Early Binding (at compile time)

10/21/2015Assoc. Prof. Stoyan Bonev74 Late Binding (at run time) Virtual methods class Base {public: virtual void Show(){ cout << "\n Base:" ; } }; class Derived1 : public Base {public: void Show(){ cout << "\n Derived1:" ;} }; class Derived2 : public Base {public: void Show(){ cout << "\n Derived2:" ;} }; Major factor/condition: contents of the ptr pointer – obj of derived class Derived1 drv1;Derived2 drv2;Base *ptr; ptr = &drv1;ptr->Show(); ptr = &drv2;(*ptr).Show(); OOP3a.cppOOP3aLate.exe

10/21/2015Assoc. Prof. Stoyan Bonev75 Late Binding (at run time) The compiler selects the function based on the contents of the pointer ptr, not on the type of the pointer. See figure on next slide.

10/21/2015Assoc. Prof. Stoyan Bonev76 Late Binding (at run time)

10/21/2015Assoc. Prof. Stoyan Bonev77 Late Binding (at run time) Pure virtual methods A virtual function with no body that is never executed. class Base { public: virtual void Show() = 0 ;}; class Derived1 : public Base { public: void Show(){ cout << "\n Derived1:" ;} }; class Derived2 : public Base { public: void Show(){ cout << "\n Derived2:" ;} };

10/21/2015Assoc. Prof. Stoyan Bonev78 Early binding / Late binding virtual vs. non virtual methods Second example on Polymorphism: method isOutstanding() class Person {... }; class Professor: public Person {... }; class Student: public Person {... };

10/21/2015Assoc. Prof. Stoyan Bonev79 Virtual method IsOutstanding() class Person { protected: char name[20]; public: void GetName() { cout > name; } void ShowName() { cout << "\n Name is:" << name << " "; } bool virtual isOutStanding() = 0; };

10/21/2015Assoc. Prof. Stoyan Bonev80 Virtual method IsOutstanding() class Student : public Person { private: float score; public: void GetScore() { cout > score; } bool isOutStanding() { return (score > 98.0) ? true: false; } };

10/21/2015Assoc. Prof. Stoyan Bonev81 Virtual method IsOutstanding() class Professor : public Person { private: int NumPubs; public: void GetNumPubs() { cout << "\n Enter number of professor's publications:"; cin >> NumPubs; } bool isOutStanding() { return (NumPubs > 100) ? true: false; } };

10/21/2015Assoc. Prof. Stoyan Bonev82 Virtual method IsOutstanding() void main () { Person *PersPtr[100]; Student *StuPtr; Professor *ProPtr; int n=0; char choice; do{ cout > choice; if (choice == 's') { StuPtr = new Student; StuPtr->GetName(); StuPtr->GetScore(); PersPtr[n++] = StuPtr; } else { ProPtr = new Professor; ProPtr->GetName(); ProPtr->GetNumPubs(); PersPtr[n++] = ProPtr; } cout > choice; } while (choice == 'y'); // end of do for (int j=0; j<n; j++) { PersPtr[j]->ShowName(); if (PersPtr[j]->isOutStanding() == true) cout << " --outstanding person"; } // end of for }// end of main()

10/21/2015Assoc. Prof. Stoyan Bonev83 Polymorphism in C++ classified Dynamic polymorphism –Based on late binding and virtual methods Static or ad-hoc polymorphism – –Based on overloaded functions concept Parametric (generic) polymorphism – –Based oh template reserved word

Generic Classes in C++

10/21/2015Assoc. Prof. Stoyan Bonev85 Generic Classes in C++ General form of a generic class definition: template followed by a class definition that may include the class parameters Class parameters form (there must be at least one): class identifier

10/21/2015Assoc. Prof. Stoyan Bonev86 Task: to implement 4 classes with the same structure that differ by the type of a data component class X1{ private: char item; public:X1() { item = 0;} X1( char v) { item = v; } char getItem() { return item; } void setItem( char p) { item = p; } };... X1 obj1;obj1.setItem(‘x’); cout << obj1.getItem();

10/21/2015Assoc. Prof. Stoyan Bonev87 Task: to implement 4 classes with the same structure that differ by the type of a data component class X2{ private: int item; public:X2() { item = 0;} X2( int v) { item = v; } int getItem() { return item; } void setItem( int p) { item = p; } };... X2 obj2;obj2.setItem(23); cout << obj2.getItem();

10/21/2015Assoc. Prof. Stoyan Bonev88 Task: to implement 4 classes with the same structure that differ by the type of a data component class X3{ private: float item; public:X3() { item = 0;} X3( float v) { item = v; } float getItem() { return item; } void setItem( float p) { item = p; } };... X3 obj3;obj3.setItem(2.71f); cout << obj3.getItem();

10/21/2015Assoc. Prof. Stoyan Bonev89 Task: to implement 4 classes with the same structure that differ by the type of a data component class X4{ private: double item; public:X4() { item = 0;} X4( double v) { item = v; } double getItem() { return item; } void setItem( double p) { item = p; } };... X4 obj4;obj4.setItem( ); cout << obj4.getItem();

10/21/2015Assoc. Prof. Stoyan Bonev90 The solution: generic class definition template class X { private: Type item; public:X() { item = 0;} X( Type v) { item = v; } Type getItem() { return item; } void setItem( Type p) { item = p; } };

10/21/2015Assoc. Prof. Stoyan Bonev91 The solution: generic class definition... X obj1; X obj2; X obj3; X obj4;

10/21/2015Assoc. Prof. Stoyan Bonev92 Demo program oop5b.cpp oop5b.exeoop5b.cppoop5b.exe Generic stack class definition template class Stack { public: Stack() {...} void push(TYPE var) {... } TYPE pop() {... } private: TYPE st[SIZE]; int sp; };... Stack c1;Stack c2;

10/21/2015Assoc. Prof. Stoyan Bonev93 Demo program oop5b.cpp template class Stack { private: TYPE st[SIZE]; int sp; public: Stack() { sp = -1; } // constructor void push(TYPE var) { if (sp < SIZE-1) { sp++; st[sp] = var; } else { cout << "\n Stack full\n"; getch(); exit(1); } } TYPE pop() { TYPE pom; if (sp>=0) { pom = st[sp]; sp--; return pom; } else { cout << "\n Stack empty\n"; getch(); exit(2); } } ~Stack() { cout << endl << "GOOD Bye"; } // Destructor };

10/21/2015Assoc. Prof. Stoyan Bonev94 Demo program oop5b.cpp main() { Stack c1; c1.push(22); c1.push(33); cout << c1.pop(); Stack M; M.push(11.2); M.push(22.4); M.push(33.6); M.push(44.8); M.push(55.9); M.push(66.6); cout << "\n\nStack contents" << endl; cout << M.pop() << endl; cout << M.pop() << endl; cout << M.pop() << endl; cout << M.pop() << endl; char ch; cin>> ch; }

10/21/2015Assoc. Prof. Stoyan Bonev95 Polymorphism in C++ classified Parametric (generic) polymorphism –Based oh template reserved word Static or ad-hoc polymorphism –Based on overloaded functions concept Dynamic polymorphism –Based on late binding and virtual methods

Thank You for Your attention!