Multiple-File Programming, Inheritance, and Templates in C++, Slides of Object Oriented Programming

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

2012/2013

Uploaded on 12/09/2013

atifarifasif
atifarifasif 🇵🇰

4.2

(14)

39 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Multiple-File Programs,
Inheritance, Templates
Multiple-File Programs
header files
implementation files
main-program file)
Inheritance in C++
Templates in C++
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

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

Why Multiple Files

• Re-use

• Better data abstraction (data hiding)

• More manageability of large programs

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:

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.

Redeclarations Issue Solution

• Inside each Z.h file, do:

  • Add to at the start of the file (right after all the

#include s) 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

Class Inheritance in C++

  • Inheritance allows us to create new classes which

are derived from older classes

  • The derived classes are called subclasses or simply derived classes
  • The older classes are superclasses or parent classes or base classes
  • A derived class automatically includes some of its

parent's members, and can have additional ones.

base subclass1 subclass Notation:

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)

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

rd

access mode: protected

  • Protected members are directly accessible by

derived classes but not by other users

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:

  • We have no idea how the string class is implemented, and we don’t care
  • CharStack inherited from string all the latter’s public methods, including size( ) and empty( )
  • We implemented push( ), pop( ) and peek( ) using the public methods of the string class

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 )

  • 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(x, 6 ); double miny= min< double >(y, 3 );

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 type 1 , class type 2 > funct_decl;

Class Templates Syntax

• For class template declaration:

• For the implementation of the methods

outside the class, the syntax is:

• For the implementation of the constructors

outside the class, the syntax is:

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