C++: Interfaces, Templates, Operator Overload & Exceptions

C++: Interfaces, Templates, Operator Overload & Exceptions

C++: Interfaces, Templates, Operator Overload & Exceptions David Marshall School of Computer Science & Informatics Cardiff University CM2204 All Lecture notes, code listings on CM2204 Web page David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Interfaces: Recap on Classes so Far C++ allows you to hide the implementation details of a class. By hiding the implementation details I Your program is forced to go through the interface routines your class provides. I when you change the implementation, all you have to do is make whatever changes are necessary to the class's interface, without having to modify the rest of your program. Access Specifier C++ allows you to assign an access specifier to any of a class's data members and member functions. David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Access Specifier The access specifier defines which of your program's functions have access to the specified data member or function. The access specifier must be I public | complete access to the member function or data member, limited only by scope. I private | access to it is limited to member functions of the same class I protected { as private but data member or function can also be accessed by a class derived from the current class or friend. David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions C++ Interfaces: abstract classes C++ interfaces implemented using abstract classes I not be confused with data abstraction which is a concept of keeping implementation details separate from associated data. A class is made abstract by declaring at least one of its functions as a pure virtual function (See previous lecture). Recap: A pure virtual function is specified by placing "= 0" in its declaration as follows: c l a s s Box f p u b l i c: // pure virtual function v i r t u a l double getVolume() = 0; p r i v a t e: d o u b l e length; // Length ofa box d o u b l e breadth; // Breadth ofa box d o u b l e height; // Height ofa box g ; David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Abstract class I The purpose of an abstract class is to provide an appropriate base class from which other classes can inherit. I Abstract classes cannot be used to instantiate objects and serves only as an interface. I Attempting to instantiate an object of an abstract class causes a compilation error. I Thus, if a subclass of an abstract class needs to be instantiated, I it has to implement each of the virtual functions, which means that it supports the interface declared by the abstract class. I Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error. I Classes that can be used to instantiate objects are called concrete classes. David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Abstract Class Example Consider the following example where parent class provides an interface to the base class to implement a function called getArea(): Shape.cpp #include <i o s t r e a m> u s i n g namespace std; // Base class c l a s s Shape f p u b l i c: // pure virtual function providing interface framework. v i r t u a l int getArea() = 0; v o i d setWidth(int w) f width = w; g v o i d setHeight(int h) f h e i g h t = h ; g p r o t e c t e d: i n t width; i n t height; g; David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Abstract Class Example Cont., Shape.cpp cont. // Derived classes c l a s s Rectangle: public Shape f p u b l i c: i n t getArea() f r e t u r n (width ∗ h e i g h t ) ; g g; c l a s s Triangle: public Shape f p u b l i c: i n t getArea() f r e t u r n (width ∗ h e i g h t ) / 2 ; g g; i n t main(void) f Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << " T o t a l R e c t a n g l e a r e a : " << Rect.getArea() << e n d l ; Tri.setWidth(5); Tri.setHeight(7); // Print the area of the object. cout << " T o t a l T r i a n g l e a r e a : " << Tri.getArea() << e n d l ; r e t u r n 0; g David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Templates When you design a class, you're forced to make some decisions about the data types that make up that class. I For example: If your class contains an array, the class declaration specifies the array's data type. In the following class declaration, an array of shorts is implemented: c l a s s Array f p r i v a t e: s h o r t arraySize; // Number of array elements s h o r t ∗ a r r a y P t r ; // Pointer to the array p u b l i c: Array ( short size ); // Allocate an array // of size shorts ~ Array ( ) ; // Delete the array g ; David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Array class example cont. In this class, I the constructor allocates an array of arraySize elements, each element of type short. I The destructor deletes the array. I The data member arrayPtr points to the beginning of the array. I To make the class truly useful, you'd probably want to add a member function that gives access to the elements of the array. David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Array class example cont. I What happens when you decide that an Array of shorts is not what you need? I Perhaps you need to implement an array of longs or, even better, an array of your own data types. I One approach you can use is to make a copy of the Array class (member functions and all) and change it slightly to implement an array of the appropriate type. E.g., An Array class designed to work with an array of longs: c l a s s LongArray f p r i v a t e: s h o r t arraySize; // Number of array elements l o n g ∗ a r r a y P t r ; // Pointer to the array p u b l i c: LongArray ( short size ); // Allocate an // array of size // longs ~LongArray( void); // Delete the array g; David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Not a great idea? There are definitely problems with this approach: I You are creating a maintenance nightmare by duplicating the source code of one class to act as the basis for a second class. I Suppose you add a new feature to your Array class. I Are you going to make the same change to the LongArray class? Templates to the rescue: C++ templates allow you to parameterise the data types used by a class (also a function). I Instead of embedding a specific type in a class declaration, you provide a template that defines the type used by that class. David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions C++ Template Definition Here's a templated version of the Array class: t e m p l a t e <c l a s sT > c l a s s Array f p r i v a t e: s h o r t arraySize; T ∗ a r r a y P t r ; p u b l i c: Array ( short size ); ~ Array ( void); g; I The keyword template tells the compiler that what follows is not your usual, run-of-the-mill class declaration. I Following the keyword template is a pair of angle brackets (<>) that surround the template's template argument list. I This list consists of a series of comma-separated arguments I one argument is the minimum David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Using a template definition Once your class template is declared, I You can use it to create an object. I When you declare an object using a class template I you have to specify a template argument list along with the class name. Here's an example: Array<long > longArray( 20 ); I The compiler uses the single parameter, long, to convert the Array template into an actual class declaration. I This declaration is known as a template instantiation. I The instantiation is then used to create the longArray object. David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions Function Templates The template technique can also be applied to functions, E.g.: t e m p l a t e <c l a s sT, classU > T MyFunc( T param1, U param2 ) f T v a r 1 ; U v a r 2 ; ...... g David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions A Template Example Consider an example program that provides a basic demonstration of class and function templates. template.cpp #include <i o s t r e a m> u s i n g namespace std; c o n s t short kNumElements = 10; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Array t e m p l a t e <c l a s sT > c l a s s Array f p r i v a t e: s h o r t arraySize; T ∗ a r r a y P t r ; T errorRetValue; p u b l i c: Array ( short size ); ~ Array ( ) ; T&operator[]( short index ); g; David Marshall C++: Interfaces, Templates, Operator Overload & Exceptions A Template Example Cont.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    40 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us