reusing code public inheritance is one mechanism. class template using class member which are object...

49
Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment , composition or layering). private or protected inheritance,

Post on 20-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Reusing Code

Public inheritance is one mechanism.

Class template

Using class member which are object of other classes. ( containment , composition or layering).

private or protected inheritance,

Page 2: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Class Template

Page 3: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Stack and Queue are examples of container classes,( to hold other objects or data types).

We can define stack and queue class as generic type. Define as Template .

Consider the stack class as follow :

Page 4: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

typedef unsigned long Item; class Stack{ private : enum {MAX=10}; Item items[MAX]; // stack items int top; public : Stack(); bool isempty(); bool isfull(); bool push( const Item & item); bool pop( Item & item); // pop top into item};

Page 5: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

#ifndef STACKP_H_#define STACKP_H_template <class Type> // template <typename type>// It doesn’t mean that Type must be a classclass Stack{ private : enum {MAX=20}; Type items[MAX]; // stack items int top; public : Stack(); bool isempty(); bool isfull(); bool push( const Type & item); bool pop( Type & item); // pop top into item};

Page 6: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class Type> Stack<Type> :: Stack(){ top=0; }

template <class Type>bool Stack<Type> :: isempty( ){ return top==0; }

template <class Type>bool Stack<Type> :: isfull( ){ return top==MAX;}

Page 7: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class Type>

bool Stack<Type> :: push(const Type & item)

{

if( top < MAX)

{

items[top++]=item;

return true;

}

else

return false;

}

Page 8: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class Type>bool Stack<Type> :: pop( Type & item){ if( top > 0) { item=items[--top]; return true; } else return false; }#endif

Page 9: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

If the compiler supports export keyword we can put the stack definition class in a separate file as follow :

#ifndef STACKP_H_#define STACKP_H_export template <class Type>class Stack{….};

and the rest as usual.

OTHEWISE place the template class definitions and header file together, in a same file .

Page 10: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

In this example we want to print all sequences of 1,2,3,…,n

which can be obtained by push and pop operator.

We have sequence 1,2,3,…,n , each time either we read one number (from left to right) and push to stack, or we don’t read any number and we pop a number from stack and we print it.

For example by applying , push push pop push pop, pop on sequence 1,2,3 we get output

2 3 1.

We can’t get 3 1 2 from push and pop.

Page 11: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

#include<iostream>#include<string>#include<cctype>#include “stackp.h”Stack<int> st;void stacki_string(int n, int flag, int Max_size, int *array);

int main(){ int n; cout<<“ enter a positive integer n”; cin>>n; int * array=new int[2*n]; stacki_string(0,0,2*n, array); return 0;}

Page 12: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

void stacki_string(int n, int flag, int Max_size, int *arr){ if( flag < 0 || flag > (Max_size-n) ) return;

if( n== Max_size) { int i; int j=1; int top_item; for( i=0; i<n; i++) { if ( arr [i] ==1) { st.push(j); j++; } if( arr[i] == 0) { st.pop(top_item); cout<<top_item<<“ “; } }

Page 13: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

cout<<endl;

return;

}

arr[n]=1;

stacki_string(n+1, flag+1,Max_size,arr);

arr[n]=0;

stacki_string(n+1, flag-1,Max_size,arr);

}

Page 14: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Question ?

Given a sequence of number, decide whether it can be obtained by series of push and pop? If yes, how?

Page 15: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Using stack of pointers#ifndef STACKP1_H_#define STACKP1_H_template <class Type> class Stack{ private : enum {SIZE=20}; //defualt size const int stacksize; Type * items; //holds stack items int top; public : explicit Stack(int ss=SIZE);// If we don’t use explicit the implicit conversion happens Stack(const Stack & st); ~Stack( ) { delete [ ] items; }

Page 16: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

bool isempty() {return top==0; }

bool isfull() {return top==stacksize ; }

bool push( const Type & item);

bool pop( Type & item); // pop top into item

Stack & operator=(const Stack & st);

};

template <class Type>

Stack<Type> :: Stack(int ss) : stacksize(ss), top(0)

{ items=new Type[stacksize]; }

Page 17: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

