Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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

3 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)

4 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(); }

5 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(); }

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

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

8 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

9 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 10/21/2015Assoc. Prof. Stoyan Bonev10 SmallObj – UML class diagram SmallObj somedata -somedata SetData(int) +SetData(int) ShowData() +ShowData()

11 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(); }

12 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

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

14 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"); }

15 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

16 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

17 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 */ }

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

19 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(); }

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

21 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(); }

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

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

24 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(); }

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

26 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(); }

27 More on Constructors/Destructors

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

29 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

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

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

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

33 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++; }

34 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);

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

36 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 }

37 Overloaded Operators

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

39 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

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

41 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);

42 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”; }

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

44 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

45 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

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

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

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

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

50 10/21/2015Assoc. Prof. Stoyan Bonev50 Relations classified http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html A-Kind-OfrelationshipA-Kind-Ofrelationship Is-A relationshipIs-A relationship

51 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”.

52 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

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

54 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 {... };

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

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

57 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(): }

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

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

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

61 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

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

63 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 }

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

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

66 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

67 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

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

69 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

70 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();

71 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

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

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

74 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

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

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

77 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:" ;} };

78 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 {... };

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

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

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

82 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()

83 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

84 Generic Classes in C++

85 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

86 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();

87 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();

88 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();

89 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(3.14159265); cout << obj4.getItem();

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

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

92 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;

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

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

95 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

96 Thank You for Your attention!


Download ppt "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."

Similar presentations


Ads by Google