

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of multiple-file programs, inheritance, and templates in c++. It covers the benefits of using multiple files, the multiple-file approach, and the importance of avoiding redeclarations. The document also provides examples and syntax for inheritance and templates, as well as observations and additional resources.
Typology: Slides
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Example class Stack { public : typedef int dtype; Stack( int cap= 100 ); int getCapacity(); void push(dtype b); dtype pop(); dtype peek(); bool isEmpty(); private : dtype *dataptr; int top; int capacity; }; #include "Stack.h" Stack::Stack( int cap){ top= 0 ; capacity = (cap> 0 )?cap: 100 ; dataptr = new int [capacity]; }; int Stack::getCapacity( ) { return capacity; }; int Stack::dtype Stack::pop(){ assert (!isEmpty()); return dataptr[--top]; }; // the implementation of // the remaining methods *........ #include #include #include "Stack.h” using namespace std; // local stuff, if any int main(int argc, char argv[]){ ……… } Stack.h file: Stack.cpp file: Project1.cpp file:
base subclass1 subclass Notation:
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)
rd
Another Example of Inherited Classes (A char stack inherited from string) class CharStack: public string{ public : void push( char b){ string str; str += b; insert( 0 ,str);}; char peek( ){ return at( 0 );}; char pop( ){ char a=at( 0 ); erase( 0 , 1 ); return a; }; // size( ) and empty( ) are the // same in string, so are // inherited as is. } Observations:
More on Inheritance Syntax class derivedClass : protected baseClass { …}; // Effect: all public members inherited from baseClass are // now protected members of derivedClass class derivedClass : private baseClass { …}; // Effect: all public and protected members inherited from // baseClass are now private members of derivedClass Multiple inheritance A class can inherit several classes at once: class derivedClass : public baseClass 1 , public baseClass 2 { …}; Remark: Not recommended
Function Templates (Reminder )
Templates with More than One Generic Type
template < class T> class_declaration; template < class T> return_type className :: methodName (parameter-list){ ……} template < class T> className :: className (parameter-list){……}
template 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