// copy constructortemplate <class Type> Stack<Type> :: Stack(const Stack &st ) { stacksize=st.stacksize; top=st.top; delete[ ] items; items=new Type[stacksize]; for( int i =0; i< top; i++) items[i]=st.items[i];}

Page 18: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class Type>

bool Stack<Type> :: push(const Type & item)

{

if( top < stacksize)

{

items[top++]=item;

return true;

}

else

return false;

}

Page 19: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class Type>bool Stack<Type> :: pop( Type & item){ if( top > 0) { item=items[--top]; return true; } else return false; }

Page 20: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

// overloading the assignment operator we use dynamic //memory allocation

template <class Type>Stack<Type> & Stack<Type>::operator=(const Stack<Type> & st) { if( this == &st) return *this; delete [ ] items; stacksize = st.stacksize; top=st.top; items = new Type[stacksize]; for( int i=0; i< top; i++) items[i]=st.items[i]; return *this;}#endif

Page 21: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

#include<iostream.h>#include<cstdlib>#include<ctime>#include “stackp1.h”const int Num=10;int main(){ srand(time(0)); // randomize rand() cout<<“Please enter stack size”; int stacksize; cin>>stacksize; Stack<const char *> st(stacksize); const char * in[Num]={ “1: Dave Cohen”, “2: Anders Yeo”, “3: Gregory Gutin”, “4: Allan Davis”, “5: Janet Hales”, “6:

Amanda Baker”, “7 : Martin Green”, “8: Bob Vickers”, “9: Alex Clark”, “10: liz Hogger” };

Page 22: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

const char * out[Num]; int processed =0; int nextin=0;

while( processed < Num) { if(st.isempty()) st.push(in[nextin++]); else if (st.isfull()) st.pop(out[processed++]); else if ( rand() %2 && nextin < Num) st.push(in[nextin++]); else st.pop( out[processed++]); }

for(int i=0; i<Num;i++) cout<<out[i]<<endl;

return 0;}

Page 23: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Run :Please enter stack size:102: Anders Yeo1: Dave Cohen3: Gregory Gutin5: Janet Hales4: Allan Davis7: Martin Green9: Alex Clark8: Bob Vickers6: Amanda Baker10: liz Hogger

Page 24: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Please enter stack size:5

3: Gregory Gutin

5: Janet Hales6: Amanda Baker

4: Allan Davis

8: Bob Vickers

9: Alex Clark

10: liz Hogger

7: Martin Green

2: Anders Yeo

1: Dave Cohen

Page 25: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Array Template

Using template argument to provide the size of a regular array rather than dynamic array.

Page 26: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

#ifndef ARRAYTP_H_#define ARRAYTP_H_#include<iostream>#include<cstdlib>template <class T, int n>class ArrayTP{ private : T ar[n]; public : ArrayTP() { }; explicit ArrayTP( const T &v); virtual T & operator[ ]( int i); virtual T operator[ ]( int i) const;

};

Page 27: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class T, int n>ArrayTP<T,n>::ArrayTP (const T & v) { for(int i=0; i<n;i++) ar[i]=v;};template <class T, int n>T & ArrayTP<T,n>::operator[ ]( int i){ if( i<0 || i>=n) { cout<<“error in array lim “<<“ out of rang “<<endl; exit(1); } return ar[i];}

Page 28: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class T, int n>T ArrayTP<T,n>::operator[ ]( int i) const{ if( i<0 || i>=n) { cout<<“error in array lim “<<“ out of rang

“<<endl; exit(1); } return ar[i];}#endif

Page 29: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

#include<iostream.h>#include<cstdlib>#include "arraytp.h"int main(){ ArrayTP<double, 10> sample; ArrayTP<double, 10> sample1(5.5); int i; for(i=0; i< 10; i++)

cout<<sample1[i]<<" ";

double *pt; pt=&sample1[4]; *pt=7.7; for(i=0; i< 10; i++)

cout<<sample1[i]<<" "; cout<<endl; return 0;}

Page 30: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class T, int n>Here int n is called non-type or

expression ,argument.

ArrayTP<double , 12> sample; compiler defines a class called ArrayTP<double ,

