Reference: COS240 Syllabus COS240 O-O Languages AUBG, COS dept Lecture 01b Title: C++ as O-O Prog Lang (Review) Reference: COS240 Syllabus 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Lecture Contents C++ as O-O Prog Lang Brief review Comprehensive review Data Encapsulation and Data Hiding Inheritance Polymorphism 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Reminder - What is an object? OBJECT set of methods (member functions, methods) internal state (values of private data members) Operations Data 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Example #include <iostream> 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(); } 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev #include <iostream> 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(); } 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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. 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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. 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Defining the Class General syntax Access qualifiers: private and public Data members Usually data is private Member functions Usually functions are public 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Class SmallObj class SmallObj { private: int somedata; public: void SetData(int d) somedata = d; } void ShowData() cout << "\nData is =" << somedata; }; 6/12/2018 Assoc. Prof. Stoyan Bonev
SmallObj – UML class diagram SmallObj SmallObj somedata -somedata SetData(int) +SetData(int) ShowData() +ShowData() 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Using Class SmallObj void main() { SmallObj s1, s2; // defining objects s1.SetData(67); // sending messages s2.SetData(123); s1.ShowData(); s2.ShowData(); } 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
Class Part /modern version only/ { 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; } }; 6/12/2018 Assoc. Prof. Stoyan Bonev
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"); } 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
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 */ } 6/12/2018 Assoc. Prof. Stoyan Bonev
C++ objects as Data Types class Distance 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Class Distance class Distance { private: int feet; float inches; public: void SetDist(int ft, float in) { feet = ft; inches = in; } void GetDist() { cout <<"\nEnter feet:" ; cin >> feet; cout <<"\nEnter inches:"; cin >> 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(); 6/12/2018 Assoc. Prof. Stoyan Bonev
C++ objects as general purpose programming elements class Counter 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Class Counter class Counter { private: unsigned count; public: void SetCount(int val) { count = val; } void GetData() { cout <<"\nEnter data:"; cin >> 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(); c2.IncCount(); } 6/12/2018 Assoc. Prof. Stoyan Bonev
C++ objects Constructors and Destructors Class Counter class Distance 6/12/2018 Assoc. Prof. Stoyan Bonev
Constructors and Destructors Class Counter { private: unsigned count; public: Counter() { count = 0; } Counter(int val) { count = val; } void SetCount(int val) { count = val; } void GetData() { cout <<"\nEnter data:"; cin >> count;} void ShowData() { cout <<"\nData count is:" << count;} void IncCount() { count++; } unsigned GetCount() { return count; } }; 6/12/2018 Assoc. Prof. Stoyan Bonev
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(); } 6/12/2018 Assoc. Prof. Stoyan Bonev
Constructors and Destructors 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 <<"\nEnter feet:" ; cin >>feet; cout <<"\nEnter inches:"; cin >> inches; } void ShowDist() { cout <<“Feet=" << feet <<" Inches="<< inches; }; 6/12/2018 Assoc. Prof. Stoyan Bonev
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(); } 6/12/2018 Assoc. Prof. Stoyan Bonev
More on Constructors/Destructors
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) { } 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Copy constructor demo class CL { private: int x, y; public: CL(); . . . }; // user specified constructor CL::CL(){ cout<<“\nEnter x,y:”; cin>>x>>y; } void main() { CL obj1; // user specified constructor CL obj2=obj1; // default copy constructor CL obj3(obj1); . . . } 6/12/2018 Assoc. Prof. Stoyan Bonev
Assignment constructor demo class CL { private: int x, y; public: CL(); CL(CL&); . . . }; // user specified constructor CL::CL(){ cout<<“\nEnter x,y:”; cin>>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); . . . 6/12/2018 Assoc. Prof. Stoyan Bonev
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); 6/12/2018 Assoc. Prof. Stoyan Bonev
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++; } 6/12/2018 Assoc. Prof. Stoyan Bonev
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); 6/12/2018 Assoc. Prof. Stoyan Bonev
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; 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
Overloaded Operators
Assoc. Prof. Stoyan Bonev Introduction Why do we need operator overloading? For better readability Examples: Class Counter Class Distance 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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 6/12/2018 Assoc. Prof. Stoyan Bonev
Overloading binary operators 6/12/2018 Assoc. Prof. Stoyan Bonev
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); 6/12/2018 Assoc. Prof. Stoyan Bonev
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”; 6/12/2018 Assoc. Prof. Stoyan Bonev
Overloading unary operators 6/12/2018 Assoc. Prof. Stoyan Bonev
Counter overloaded operator ++ class Counter { void IncCount() { count++; } void operator++() {count++;} }; Counter c(100); c.IncCount(); ++c; c.operator++(); //OK 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
Counter overloaded operator ++ class Counter { private: unsigned count; public: Counter() { count = 0; } Counter(int val ) { count = val; } void IncCount() { count++; } void GetData() { cout <<"\nEnter data:"; cin >> count;} void ShowData() { cout <<"\nData count is:" << count; } unsigned GetCount() { return count; } // overloaded unary operator ++, first version Counter operator++() { count++; return Counter(count); } }; 6/12/2018 Assoc. Prof. Stoyan Bonev
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. 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev INHERITANCE Base class and Derived classes; Access control; Class hierarchies; Multiple inheritance. 6/12/2018 Assoc. Prof. Stoyan Bonev
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. 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Relations classified http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html A-Kind-Of relationship Is-A relationship 6/12/2018 Assoc. Prof. Stoyan Bonev
A-Kind-Of 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”. Illustration of ''a-kind-of'' class level relationship 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev A SubClass inherits all the attributes and behaviors of the SuperClass, and may have additional attributes and behaviors. . 6/12/2018 Assoc. Prof. Stoyan Bonev
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 { 6/12/2018 Assoc. Prof. Stoyan Bonev
Accessing base class members The protected access specifier class Base { private: . . . protected: . . . public: . . . }; 6/12/2018 Assoc. Prof. Stoyan Bonev
Substituting Base class members DecrementCounter (1st version) class CountDn : public Counter { . . . }; 6/12/2018 Assoc. Prof. Stoyan Bonev
Class CountDn (1st idea) class Counter { protected: unsigned count; public: void GetData() { cout <<"\nEnter data:"; cin >> 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(): } 6/12/2018 Assoc. Prof. Stoyan Bonev
Generalization in UML Class diagrams Counter #count +GetData() +ShowData() +IncCount() CountDn +DecCount() 6/12/2018 Assoc. Prof. Stoyan Bonev
Class CountDn (working demo) class Counter { protected: unsigned count; public: Counter() { count = 0; } Counter(int val ) { count = val; } void IncCount() { count++; } void GetData() { cout <<"\nEnter data:"; cin >> 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); } 6/12/2018 Assoc. Prof. Stoyan Bonev
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 <<"\nEnter data:"; cin >> 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); } 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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 6/12/2018 Assoc. Prof. Stoyan Bonev
Real Multiple Inheritance Student Employee diploma name, id Manager Scientist Laborer title publications 6/12/2018 Assoc. Prof. Stoyan Bonev
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 } 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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; | }; 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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. 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Polymorphism Early binding and Late binding; Regular member functions; virtual member functions; Methods accessed with pointers. 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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 6/12/2018 Assoc. Prof. Stoyan Bonev
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(); 6/12/2018 Assoc. Prof. Stoyan Bonev
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.cpp OOP3aEarly.exe 6/12/2018 Assoc. Prof. Stoyan Bonev
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. 6/12/2018 Assoc. Prof. Stoyan Bonev
Early Binding (at compile time) 6/12/2018 Assoc. Prof. Stoyan Bonev
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.cpp OOP3aLate.exe 6/12/2018 Assoc. Prof. Stoyan Bonev
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. 6/12/2018 Assoc. Prof. Stoyan Bonev
Late Binding (at run time) 6/12/2018 Assoc. Prof. Stoyan Bonev
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:" ;} }; 6/12/2018 Assoc. Prof. Stoyan Bonev
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 { . . . }; 6/12/2018 Assoc. Prof. Stoyan Bonev
Virtual method IsOutstanding() class Person { protected: char name[20]; public: void GetName() { cout << "\nEnter name:"; cin >> name; } void ShowName() cout << "\n Name is:" << name << " "; bool virtual isOutStanding() = 0; }; 6/12/2018 Assoc. Prof. Stoyan Bonev
Virtual method IsOutstanding() class Student : public Person { private: float score; public: void GetScore() cout << "\n Enter student's score:"; cin >> score; } bool isOutStanding() return (score > 98.0) ? true: false; }; 6/12/2018 Assoc. Prof. Stoyan Bonev
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; }; 6/12/2018 Assoc. Prof. Stoyan Bonev
Virtual method IsOutstanding() void main () { Person *PersPtr[100]; Student *StuPtr; Professor *ProPtr; int n=0; char choice; do { cout << "\n Enter student or professor (s/p)?:"; cin >> 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 << "\n\n Enter another (y/n)?:"; cin >> 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() 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
Generic Classes in C++
Assoc. Prof. Stoyan Bonev Generic Classes in C++ General form of a generic class definition: template <class parameters> followed by a class definition that may include the class parameters Class parameters form (there must be at least one): class identifier 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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(); 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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(); 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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(); 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev 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(3.14159265); cout << obj4.getItem(); 6/12/2018 Assoc. Prof. Stoyan Bonev
The solution: generic class definition template <class Type > class X { private: Type item; public: X() { item = 0;} X(Type v) { item = v; } Type getItem() { return item; } void setItem(Type p) { item = p; } }; 6/12/2018 Assoc. Prof. Stoyan Bonev
The solution: generic class definition . . . X<char> obj1; X<int> obj2; X<float> obj3; X<double> obj4; 6/12/2018 Assoc. Prof. Stoyan Bonev
Demo program oop5b.cpp oop5b.exe Generic stack class definition template <class TYPE, int SIZE> class Stack { public: Stack() { . . .} void push(TYPE var) { . . . } TYPE pop() { . . . } private: TYPE st[SIZE]; int sp; }; . . . Stack<int, 100> c1; Stack<float, 200> c2; 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Demo program oop5b.cpp template <class TYPE, int SIZE> 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 }; 6/12/2018 Assoc. Prof. Stoyan Bonev
Assoc. Prof. Stoyan Bonev Demo program oop5b.cpp main() { Stack<int, 100> c1; c1.push(22); c1.push(33); cout << c1.pop(); Stack<float, 10> 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; char ch; cin>> ch; } 6/12/2018 Assoc. Prof. Stoyan Bonev
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 6/12/2018 Assoc. Prof. Stoyan Bonev
Thank You for Your attention!