Download Multiple-File Programming, Inheritance, and Templates in C++ and more Slides Object Oriented Programming in PDF only on Docsity! Multiple-File Programs, Inheritance, Templates • Multiple-File Programs – header files – implementation files – main-program file) • Inheritance in C++ • Templates in C++ docsity.com Why Multiple Files • Re-use • Better data abstraction (data hiding) • More manageability of large programs docsity.com What should Go into a .h File • Only declarations (i.e., info to the compiler) • Nothing that requires storage • Reason: – a header file gets included in several files of a project; – if storage for a variable (or for a code) is allocated multiple times, you get a multiple-definition compilation error • Advanced: One exception to this rule is static data (data local to a file), because the linker does not allocate multiple instances to static data. docsity.com Redeclarations Issues • An X.h file can have: #include “Y.h” • Consider this scenario: – The X.h file has: #include “Y.h” #include “Z.h” – The Y.h file has: #include “Z.h” – This means: the declarations in Z.h are included twice in X.h. The second declarations are called redeclarations • Class redeclarations are not allowed. • So, we have a problem docsity.com Redeclarations Issue Solution • Inside each Z.h file, do: – Add to at the start of the file (right after all the #includes) the next two lines: #ifndef Z_H_ // Not that Z is the name of .h file #define Z_H_ – Add the following line at the very end of Z.h (on a separate line): #enddef docsity.com Syntax for Inheritance class derivedClass : public baseClass { private : // Declarations of additional members, if needed. public: // Declarations of additional members, if needed. protected: // Declarations of additional members, if needed. } The derived class inherits from the base class: all public members, all protected members (see later), and the default constructor The additional members defined can have the same name (and type) as those of the base class (as when some base members are to be redefined) docsity.com “Protected” Access • We have seen two access modes in C++ classes: public and private – Public members are directly accessible by users of the class – Private members are NOT directly accessible by users of the class, not even by inheritors • There is a 3rd access mode: protected – Protected members are directly accessible by derived classes but not by other users docsity.com Example of Inherited Classes class Shape { protected: int width, height; public: void setDims (int a, int b){ width=a; height=b;} }; class Rectangle: public Shape { public: int area ( ) { return (width * height); } }; class Triangle: public Shape { public: int area ( ) { return (width * height/2); } }; class Square: public Rectangle { public: void setDims (int a){ width=a; height=a;} }; docsity.com Templates • We saw function templates early on • Templates allow us to turn the type of data into a parameter that can be changed at will • For example, we defined stacks/queues/trees of ints – If we want a stack/queues/trees of floats, we have to cut and paste, and change the data type from int to float – We reduced this effort by using: typedef int datatype; – That is still inconvenient, time-consuming, and error prone – With templates, we do not need to cut+paste+change docsity.com Function Templates (Reminder ) • Syntax for declaring a function template: template<class type> function_declaration; -Or- template<typename type> function_declaration; Example of a Function Template Declaration: // Returns the minimum of array x[ ]. The data // type of x[ ] is arbitrary & customizable template<typename T> T min(T x[], int length){ T m = x[0]; // M is the minimum so far for (int i=1;i<n;i++) if (x[i]<m) m=x[i]; return m; } Example of Use: int x[]= {11, 13, 5, 7, 4, 10}; double y[]= {4.5, 7.13, 17}; int minx = min<int>(x,6); double miny= min<double>(y,3); docsity.com Templates with More than One Generic Type • Templates can have several generic types • Syntax for their declaration: • class can be replaced by typename. template<class type1,class type2> funct_decl; docsity.com template <class T> class stack { private : T *dataptr; int top; int capacity; public: stack(int cap=100); int getCapacity() {return capacity;} void push(T b); T pop() {assert(top>0); return dataptr[--top];} bool isEmpty() {return top==0;} }; Stack as a Class Template docsity.com template<class T> stack<T>::stack(int cap){ top=0; capacity = (cap>0)?cap:100; dataptr = new T[capacity]; } template<class T> void stack<T>::push(T b){ if (top < capacity) dataptr[top++]=b; else{ capacity *=2; T *newptr=new T[capacity]; for(int k=0;k<capacity/2;k++) newptr[k]=dataptr[k]; delete [] dataptr; dataptr = newptr;dataptr[top++]=b; } } docsity.com A Complete Program Using a Stack Template #include <cstdlib> #include <iostream> using namespace std; // template stack definition goes here int main(int argc, char *argv[]){ stack<int> intS(5); // a stack of integers cout<<"intS capacity after construction = "<<intS.getCapacity()<<endl; int x[]={2,3,7,8,-10,14,5}; for (int i=0;i<7;i++) intS.push(x[i]); cout<<"intS capacity after pushing 7 elements="<< intS.getCapacity(); cout<<“\nEmptying intS: "; while (!intS.isEmpty()) cout<<intS.pop()<<"; "; cout<<endl; docsity.com