Static Class Member Function We can declare a member function being static. A static member function doesn’t have to be invoked by an object. A static.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Relation Between Derived-Class and Base-Class Derived class object can use base class methods, if they are not private. Base class pointer can point to.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Templates Zhen Jiang West Chester University
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
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,
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Stack and Queue APURBO DATTA.
Chapter 19: Stacks and Queues.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers & Dynamic Data Structures
Stacks CS-240 Dick Steflik.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Getting queues right … finally (?)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

Static Class Member Function We can declare a member function being static. A static member function doesn’t have to be invoked by an object. A static member function can use only static data member.

Returning Objects Return a reference to an object A constant reference to an object An object A constant object

A queue holds an ordered sequences of items. Has a limit Add item to the end of the queue Remove item from front of the queue. Create an empty queue Check whether queue is empty Check whether queue is full Example(Queue simulation)

#ifndef Queue_H #define Queue_H class Queue { private : enum {Q_SIZE=10;} struct Node {int item; struct Node *next; }; Node *front; Node *end; int items; const int qsize;

public : Queue( int qs =Q_SIZE ); // creates queue with a qs limit ~Queue(); bool isempty(); bool isfull(); int queuecount(); bool enqueue( const int &item); bool dequeue(int &item); int get_size(); };

Queue :: Queue(int qs) { front=end =NULL; item=0; qsize=qs; // not acceptable; since qsize is // constant } What to do ?

Using syntax member initialize list Queue :: Queue(int qs) : qsize(qs) // initialize //qsize to qs { front=end =NULL; item=0; } Or Queue :: Queue(int qs) : qsize(qs), front(NULL), end(NULL), items(0) { }

This form can be used with constructors We have to use this form class member that are declared as reference. e.g. class Agency {…}; class Agent { private : Agency & belong; }; Agent::Agent (Agency & a) : belong(a) {…}

bool Queue :: enqueue(const int &item) { if( isfull()) return false; Node * add=new Node; If( add==NULL) return false; add->item=item; add->next=NULL; items++; if( front == NULL) front=add; else end ->next=add; end=add; return true; }

bool Queue :: dequeue(int &item) { if( front ==end) return false; item=front->item; items--; Node *temp=front; front=front->next; delete temp; if (items==0) end=NULL; return true; }

bool Queue :: isempty() { if (item==0) return true; else return false; } bool Queue :: isfull() { if (item==qsize) return true; else return false; }

Queue :: ~ Queue () { Node *temp; while( front !=NULL) { temp=front; front=front->next; delete temp; }

int Queue :: get_size() { return qsize; }

#include #include “queue.h” int main() { Queue qu; Queue qu1(12); cout<<qu.get_size()<<endl; cout<<qu1.get_size()<<endl; return 0; }

Class Inheritance The goal of OOP is providing reusable code. Traditional C provides reusability through predefined, precompiled function e.g strln(), rand();

Often, class libraries are available in source code, which means we can modify them. C++ has better method than code modification. It’s called inheritance. Deriving new class from old one( base class) We can do the following with inheritance:

Add functionality to an existing class. e.g. Given a basic array class we can add arithmetic operations. Add to the data that a class represent. derive a class from class string having a color element to show the color of the string Modify class method behavior.

The ability to define the behavior or implementation of one class as a superset of another class

#ifndef TABTENN0_H_ #define TABTENN0_H_ class TableTennisPlayer { private : enum{LIM=20}; char firstname[LIM]; char lastname[LIM]; bool hasTable; public : TableTennisPlayer ( const char *fn=“none”, const char *In =“none”, bool ht=false); void name (); bool HasTable() { return hasTable;}; void ResetTable(bool v) { hasTable=v;}; }; #endif

#include “tabtenn0.h” #include TableTennisPlayer:: TableTennisPlayer( const char *fn, const char *ln, bool ht) { strncpy(firstname, fn, LIM-1); firstname[LIM-1]=‘\0’; strncpy(lastname, ln, LIM-1); lastname[LIM-1]=‘\0’; hasTable=ht; } void TableTennisPlayer::name() { cout<<lastname<<“, “<<firstname; }

The following class is a derived class and manage the points that player earned. class RatedPlayer : public TableTennisPlayer { … }; The colon indicates that RatedPlayer class is based on the TableTennisPlayer.

If we declare a RatedPlayer object it has the following special properties. An object of the derived type stores the data members of the base type in it. An object of the derived type can use the method of the base type. A derived class needs its own constructers A derived class can add additional data members.

class RatedPlayer : public TableTennisPlayer { private : unsigned int rating; public : RatedPlayer ( unsigned int r =0, const char *fn=“none”, const char *In =“none”, bool ht=false); RatedPlayer ( unsigned int r, const TableTennisPlayer &tp); unsigned int Rating() { return rating;} void ResetRating ( unsigned int r) { rating = r;} };

A derived class does not have direct access to the private member of the base class. They have to use public base-class methods to access private base-class member. When a program construct a derived-class object, it first constructs the base-class object.

We can use C++ member initializer list syntax: RatedPlayer:: RatedPlayer( unsigned int r, const char *fn, const char *In, bool ht) : TableTennisPlayer(fn, ln, ht) { rating=r; } Or we can write : RatedPlayer:: RatedPlayer( unsigned int r, const TableTennisPlayer &tp) : TableTennisPlayer(tp) { rating=r; } Or RatedPlayer:: RatedPlayer( unsigned int r, const TableTennisPlayer &tp) : TableTennisPlayer(tp), rating(r); { }

We can call RatedPlayer rplayer1(1140,”Mary”, “Smith”,true);

#ifndef TABTENN0_H_ #define TABTENN0_H_ class TableTennisPlayer { private : enum{LIM=20}; char firstname[LIM]; char lastname[LIM]; bool hasTable; public : TableTennisPlayer ( const char *fn=“none”, const char *In =“none”, bool ht=false); void name (); bool HasTable() { return hasTable;}; void ResetTable(bool v) { hasTable=v;}; };

class RatedPlayer : public TableTennisPlayer { private : unsigned int rating; public : RatedPlayer ( unsigned int r =0, const char *fn=“none”, const char *In =“none”, bool ht=false); RatedPlayer ( unsigned int r, const TableTennisPlayer &tp); unsigned int Rating() { return rating;} void ResetRating ( unsigned int r) { rating = r;} }; #endif

#include “tabtenn0.h” #include TableTennisPlayer:: TableTennisPlayer( const char *fn, const char *ln, bool ht) { strncpy(firstname, fn, LIM-1); firstname[LIM-1]=‘\0’; strncpy(lastname, ln, LIM-1); lastname[LIM-1]=‘\0’; hasTable=ht; } TableTennisPlayer::name() { cout<<lastname<<“, “<<firstname; }

RatedPlayer:: RatedPlayer( unsigned int r, const char *fn, const char *In, bool ht) : TableTennisPlayer(fn, ln, ht) { rating=r; } RatedPlayer:: RatedPlayer( unsigned int r, const TableTennisPlayer &tp) : TableTennisPlayer(tp) { rating=r; }

#include #include “tabtenn1.h” int main() { TableTennisPlayer player1(“tara”,”Smith”,false); RatedPlayer rplayer1(1140,”Dave”,”Cohen”,true); rplayer1.name(); if(rplayer1.HasTable()) cout<<“has a table \n”; else cout<<“hasn’t a table\n”; player1.name();

if(player1.HasTable()) cout<<“has a table \n”; else cout<<“hasn’t a table\n”; rplayer1.name(); cout<<“rating “<< rplayer1.Rating()<<endl; RatedPlayer rplayer2(1212,player1); rplayer2.name(); cout<<“rating “<< rplayer2.Rating()<<endl; return 0; }

Relation Between Derived-Class and Base-Class Derived class object can use base class methods, if they are not private. Base class pointer can point to a derived class object A base class reference can refer to a derived class object A derived class reference or pointer can NOT refer to base class object.

RatedPlayer rplay(10,”Sue”,”Smith”,true); TableTennisPlayer &rt= rplay; TableTennisPlayer *pt= &rplay; rt.name(); // call by references pt->name(); //call by pointer TableTennisPlayer play(“dan”, “cohen”,true); RatedPlayer &rr=play; // not allowed RatedPlayer *pr=&play; // not allowed

TableTennisPlayer *ptr; ptr = &rplayer2; ptr->name(); cout<<endl; cout Rating()<<endl; //not ok

Derived class inherits the base class methods and data member, so derived class method can use base class reference to call a method. A base class can’t use derived class method therefore base class method can not use derived class reference to call a method.

Three types of inheritance : Public Private Protected Public Inheritance is an Is-a relation. An object of derived class is an object of base class. Consider Fruit and Banana, Banana is a Fruit.

public inheritance doesn’t model a has-a relationship. Consider Lunch and Fruit. In general fruit is not lunch, but lunch can have fruit. public inheritance doesn’t model a is-like-a relationship. Lawyers are like sharks, (some times) Don’t try to derived a Lawyer class from Shark. Otherwise lawyers must live underwater!

public inheritance doesn’t model an is- implemented-as-a relationship. Stack and Array. Not proper to derive a Stack from Array class public inheritance doesn’t model a uses-a relationship. Computer can use printer, but we don’t derived printer class from computer class.