Download presentation
Presentation is loading. Please wait.
1
1 Lab Session-6 CSIT221 Fall 2002 A Note About Destructors Using Pointers Implementing Stacks
2
2 Destructors Destructors perform the opposite function of constructors A destructor is a member functions of a class and it has the same name as the class Destructor runs automatically when an object belonging to its class is destroyed. It ensures “clean up” operations See an example source code
3
3 Example: Class Definition #include using namespace std; class menu { public: menu(); bool reserved(); void viewseats(); ~menu(); private: char bus [9][4]; };
4
4 Example: Member Functions bool menu::reserved() { cout<<"Reserving....."<<endl; return true; } void menu::viewseats() { cout<<"View seats....."<<endl; }
5
5 Example: Constructor and Destructor menu::menu() { cout<<"Constructor runs now"<<endl; } menu::~menu() { cout<<"Destructor runs now"<<endl; }
6
6 Example: Client Code void main() { menu mymenu; if (mymenu.reserved()) cout<<“Runs OK”<<endl; mymenu.viewseats(); }
7
7 Lab Exercise (Demo Not Required) Define a pointer variable. Get an array of integers from the system using this pointer. Initialize the array to all odd integers from 0 to 20. Write a for loop to sum the contents of the array and produce the result. Delete the array (Use delete [] ptr;) and print the sum.
8
8 Lab Exercise (Demo Due Oct 8 th Sec 01 and Oct 9 th Sec 02) (Section 2 will use the assignment submission page located at http://www.cs.fredonia.edu/~zubairi/submit_221.html to submit this lab due to the start of Fall Break on Oct 10th). http://www.cs.fredonia.edu/~zubairi/submit_221.html Implement a character stack of size 10 using the source code in chapter 4 lectures. Push the characters FALL BREAKS into the stack then pop them out and display “FALL BREAK” on the screen. Find the problem in your program and fix it so that any illegal push or pop is prevented.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.