12> and sample is an object of it.

An expression argument can be an enumeration type, a reference, a pointer.

Not a double, but double &rm or double *pm are allowed . We can’t alter the value of expression argument .

Page 31: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

When we instantiate a template, the value used for the expression argument should be a constant expression.

The expression argument has no advantage over the constructor approach. CA approach uses heap memory and EA uses the memory stack for automatic variables which is faster in run time.

Page 32: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Template class as base class

template <class T>

class Array

{

private :

T entry;

….

};

Page 33: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

template <class Type> class Growarray : public Array<Type> {…}; // inheritance

template <class Tp> class Stack { Array<Tp> ar; //use an array <> as a component …};…Array <Stack<int> > asi; // an array of stack of int

Page 34: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

ArrayTp< ArrayTp<int, 5>, 10 > tw;

What is tw;

Page 35: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

ArrayTp< ArrayTp<int, 5>, 10 > tw;

tw is an array of 10 element, each is an array of five int.

is the same as

int tw[10][5];

Page 36: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Write a program based on Graphical User Interface (GUI) using Microsoft Visual Studio C++ 6.0 to model the registration of students for university courses.

Students request courses and the university offers courses. Your program handles the

the requests and offers.

Page 37: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Students submit a single application which includes requests for registration in chosen courses. The university offers vacancies in courses any number of times.

Student requests are always considered in the order in which their applications were received.

Students can register for a maximum of ten courses.

Page 38: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Because the number of vacancies in

courses is limited they may apply for any number of courses. If vacancies are available they are registered in these courses in descending order of preference up to a maximum of ten courses.

Each time vacancies in courses are offered by the university, the requests of each student are reviewed in the order of preference specified by each student and in the order in which student applications were received.

Page 39: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

If a student is registered for the maximum number of course when a vacancy is offered in a course which has higher

preference than one of the courses in which he is already registered then he is registered for the higher preference course and his lowest preference registration is cancelled and this newly released course vacancy is offered to other students on the next round.

Page 40: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Program Inputs Students’ registration requests and university

course vacancy offers are input from the keyboard via appropriate GUI controls. You are responsible to design your GUI to allow your application to take inputs only via GUI.

Usability The user can also request various reports

describing the state of the system. All output is written to appropriate GUI control tools. Note

that output is produced only in response to the user's request for reports.

Page 41: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Main InterfaceThe front page of your GUI must support the

following services:

– Course Offer– Registration Request– Student Report– Course Report– Course Summary– Student Summary– Quit

Please Select Service….

Page 42: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Sample Input

Command: Course OfferCMPT212 (Course code)

Object Oriented Cooking (Course title)

100 (Number of places offered)

1234.56 (Cost of course)

Page 43: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Command: Registration Request

R123456 (Student registration number)

Moyo, J (Student name)

BSc (Student degree)

CMPT123 (Student course request)

CMPT234 (Student course request)

CMPT110 (Student course request)

(Any number of courses may be requested)

Page 44: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Command: Student Report RequestR123456A (For student registration number)CMPT212 (for course code)

Command: Course Report RequestCMPT212 (for course code)

Command: Course Summary Request

Command: Student Summary Request

Page 45: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Output Reports

The Student Report Request command above should produce a report similar to the following sample:

R123456A Moyo, J BSc $2345.56

Registered: CMPT123 CMPT234 MATH567

Page 46: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

The Course Report Request command above should produce a report similar to the

following sample:

CMPT212 Pure Object Oriented Programming ()

Offered: 100 Registered: 50

Page 47: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

The Course Summary Request command above should produce a report similar to the following sample showing the total number of students currently registered for each course and the total cost of offering the course (computed from the number of students registered for the course multiplied by the course fee):

Code Registration Total CostCT101 200 5000.00CT102 135 4326.00XY345 56 1234.56...

Page 48: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

The Student Summary Request command above should produce a report similar to the following sample showing each student, degree, number of courses registered for and total cost of those courses:

ID Number Degree #Courses Fee R123456A BSc 4 2345.56 R234567B MSc 5 3456.78 ...

Page 49: Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering)

Submission Date

30th July 2007

13% of the total mark.