reusing code in c++ has-a relationship classes with member objects(containment) the valarray...
TRANSCRIPT
![Page 1: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/1.jpg)
Reusing Code in C++
• Has-a relationship
• Classes with member objects(containment)
• The valarray template class
• Private & protected inheritance
• Multiple inheritance
• Virtual base class
• Template class
• Template specializations
![Page 2: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/2.jpg)
Classes with object members
• A student class– A name (string class)– A set of quiz scores– A fixed-size array? dynamic memory
allocation?– An array-of-double
![Page 3: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/3.jpg)
The valarray class
![Page 4: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/4.jpg)
The Student class design
• Containment (composition, layering) is a has-a relationship
• Program studentc.h, studentc.cpp, use_stuc.cpp
![Page 5: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/5.jpg)
Private inheritance
• With private inheritance, public and protected members of the base class become private members of the derived class
• Program studenti.h, studenti.cpp, use_studi.cpp
![Page 6: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/6.jpg)
Containment or private inheritance
• Has-a relationship can be generated by containment or private inheritance
• In general, it is easy to use containment to build up a “has-a” relationship
• Private inheritance provides some privileges, such as accessing protected member, or redefining virtual function, which can not be implemented by containment
![Page 7: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/7.jpg)
Containment or private inheritance
• We should use containment to model a has-a relationship
• Use private inheritance if the new class needs to access protected members in the original class or if it needs to redefine virtual functions
![Page 8: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/8.jpg)
Varieties of inheritance
![Page 9: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/9.jpg)
Redefining access with using
• Public members of a base class become protected or private when one uses protected or private derivation
![Page 10: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/10.jpg)
Multiple inheritance
• Program worker0.h, worker0.cpp, worktest.cpp
![Page 11: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/11.jpg)
Ambiguous inheritance
• Program worker.h, worker0.cpp, worktest.cpp // point out the ambiguities
![Page 12: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/12.jpg)
Type conversion
class SingingWaiter:public Singer, public Waiter { };
SingingWaiter ed;
Worker *pw = &ed; // ambiguous
Worker *pw1 = (Waiter *) &ed; // the Worker in Waiter
Worker *pw2 = (Singer *) &ed; // the Worker in Singer
![Page 13: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/13.jpg)
Inherit two base-class objects
![Page 14: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/14.jpg)
Virtual base classes
class Singer : virtual public Worker { };
class Waiter : public virtual Worker { };
class SingingWaiter: pubic Singer, public Waiter { …};
![Page 15: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/15.jpg)
Using virtual base class• If a class has an indirect inherit virtual base
class, a constructor for that class should explicitly invoke a constructor for the virtual base class unless all that is needed is the default constructor for the virtual base class– SingingWaiter(const Worker &wk, int p=0, int
v=Singer::other) : Waiter(wk, p), Singer(wk, v) { }
// flawed– SingingWaiter(const Worker &wk, int p=0, int
v=Singer::other) : Worker(wk), Waiter(wk, p), Singer(wk, v) { } // ok
![Page 16: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/16.jpg)
Indicate which method
• Multiple inheritance can result in ambiguous callsSingingWaiter newhire(“Elise Hawks”, 2005, 6, soprano);
newhire.Show(); // ambiguous
newhire.Singer::Show(); // use Singer version
void SingingWaiter::Show() // best way to do it
{ Singer::Show();}
![Page 17: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/17.jpg)
Problems shootingvoid Worker::Show() const{ cout << “Name:” << fullname << “\n”;
cout << “Employee ID:” << id << “\n”;}void Waiter::Show() const{ Worker::Show();
cout <<“Panache rating:” << panache << “\n”; }void HeadWaiter::Show() const{ Waiter::Show();
cout << “presence rating:” << presence << “\n”; }void SingingWaiter::Show(){ Singer::Show(); } // fail due to ignoring Waitervoid SingingWaiter::Show(){ Singer::Show(); Waiter::Show(); } // call Worker::show() twice
![Page 18: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/18.jpg)
void Worker::Data() const{ cout << “Name:” << fullname << “\n”;
cout << “Empolyee ID:” << id << “\n”; }void Waiter::Data() const{ cout << “Panache rating:” << panache << “\n”;void Singer::Data() const{ cout << “Vocal range:” << pv[voice] << “\n”;}void SingingWaiter::Data() const{ Singer::Data(); Waiter::Data(); }void SingingWaiter::Show() const{ cout << “Category: singing waiter\n”;
Worker::Data(); Data();}• Program workermi.h, workermi.cpp, workmi.cpp
Using modular approach
![Page 19: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/19.jpg)
Multiple inheritance synopsis
• A derived class with an indirect virtual base class should have its constructors invoke the indirect base-class constructors directly, which is illegal for indirect non-virtual base classes
• Name ambiguity is resolved via the dominance rule
![Page 20: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/20.jpg)
Class templates• Inheritance and containment aren’t
always the solution when you want to reuse code
• Drawbacks of different datatype– Edit header file each time when one changes
the type– One can use the technique to generate just
one kind of the class per program
• C++ class template provide a better way to generate generic class declaration
• C++’s Standard Template Library (STL)
![Page 21: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/21.jpg)
The original Stack class
![Page 22: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/22.jpg)
Using a class template Stack
Stack<int> kernels; // create a stack of int
Stack<string> colonels; // a stack of string objects
Template<class T>
void simple(T t) { cout << t << “\n”; }
…
simple(2); // generate void simple(int)
simple(“two”);// generate void simple(char *)• Program stacktp.h, stacktem.cpp
![Page 23: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/23.jpg)
Stack pointer – incorrect version
Stack<string> s; // original stackStack<char *> st; // create a stack for pointers-to-charstring po; • Three versions to replace “string po;”1. char *po; cin >> po; // error2. char po[40];
// template <class Type>// bool Stack<Type>::pop(Type &item)// { …; item = items[--top]; … }// item is Lvalue, cannot be array name
3. char *po = new char[40];// push to the same address, and pop the same address value// pop outcome is always the last push in data
![Page 24: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/24.jpg)
Correct version
• Program stcktp1.h, stkoptr1.cpp
![Page 25: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/25.jpg)
Array template• Template is usually used in container class
ArrayTP<ArrayTP<int, 5>, 10> twodee;
int twodee [10][5];• Program arraytp.h, twod.cpp• Generate 2 separate class declarations
• Just one class declaration through constructor
![Page 26: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/26.jpg)
Template versatility
![Page 27: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/27.jpg)
Using one than one type parameter
• Program pairs.cpp
![Page 28: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/28.jpg)
Template specializations
• Template specializations– Implicit instantiation– Explicit instantiation– Explicit specialization
• Partial specialization
![Page 29: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/29.jpg)
Implicit instantiation
• The compiler doesn’t generate an implicit instantiation of the class until it needs an object
![Page 30: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/30.jpg)
Explicit instantiation
• Generate ArrayTP<string, 100> class
• The compiler generates the class definition, including method definitions, even though no object of the class has yet been created or mentioned
![Page 31: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/31.jpg)
Explicit specialization• An explicit specialization is a definition for a
particular type of types that is to be used instead of the general template
• A specialized class template definition has the form
• To provide a SortedArray template specialized for the const char * type, using the current notation
![Page 32: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/32.jpg)
Partial specializations• C++ allows for partial specializations,
which partially restrict the generality of a template
![Page 33: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/33.jpg)
Partial specializations• Providing a special version for pointers• Making a variety of restrictions
• The compiler would make the following choices
![Page 34: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/34.jpg)
Member templates
• Member templates– Program tempmemb.cpp
• Templates as parameters– Program tempparm.cpp
![Page 35: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/35.jpg)
Template classes and friends
• Template class can have friends• 3 types of template friends
– Non-template friends– Bound template friends
• The type of the friend is determined by the type of the class when a class is instantiated
– Unbound template friends• All specializations of the friend are friends
to each specialization of the class
![Page 36: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/36.jpg)
Non-template friend functions to template
classes
• counts() function is a friend to HasFriend<int> or HasFriend<string>
![Page 37: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/37.jpg)
Bound template friends
• The compiler would replace the template parameter T with int, giving in this form
![Page 38: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/38.jpg)
Bound template friends
• The report() function is not itself a template function, it just has a parameter that is a template
• Need to define explicit specializations for the friends one plans to use
![Page 39: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/39.jpg)
Template class with non-template friends
• Program frnd2tmp.cpp
![Page 40: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/40.jpg)
Bound template friend functions to template
classes• Bound template friend involves three
steps• Step 1, declare each template function
• Step 2, declare the templates again
![Page 41: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/41.jpg)
Bound template friend functions to template
classes• Step 3, declare an object of a
particular specialization
• The compiler substitutes int for TT and generates the following class
![Page 42: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/42.jpg)
Template friend to a template class
• Program tmp2tmp.cpp
![Page 43: Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance](https://reader035.vdocument.in/reader035/viewer/2022062301/5697bf981a28abf838c913bd/html5/thumbnails/43.jpg)
Unbound template friend functions to template
classes
• show2(hfd, hfi2) gets matched to this specialization
• Program manyfrnd.cpp