object-oriented programming using c++
DESCRIPTION
Object-Oriented Programming Using C++. CLASS 17. Objectives. Change a type specific Array class to a template. #include template class Array {friend ostream &operator> (istream &, Array &);. - PowerPoint PPT Presentation Copyright Complaint Adult Content Flag as Inappropriate Report This Download Presentation Object-Oriented Programming Using C++ An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - Presentation Transcript Object-Oriented Programming Using C++ CLASS 17 Objectives Change a type specific Array class to a template #include template class Array {friend ostream &operator> (istream &, Array &); Fig. 8.4, Pg. 474 #include template class Array {friend ostream &operator> (istream &, Array &); Fig. 8.4, Pg. 474 public: Array(int = 10); Array(const Array&); ~Array( ); int getSize( ) const; const Array &operator=(const Array &); bool operator= = ( const Array & ) const; bool operator!= ( const Array &right) const { return ! ( *this == right ); } int & operator[ ] ( int ); const int &operator[ ] ( int ) const; static int getArrayCount ( ); Fig. 8.4, Pg. 474TRANSCRIPT
![Page 1: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/1.jpg)
1IDLOOPC1998.
Object-Oriented Programming
Using C++
CLASS 17
![Page 2: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/2.jpg)
2IDLOOPC1998.
� Change a type specific Array class to a template
Ob
ject
ives
![Page 3: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/3.jpg)
3IDLOOPC1998.
#include <iostream.h>template <class T>class Array{ friend ostream &operator<<
(ostream &, const Array &);friend istream &operator>> (istream &, Array &);
Fig. 8.4, Pg. 474
![Page 4: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/4.jpg)
4IDLOOPC1998.
#include <iostream.h>template <class T>class Array{ friend ostream &operator<<
(ostream &, const Array<T> &);friend istream &operator>> (istream &, Array<T> &);
Fig. 8.4, Pg. 474
![Page 5: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/5.jpg)
5IDLOOPC1998.
public:Array(int = 10);Array(const Array&);~Array( );int getSize( ) const;const Array &operator=(const Array &);bool operator= = ( const Array & ) const;bool operator!= ( const Array &right) const
{ return ! ( *this == right ); }int & operator[ ] ( int );const int &operator[ ] ( int ) const;static int getArrayCount ( );
Fig. 8.4, Pg. 474
![Page 6: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/6.jpg)
6IDLOOPC1998.
public:Array(int = 10);Array(const Array<T> &);~Array( );int getSize( ) const;const Array<T> &operator= (const Array<T> &);bool operator==(const Array<T> &right) const;bool operator! =(const Array<T> &) const;{ return ! ( *this == right ); }
T &operator[]( int );const T &operator[] ( int ) const; static int getArrayCount( );
Fig. 8.4, Pg. 474
![Page 7: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/7.jpg)
7IDLOOPC1998.
private:int *ptr;int size;static int arrayCount;
};
Fig. 8.4, Pg. 474
![Page 8: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/8.jpg)
8IDLOOPC1998.
private:T *ptr;int size;static int arrayCount;
};
Fig. 8.4, Pg. 474
![Page 9: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/9.jpg)
9IDLOOPC1998.
// Initialize static data member at file scope
int Array::arrayCount = 0;
Fig. 8.4, Pg. 475
![Page 10: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/10.jpg)
10IDLOOPC1998.
// Initialize static data member at file scope
int Array<T>::arrayCount = 0;
Fig. 8.4, Pg. 475
![Page 11: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/11.jpg)
11IDLOOPC1998.
Array::Array(int arraySize){
size = ( arraySize > 0 ? arraySize : 10);ptr = new int[ size ];assert( ptr ! = 0 );++arrayCount;
for ( int i - 0; i < size; i++ )ptr[ i ] = 0;
}
Fig. 8.4, Pg. 475
![Page 12: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/12.jpg)
12IDLOOPC1998.
Fig. 8.4, Pg. 475
Array<T>::Array(int arraySize){
size = ( arraySize > 0 ? arraySize : 10);ptr = new T [ size ];assert( ptr ! = 0 );++arrayCount;
for ( int i - 0; i < size; i++ )ptr[ i ] = 0;
}
![Page 13: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/13.jpg)
13IDLOOPC1998.
Array::Array(const Array &init):size( init.size){
ptr = new int[ size ];assert( ptr != 0 );++arrayCount;
for ( int i = 0; i < size; i++ )ptr[ i ] = init.ptr[ i ];
}
Fig. 8.4, Pg. 475
![Page 14: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/14.jpg)
14IDLOOPC1998.
Fig. 8.4, Pg. 475
Array<T>::Array(const Array <T>&init):size( init.size)
{ptr = new T[ size ];assert( ptr != 0 );++arrayCount;
for ( int i = 0; i < size; i++ )ptr[ i ] = init.ptr[ i ];
}
![Page 15: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/15.jpg)
15IDLOOPC1998.
Array::~Array( ){
--arrayCount;delete [ ] ptr;
}// Get the size of the arrayint Array::getSize( ) const{
return size;}
Fig. 8.4, Pg. 475
![Page 16: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/16.jpg)
16IDLOOPC1998.
Array<T>::~Array( ){
--arrayCount;delete [ ] ptr;
}// Get the size of the arrayint Array<T>::getSize( ) const{
return size;}
Fig. 8.4, Pg. 475
![Page 17: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/17.jpg)
17IDLOOPC1998.
//Overloaded assignment operatorconst Array &Array::operator
=(const Array &right) {if ( size != right.size ) {delete [ ] ptr;size = right.size;ptr = new int[ size ];assert( ptr ! = 0 );
}for (int i = 0; i < size; i++) ptr[ i ] = right.ptr[ i ]; }return *this;
}
Fig. 8.4, Pg. 476
![Page 18: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/18.jpg)
18IDLOOPC1998.
Fig. 8.4, Pg. 476
//Overloaded assignment operatorconst Array <T>&Array<T>::operator
=(const Array <T>&right) {if ( size != right.size ) {delete [ ] ptr;size = right.size;ptr = new T[ size ];assert( ptr ! = 0 );
}for (int i = 0; i < size; i++) ptr[ i ] = right.ptr[ i ]; }return *this;
}
![Page 19: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/19.jpg)
19IDLOOPC1998.
int Array::operator==(constArray & right) const
{if (size != right.size)
return false;for (int i = 0; i < size; i++)
if (ptr[i] != right.ptr[i])return false;
return true;}
Fig. 8.4, Pg. 476
![Page 20: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/20.jpg)
20IDLOOPC1998.
int Array<T>::operator==(constArray<T> & right) const
{if (size != right.size)
return false;for (int i = 0; i < size; i++)
if (ptr[i] != right.ptr[i])return false;
return true;}
Fig. 8.4, Pg. 476
![Page 21: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/21.jpg)
21IDLOOPC1998.
const int &Array::operator[ ] (int subscript){
// check for subscript out of range errorassert(0<=subscript&&subscript<size);
return ptr[subscript];}
Fig. 8.4, Pg. 476
![Page 22: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/22.jpg)
22IDLOOPC1998.
const T &Array<T>::operator[ ] (int subscript)
{// check for subscript out of range errorassert(0<=subscript&&subscript<size);
return ptr[subscript];}
Fig. 8.4, Pg. 476
![Page 23: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/23.jpg)
23IDLOOPC1998.
int Array::getArrayCount( ) { return arrayCount; }
istream &operator>>(istream &input,Array &a)
}for (int i = 0; i < a.size; i++)
input >> a.ptr[i];return input;
}
Fig. 8.4, Pg. 477
![Page 24: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/24.jpg)
24IDLOOPC1998.
int Array<T>::getArrayCount( ) { return arrayCount; }
istream &operator>>(istream &input,Array<T> &a)
}for (int i = 0; i < a.size; i++)
input >> a.ptr[i];return input;
}
Fig. 8.4, Pg. 477
![Page 25: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/25.jpg)
25IDLOOPC1998.
ostream &operator<<(ostream &output, const Array &a)
{for (int i = 0; i < a.size; i++) {
output << a.ptr[i] << ‘ ‘;if ((i + 1) % 4 = = 0)
output << endl;}
if (i % 4 ! = 0)output << endl;
return output; }
Fig. 8.4, Pg. 477
![Page 26: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/26.jpg)
26IDLOOPC1998.
ostream &operator<<(ostream &output, const Array<T> &a)
{for (int i = 0; i < a.size; i++) {
output << a.ptr[i] << ‘ ‘;if ((i + 1) % 4 = = 0)
output << endl;}
if (i % 4 ! = 0)output << endl;
return output; }
Fig. 8.4, Pg. 477
![Page 27: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/27.jpg)
27IDLOOPC1998.
main ( ){
cout << “# of arrays instantiated =”<< Array::getArrayCount( );
Array integers1 (7), integers2;cout << “# of arrays instantiated =”
<< Array::getArrayCount( )<<endl << endl;
Fig. 8.4, Pg. 477
![Page 28: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/28.jpg)
28IDLOOPC1998.
main ( ){
cout << “# of arrays instantiated =”<< Array<int>::getArrayCount( );
Array<int> integers1 (7), integers2;cout << “# of arrays instantiated =”
<< Array<int>::getArrayCount( )<<endl << endl;
Fig. 8.4, Pg. 477
![Page 29: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/29.jpg)
29IDLOOPC1998.
Garage Stack Template
![Page 30: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/30.jpg)
30IDLOOPC1998.
The SCRATCHEMUP parking garage contains a single lane that holds up to 10 cars. There is only a single entrance/exit to the garage at one end of the lane. If a customer arrives to pick up a car that is not nearest the exit, all cars blocking it’s path are moved out, the customer’s car is driven out, and the other cars are restored in the same order that they were in originally.
![Page 31: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/31.jpg)
31IDLOOPC1998.
Write a program that processes a group of input lines. Each input line contains an ‘A’ for arrival or a ‘D’ for departure, and a license plate number. Cars are assumed to arrive and depart in the order specified by the input. The program should print a message whenever a car arrives or departs. When a car arrives, the message should specify whether or not there is room for the car in the garage. If there is no room, the car leaves without entering the garage.
![Page 32: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/32.jpg)
32IDLOOPC1998.
GARAGE
121
![Page 33: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/33.jpg)
33IDLOOPC1998.
GARAGE
121 333
![Page 34: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/34.jpg)
34IDLOOPC1998.
GARAGE
121 333 297
![Page 35: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/35.jpg)
35IDLOOPC1998.
GARAGE
121 333
297
![Page 36: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/36.jpg)
36IDLOOPC1998.
GARAGE
121
333297
![Page 37: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/37.jpg)
37IDLOOPC1998.
GARAGE
333297
![Page 38: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/38.jpg)
38IDLOOPC1998.
GARAGE
333
297
![Page 39: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/39.jpg)
39IDLOOPC1998.
GARAGE
333 297
![Page 40: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/40.jpg)
40IDLOOPC1998.
Exercises
Page 665
![Page 41: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/41.jpg)
41IDLOOPC1998.
Exercises
a) friend void f1 ( );
Page 665
![Page 42: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/42.jpg)
42IDLOOPC1998.
Exercises
a) friend void f1 ( );
friend of all template classes instantiated
Page 665
![Page 43: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/43.jpg)
43IDLOOPC1998.
Exercises
b) friend void f2(C1<T1> &);
Page 665
![Page 44: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/44.jpg)
44IDLOOPC1998.
Exercises
b) friend void f2(C1<T1> &);
friends of only a friend of C1 <particular type>
Page 665
![Page 45: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/45.jpg)
45IDLOOPC1998.
Exercises
c) friend void C2::f4( );
Page 665
![Page 46: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/46.jpg)
46IDLOOPC1998.
Exercises
c) friend void C2::f4( );
f4 is part of C2 class a friend of all
Page 665
![Page 47: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/47.jpg)
47IDLOOPC1998.
Exercises
d) friend void C3<T1>::f5 (C1<T1> &);
Page 665
![Page 48: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/48.jpg)
48IDLOOPC1998.
Exercises
d) friend void C3<T1>::f5 (C1<T1> &);
f5 is part of C3 <particular type>only friend of C1 <particular type>
Page 665
![Page 49: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/49.jpg)
49IDLOOPC1998.
Exercises
e) friend class C5;
Page 665
![Page 50: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/50.jpg)
50IDLOOPC1998.
Exercises
e) friend class C5;
friend of all
Page 665
![Page 51: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/51.jpg)
51IDLOOPC1998.
Exercises
f) friend class C6<T1>;
Page 665
![Page 52: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/52.jpg)
52IDLOOPC1998.
Exercises
f) friend class C6<T1>;
friend of all members of class C6 <particular type>friends of class C1 <Particular type>
Page 665
![Page 53: Object-Oriented Programming Using C++](https://reader030.vdocuments.site/reader030/viewer/2022032607/56813039550346895d95d0d0/html5/thumbnails/53.jpg)
53IDLOOPC1998.
Q & A