Download presentation
Presentation is loading. Please wait.
1
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,
2
Class Template
3
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 :
4
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 };
5
#ifndef STACKP_H_ #define STACKP_H_ template // template // It doesn’t mean that Type must be a class class 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 };
6
template Stack :: Stack() { top=0; } template bool Stack :: isempty( ) { return top==0; } template bool Stack :: isfull( ) { return top==MAX; }
7
template bool Stack :: push(const Type & item) { if( top < MAX) { items[top++]=item; return true; } else return false; }
8
template bool Stack :: pop( Type & item) { if( top > 0) { item=items[--top]; return true; } else return false; } #endif
9
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 Stack { …. }; and the rest as usual. OTHEWISE place the template class definitions and header file together, in a same file.
10
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.
11
#include #include “stackp.h” Stack 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; }
12
void stacki_string(int n, int flag, int Max_size, int *arr) { if( 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<<“ “; }
13
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); }
14
Question ? Given a sequence of number, decide whether it can be obtained by series of push and pop? If yes, how?
15
Using stack of pointers #ifndef STACKP1_H_ #define STACKP1_H_ template 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; }
16
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 Stack :: Stack(int ss) : stacksize(ss), top(0) { items=new Type[stacksize]; }
17
// copy constructor template Stack :: 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]; }
18
template bool Stack :: push(const Type & item) { if( top < stacksize) { items[top++]=item; return true; } else return false; }
19
template bool Stack :: pop( Type & item) { if( top > 0) { item=items[--top]; return true; } else return false; }
20
// overloading the assignment operator we use dynamic //memory allocation template Stack & Stack ::operator= (const Stack & 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
21
#include #include “stackp1.h” const int Num=10; int main() { srand(time(0)); // randomize rand() cout<<“Please enter stack size”; int stacksize; cin>>stacksize; Stack 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” };
22
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; }
23
Run : Please enter stack size:10 2: Anders Yeo 1: Dave Cohen 3: Gregory Gutin 5: Janet Hales 4: Allan Davis 7: Martin Green 9: Alex Clark 8: Bob Vickers 6: Amanda Baker 10: liz Hogger
24
Please enter stack size:5 3: Gregory Gutin 5: Janet Hales 6: Amanda Baker 4: Allan Davis 8: Bob Vickers 9: Alex Clark 10: liz Hogger 7: Martin Green 2: Anders Yeo 1: Dave Cohen
25
Array Template Using template argument to provide the size of a regular array rather than dynamic array.
26
#ifndef ARRAYTP_H_ #define ARRAYTP_H_ #include template class ArrayTP { private : T ar[n]; public : ArrayTP() { }; explicit ArrayTP( const T &v); virtual T & operator[ ]( int i); virtual T operator[ ]( int i) const; };
27
template ArrayTP ::ArrayTP (const T & v) { for(int i=0; i<n;i++) ar[i]=v; }; template T & ArrayTP ::operator[ ]( int i) { if( i =n) { cout<<“error in array lim “<<“ out of rang “<<endl; exit(1); } return ar[i]; }
28
template T ArrayTP ::operator[ ]( int i) const { if( i =n) { cout<<“error in array lim “<<“ out of rang “<<endl; exit(1); } return ar[i]; } #endif
29
#include #include "arraytp.h" int main() { ArrayTP sample; ArrayTP 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; }
30
template Here int n is called non-type or expression,argument. ArrayTP sample; compiler defines a class called ArrayTP 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.
31
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.
32
Template class as base class template class Array { private : T entry; …. };
33
template class Growarray : public Array {…}; // inheritance template class Stack { Array ar; //use an array <> as a component … }; … Array > asi; // an array of stack of int
34
ArrayTp, 10 > tw; What is tw;
35
ArrayTp, 10 > tw; tw is an array of 10 element, each is an array of five int. is the same as int tw[10][5];
36
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.
37
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.
38
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.
39
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.
40
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.
41
Main Interface The 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….
42
Sample Input Command: Course Offer CMPT212 (Course code) Object Oriented Cooking (Course title) 100 (Number of places offered) 1234.56 (Cost of course)
43
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)
44
Command: Student Report Request R123456A (For student registration number) CMPT212 (for course code) Command: Course Report Request CMPT212 (for course code) Command: Course Summary Request Command: Student Summary Request
45
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
46
The Course Report Request command above should produce a report similar to the following sample: CMPT212 Pure Object Oriented Programming () Offered: 100 Registered: 50
47
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 Cost CT101 200 5000.00 CT102 135 4326.00 XY345 56 1234.56...
48
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...
49
Submission Date 30th July 2007 13% of the total mark.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.