1 Lab Session-6 CSIT221 Fall 2002 A Note About Destructors Using Pointers Implementing Stacks.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Dynamic Allocation Eric Roberts CS 106B February 4, 2013.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
Writing a Good Program 6. Pointers and Arrays
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
1 Lab Session-9 CSIT221 Fall 2002 Lab Exercise Based on operator overloading (Demo Required)
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
1 Lab Session-5 CSIT221 Spring 2003 Default and Parameterized Constructors Destructors Programming Exercise for building a template based class (demo required)
1 Lab Session-7 CSIT-121 Fall Revising Structured Choice 4 The While Loop variations 4 Lab Exercises.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Lab Session-12 CSIT121 Fall 2004 Structures and Their Usage Passing Struct Variables to Functions.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
C++ Review (3) Structs, Classes, Data Abstraction.
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC241: Object Oriented Programming Lecture No 17.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
IndexedListWithIteratorsViaLinear1Ind
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers and Linked Lists
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Overview of Memory Layout in C++
Introduction to Programming
Indirection.
Presentation transcript:

1 Lab Session-6 CSIT221 Fall 2002 A Note About Destructors Using Pointers Implementing Stacks

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 Example: Class Definition #include using namespace std; class menu { public: menu(); bool reserved(); void viewseats(); ~menu(); private: char bus [9][4]; };

4 Example: Member Functions bool menu::reserved() { cout<<"Reserving....."<<endl; return true; } void menu::viewseats() { cout<<"View seats....."<<endl; }

5 Example: Constructor and Destructor menu::menu() { cout<<"Constructor runs now"<<endl; } menu::~menu() { cout<<"Destructor runs now"<<endl; }

6 Example: Client Code void main() { menu mymenu; if (mymenu.reserved()) cout<<“Runs OK”<<endl; mymenu.viewseats(); }

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 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 to submit this lab due to the start of Fall Break on Oct 10th). 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.