CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;

Size: px
Start display at page:

Download "CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;"

Transcription

1 Inheritance and Virtual Functions. Linked lists. CS427 Lecture 12.2, 11am, 26th March 2012 In today s class 1 Recall... Inheritance Limitations of arrays 5 Linked lists Further linked list operations 9 The End... Recall... Inheritance Inheritance Given a class, construct from it a new class that has properties of the original class, plus some new ones. The original class is called the base class. The new one, is called the derived class. The derived class will inherit All the data members of the base class, though they can only be accessed directly if they are specified as protected by the base class (instead of private). The ordinary function members, but not constructors, destructors, or the assignment operator. The syntax for defining a derived class class Derived : public Base CS427 Inheritance and Virtual Functions. Linked lists. 1/27 CS427 Inheritance and Virtual Functions. Linked lists. 2/27 CS427 Inheritance and Virtual Functions. Linked lists. 3/27 Suppose, for example, that a base class has a method void PrintData(void). It is possible to make a derived class that also contains a (new) method call void PrintData(void). The version of PrintData(void) from the derived class will superceed the one from the base class. However, the original one does not disappear, it can still be called, but you need to use the base class name and scope resolution operator. Note: If the base and derived classes have functions with the same names, but different signatures, then the function is overloaded, not replaced. class Point protected : i n t x ; Point ( i n t X=0) x=x ; ; i n t GetX ( void ) return ( x ) ; ; void SetX ( i n t X) x=x ; ; void PrintData ( void ) ; ; void Point : : PrintData ( void ) cout << ( << x << ) \ n ; 01Replacement.cpp link! class TwoPoint : public Point private : i n t y ; TwoPoint ( i n t X=0, i n t Y=0) x=x ; y=y ; ; i n t GetY ( void ) return ( y ) ; ; void SetY ( i n t Y) y=y ; ; void PrintData ( void ) ; ; void TwoPoint : : PrintData ( void ) cout << ( << x <<, << y << ) \ n ; CS427 Inheritance and Virtual Functions. Linked lists. 4/27 CS427 Inheritance and Virtual Functions. Linked lists. 5/27 CS427 Inheritance and Virtual Functions. Linked lists. 6/27

2 int main ( void ) Point b ; TwoPoint d ; b. SetX ( 1 2 ) ; cout << b= ; b. PrintData ( ) ; d. SetX ( 1 ) ; d. SetY ( 2 ) ; cout << \nd= ; d. PrintData ( ) ; cout << Calling the Point version of Print Data : ; cout << d= ; d. Point : : PrintData ( ) ; return ( 0 ) ; There are other times when we need to take care which of two functions that have the same signature and belonging the base and derived class is called. In particular, consider the following code segment: Point d, b ; b = new Point ( 3 ) ; d = new TwoPoint ( 5, 6 ) ; Here b and d are both pointers to the type Point, but created using dynamically memory allocation. In particular, d in initialised as an instance of the TwoPoint class. But if we call (*d).printdata() (or, equivalently, d->printdata() it is Point::PrintData() that is called. If we want TwoPoint::PrintData() to be called, then in the definition of Point, we put before the definition of PrintData(). This is shown in 02Virtual.cpp link! As a more natural example of when a function might be required, consider the following implementation for items stored in a library. Items in the library all have, at the least, a title and a call-number. The base class item includes private members string title and int CallNumber. There are methods that print the title and call number, and a method printrecord that does both. class item protected : s t r i n g t i t l e ; i n t CallNumber ; item ( string t=, i n t n=0) t i t l e = t ; CallNumber=n ; ; void s e t T i t l e ( s t r i n g t ) t i t l e = t ; ; void setcallnumber ( i n t n ) CallNumber=n ; ; void p r i n t T i t l e ( void ) cout << t i t l e ; ; void printcallnumber ( void ) cout << CallNumber ; ; void printrecord ( void ) ; ; CS427 Inheritance and Virtual Functions. Linked lists. 7/27 CS427 Inheritance and Virtual Functions. Linked lists. 8/27 CS427 Inheritance and Virtual Functions. Linked lists. 9/27 The code for the printrecord method is: void item : : printrecord ( void ) cout << t i t l e : ; p r i n t T i t l e ( ) ; cout << ( c a l l number : ; printcallnumber ( ) ; cout << ) << endl ; Next we have a derived class specifically for books. It has a different form of call number in this case, based on the Dewey Decimal system. Each book s call number is made up of two integers. The first represents the broad category (e.g., 005=Computer programming) and the second the specific subject (e.g., 100=Software Engineering). class Book : public item private : i n t Category ; i n t Subject ; Book ( s t r i n g t =, i n t c =0, i n t s =0) t i t l e = t ; Category=c ; Subject=s ; ; void setcallnumber ( i n t c, i n t s ) Category=c ; Subject=s ; ; void printcallnumber ( void ) ; ; void Book : : printcallnumber ( void ) cout << s e t f i l l ( 0 ) << setw ( 3 ) << Category <<. << Subject ; So if we declare an object of type book, and call its method printrecord. But printrecord was inherited from the base class. That is, book::printrecord is the same as item::printrecord. So it will call item::printcallnumber. That is not what we intended. If we change the definition of item::printcallnumber in the base class to specify that it is, then the binding that is done at compile time is changed, and even though printrecord is inherited from item, we ll find that book::printrecord will call book::printcallnumber. The full example is given in 03Library.cpp link! CS427 Inheritance and Virtual Functions. Linked lists. 10/27 CS427 Inheritance and Virtual Functions. Linked lists. 11/27 CS427 Inheritance and Virtual Functions. Linked lists. 12/27

3 Limitations of arrays For many applications, such as your GradeBook project, it may seem natural to store the data is a dynamically sized arrays of objects. However, arrays are very rigid. For example, it is not clear how to add a new item to the middle of the list. delete an item allocate extra memory on the fly. To deal with these issues, we could use a linked list: an object that includes as one of its members a link to another object. These ideas featured in you Cs209 class. Now we ll review how they work in C There are many operations one would like to perform on a linked list: Output the items in the list Add a new item to the end of the list Add a new item to the middle of the list Delete item, etc. Linked lists A linked list is a collection of nodes, each of which has Some data element, sometimes called Cargo, A link (i.e., a pointer) to the next node in the list. If a node is implemented as a class in C++, it might also need methods to get and set the next node on the list. Here is part of a very basic example which you can find in 04OneTwoThree.cpp link! 04OneTwoThree.cpp 11 class Node friend i n t Length (Node head ) ; 13 private : i n t data ; 15 Node next ; 17 Node ( void ) next=null ; ; void setnext (Node n ) next=n ; ; 19 Node getnext ( void ) return ( next ) ; ; void setdata ( i n t d ) data=d ; ; 21 i n t getdata ( void ) return ( data ) ; ; ; Linked lists Now use this class: i n t main ( void ) 26 Node f i r s t, second, t h i r d ; 28 f i r s t. setdata ( 1 ) ; 30 f i r s t. setnext (&second ) ; second. setdata ( 2 ) ; 32 second. setnext (& third ) ; t h i r d. setdata ( 3 ) ; 34 cout << Number of items i n l i s t : 36 << Length (& f i r s t ) << endl ; system ( pause ) ; CS427 Inheritance and Virtual Functions. Linked lists. 13/27 CS427 Inheritance and Virtual Functions. Linked lists. 14/27 CS427 Inheritance and Virtual Functions. Linked lists. 15/27 The Length() function takes a linked list and computes the number of elements in the list. Length() is a simple list function, but it demonstrates several concepts which will be used in later, more complex list functions i n t Length ( Node head ) 45 Node c u r r e n t =head ; i n t count =0; 47 while ( c u r r e n t!= NULL) 49 count ++; 51 current = current >next ; 53 return ( count ) ; There are two common features of linked lists demonstrated in Length() 1 Pass The List By Passing The Head Pointer The linked list is passed in to Length() via a single head pointer. The pointer is copied from the caller into the head variable local to Length(). 2 Iterate Over The List With A Local Pointer The code to iterate over all the elements is very typical: while (current!= NULL) //... current = current->next; The key ideas of this example are 1 The local pointer, current in this case, starts by pointing to the same node as the head pointer with current = head; So when the function exits, the value of head there has not changed. 2 The while loop tests for the end of the list with (current!= NULL) This catches the empty list case 3 At the end of the while loop we have the line current = current->next; This advances the local pointer to the next node in the list. When there are no more links, this sets the pointer to NULL. CS427 Inheritance and Virtual Functions. Linked lists. 16/27 CS427 Inheritance and Virtual Functions. Linked lists. 17/27 CS427 Inheritance and Virtual Functions. Linked lists. 18/27

4 Now we ll develop some functions to add an items to a list. The full code is given in 05addToList.cpp link! The new functions are printlist addtostart and First, the following lines gets added to the class definition of Node 05addtoList.cpp friend i n t Length (Node head ) ; 13 friend void printlist (Node head ) ; friend void addtostart (Node &head, i n t Data ) ; 15 friend void (Node &head, i n t Data ) ; Note that pointer to the head of the list is passed by reference to the addtostart and functions. This is because they have to change the value of head in the calling function. The printlist function is the simplest: 66 void p r i n t L i s t ( Node head ) 68 Node c u r r e n t =head ; i n t count =0; 70 while ( c u r r e n t!= NULL) 72 cout << Link << count << contains 74 << current >data << endl ; current = current >next ; 76 count ++; 78 Next we ll write the addtostart function: it creates a new link at the head of the list (like a stack). It performs four operations: 1 Allocate memory for the new link, using new 2 Store the new data in the new link 3 Set the.next pointer of the new node to point to the current head of the list. 4 Change the old head pointer so that it points to the new head of the list. This last part is importance: because the function must change to value of a variable belonging to the calling function, we have to pass the address by reference. CS427 Inheritance and Virtual Functions. Linked lists. 19/27 CS427 Inheritance and Virtual Functions. Linked lists. 20/27 CS427 Inheritance and Virtual Functions. Linked lists. 21/27 The addtostart functions is defined as / 81 add a link at the start of the l i s t. Since the head of the l i s t is being changed 83 i t must be passed by reference / 85 void addtostart ( Node &headref, i n t NewData ) 87 Node newnode = new Node ; 89 newnode >setdata (NewData ) ; newnode >setnext ( headref ) ; 91 headref=newnode ; 93 Note: we could write a much shorter version of this function if we had a more sophisticated constructor. Adding a new link to the top of the list is the easiest way of adding a link. However, more often we often want to add something to the end (tail) of the list. Generally this should not change the value of the head of the list. But, because we have to allow for the case of the list being empty, we have to pass by reference. 96 / / Adding to the end of the list. If the list / / is empty, then the head gets chagned. So 98 / / we much pass by reference void ( Node &headref, 100 i n t NewData ) 102 Node newnode = new Node ; newnode >setdata (NewData ) ; 104 newnode >setnext (NULL ) ; 106 i f ( headref==null) / / if list is empty headref=newnode ; 108 else / / Find the end of the list 110 Node c u r r e n t =headref ; while ( current >next!= NULL) 112 current = current >next ; 114 current >next=newnode ; 116 Adding a new link to the top of the list is the easiest way of adding a link. However, more often we often want to add something to the end (tail) of the list. Usually this should not change the value of the head of the list. But, because we have to allow for the case of the list being empty, we have to pass by reference. CS427 Inheritance and Virtual Functions. Linked lists. 22/27 CS427 Inheritance and Virtual Functions. Linked lists. 23/27 CS427 Inheritance and Virtual Functions. Linked lists. 24/27

5 Further linked list operations There are many other operations that one could do on linked lists, other implementation issues, and variants. These include Using template to allow generic data types for the cargo. See 06LListTempate.cpp link! for an example. Functions to copy lists. insert a link in the middle of the list, delete a link from a list delete all items in a list, and deallocate the associated memory implement doubly-linked lists; here each link has a reference to the previous link as well as the next. The End... The final paper has 6 questions. You should attempt 4. 3 Questions on Software Engineering. Overview, and so could over any part of the course. It will, however, include the Waterfall model of the software lifecycle. Requirements; Specification; Logic and Predicate Calculus. Design (including Pseudocode, Finite State Machines, Data Flow Diagrams, ADTs, OOD), the coding and testing phases. 3 questions on C++ programming. Any topic covered in class or labs may be asked including: basic I/O and file I/O; functions: including overloading, default parameter lists, pass-by-reference, classes, including constructors and destructors; private, public, protected; operator overloading and friend functions. static variables. templates inheritance, including functions (but not) Linked Lists Full marks for FOUR questions. There is no sample paper, but many of the Revision Exercises are of exam standard. The End... The final exam will be worth 70%. The remaining 30% will come from In-class C test (%5) lab programming assignments. Software engineering assignments the Project. For details, see web-site. The end CS427 Inheritance and Virtual Functions. Linked lists. 25/27 CS427 Inheritance and Virtual Functions. Linked lists. 26/27 CS427 Inheritance and Virtual Functions. Linked lists. 27/27

Lecture 10: Introduction to Inheritance

Lecture 10: Introduction to Inheritance Lecture 10: Introduction to Inheritance CS427: Programming in C++ Lecture 10.2 11am, 12th March 2012 CS427 Lecture 10: Introduction to Inheritance 1/17 In today s class 1 Inheritance protected 2 Replacing

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

More information

Week 8: Operator overloading

Week 8: Operator overloading Due to various disruptions, we did not get through all the material in the slides below. CS319: Scientific Computing (with C++) Week 8: Operator overloading 1 The copy constructor 2 Operator Overloading

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure that you review all previous exams and make sure you fully understand

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Fall 2018 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Assignment of Objects

Assignment of Objects Copying Objects 1 Assignment of Objects 2 Slides 1. Table of Contents 2. Assignment of Objects 3. Dynamic Content 4. Shallow Copying 5. Deep Copying 6. this Pointer 7. Improved Deep Copy 8. Passing an

More information

CMSC 341 Lecture 7 Lists

CMSC 341 Lecture 7 Lists CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Spring 2013 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int, Classes Starting Savitch Chapter 10 l l A class is a data type whose variables are objects Some pre-defined classes in C++ include int, char, ifstream Of course, you can define your own classes too A class

More information

Data Structures (CS301) LAB

Data Structures (CS301) LAB Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes Scott Gibson Pointers & Dynamic Memory Lecture #1 Office: LAH 103 Email: sgibson@brookdalecc.edu sgib@optonline.net Web: www.brookdalecc.edu/fac/cos/sgibson Phone: (732) 224 2285 1 2 Pre & Co Requisites

More information

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12 CS 151 Linked Lists, Recursively Implemented 1 2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can

More information

LECTURE 03 LINKED LIST

LECTURE 03 LINKED LIST DATA STRUCTURES AND ALGORITHMS LECTURE 03 LINKED LIST IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD LINKED LISTS DEFINITION A linked list is a data structure where each object is stored in

More information

Written by John Bell for CS 342, Spring 2018

Written by John Bell for CS 342, Spring 2018 Advanced OO Concepts Written by John Bell for CS 342, Spring 2018 Based on chapter 3 of The Object-Oriented Thought Process by Matt Weisfeld, with additional material from other sources. Constructors Constructors

More information

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 Data Structures Course Review FINAL Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Final When: Wednesday (12/12) 1:00 pm -3:00 pm Where: In Class

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

More information

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24

More information

Motivation for Templates

Motivation for Templates Motivation for You want both: a list of Location objects a list of MazeMonster objects 1 How can you accomplish this by writing one LinkedList class? state all the ways you can think of doing this state

More information

Class and Function Templates

Class and Function Templates Class and Function 1 Motivation for One Way to Look at... Example: Queue of some type Foo C++ What can a parameter be used for? Instantiating a Template Usage of Compiler view of templates... Implementing

More information

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

Motivation for Templates. Class and Function Templates. One Way to Look at Templates... Class and Function 1 Motivation for 2 Motivation for One Way to Look at... Example: Queue of some type Foo C++ What can a parameter be used for? Instantiating a Template Usage of Compiler view of templates...

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence

More information

Lecture 15a Persistent Memory & Shared Pointers

Lecture 15a Persistent Memory & Shared Pointers Lecture 15a Persistent Memory & Shared Pointers Dec. 5 th, 2017 Jack Applin, Guest Lecturer 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 1 Announcements PA9 is due today Recitation : extra help

More information

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Question No: 1 ( Marks: 2 ) Write a declaration statement for an array of 10

More information

Spring 2008 Data Structures (CS301) LAB

Spring 2008 Data Structures (CS301) LAB Spring 2008 Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Singly Linked List practically using c++ and adding more functionality in it. o Enabling

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++ No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first Ch. 17: Linked Lists 17.1 Introduction to Linked Lists! A data structure representing a list! A series of nodes chained together in sequence CS 2308 Spring 2013 Jill Seaman - Each node points to one other

More information

CS 216 Exam 1 Fall SOLUTION

CS 216 Exam 1 Fall SOLUTION CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT

More information

Linked Lists and other Dynamic Data Structures

Linked Lists and other Dynamic Data Structures Linked Lists and other Dynamic Data Structures Arrays Fixed in size Allocated in advance within a contiguous memory block Look-up is fast Resizing and Deleting is hard (reallocate and copy) Dynamic Data

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

Implementing an ADT with a Class

Implementing an ADT with a Class Implementing an ADT with a Class the header file contains the class definition the source code file normally contains the class s method definitions when using Visual C++ 2012, the source code and the

More information

Lecture No.04. Data Structures

Lecture No.04. Data Structures Lecture No.04 Data Structures Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i

More information

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff Recap: Pointers IFMP 18, M. Schwerhoff int* int& *p &i &*&* ** * * * * A 0 A 1 A 2 A 3 A 4 A 5 A 0 A 1 A 2 A 3 A 4 A 5 5 i int* p; A 0 A 1 A 2 A 3 A 4 A 5 5 i p int* p; p = &i; A 0 A 1 A 2 A 3 A 4 A 5

More information

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19 Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2008 7p-9p, Tuesday, February 19 Name: NetID: Lab Section (Day/Time): This is a closed book and closed

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Sample Exam 2 75 minutes permitted Print your name, netid, and

More information

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms Introduction to Linked Lists Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms Lecture Slides Friday, September 25, 2009 Glenn G. Chappell Department of Computer Science

More information

The University of Nottingham

The University of Nottingham The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER 2011-2012 G52CPP C++ Programming Examination Time allowed TWO hours Candidates may complete the front cover of

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). UNITII Classes Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). It s a User Defined Data-type. The Data declared in a Class are called Data- Members

More information

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula Object-Oriented Programming Lecture 2 Dr Piotr Cybula Encapsulation : data protection code safety and independence better team support with the code separation without «giving

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

CSC 210, Exam Two Section February 1999

CSC 210, Exam Two Section February 1999 Problem Possible Score 1 12 2 16 3 18 4 14 5 20 6 20 Total 100 CSC 210, Exam Two Section 004 7 February 1999 Name Unity/Eos ID (a) The exam contains 5 pages and 6 problems. Make sure your exam is complete.

More information

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked Chapter 6 LISTS AND STRINGS 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked 3. Strings 4. Application:

More information

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example CS 311 Data Structures and Algorithms Lecture Slides Friday, September 11, 2009 continued Glenn G. Chappell

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Spare Matrix Formats, and The Standard Template Library

Spare Matrix Formats, and The Standard Template Library Annotated slides CS319: Scientific Computing (with C++) Spare Matrix Formats, and The Standard Template Library Week 10: 9am and 4pm, 20 March 2019 1 Sparse Matrices 2 3 Compressed Column Storage 4 (Not)

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

More information

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation Announcements Lab 3 was a Frankenstein assembly of a new group exercise with part of a gdb lab. I hear many of you

More information

Module 1. C++ Classes Exercises

Module 1. C++ Classes Exercises Module 1. C++ Classes Exercises 1. The ZooAnimal class definition below is missing a prototype for the Create function. It should have parameters so that a character string and three integer values (in

More information

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington CSE 333 Lecture 11 - constructor insanity Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington Administrivia Exercises: - New exercise out today, due Monday morning

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract

More information

Lecture 3. Lecture

Lecture 3. Lecture True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Objects and streams and files CS427: Elements of Software Engineering

Objects and streams and files CS427: Elements of Software Engineering Objects and streams and files CS427: Elements of Software Engineering Lecture 6.2 (C++) 10am, 13 Feb 2012 CS427 Objects and streams and files 1/18 Today s topics 1 Recall...... Dynamic Memory Allocation...

More information

G52CPP C++ Programming Lecture 20

G52CPP C++ Programming Lecture 20 G52CPP C++ Programming Lecture 20 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Wrapping up Slicing Problem Smart pointers More C++ things Exams 2 The slicing problem 3 Objects are not

More information

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++ Introduction to Programming in C++ Course Text Programming in C++, Zyante, Fall 2013 edition. Course book provided along with the course. Course Description This course introduces programming in C++ and

More information

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

double d0, d1, d2, d3; double * dp = new double[4]; double da[4]; All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

More information

Object-Oriented Programming in C++

Object-Oriented Programming in C++ Object-Oriented Programming in C++ Pre-Lecture 9: Advanced topics Prof Niels Walet (Niels.Walet@manchester.ac.uk) Room 7.07, Schuster Building March 24, 2015 Prelecture 9 Outline This prelecture largely

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures CSE 465 LECTURE 9 Abstract Data Types Queues Sofya Raskhodnikova and Adam Smith 1 Data Structures So far in this class: designing algorithms Inputs and outputs were specified,

More information

CS 11 C++ track: lecture 1

CS 11 C++ track: lecture 1 CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account http://www.cs.caltech.edu/cgi-bin/ sysadmin/account_request.cgi Need to know UNIX (Linux) www.its.caltech.edu/its/facilities/labsclusters/

More information

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers) Review Final exam Final exam will be 12 problems, drop any 2 Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers) 2 hours exam time, so 12 min per problem (midterm 2 had

More information

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

COM S 213 PRELIM EXAMINATION #2 April 26, 2001 COM S 213 PRELIM EXAMINATION #2 April 26, 2001 Name: Student ID: Please answer all questions in the space(s) provided. Each question is worth 4 points. You may leave when you are finished with the exam.

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

CS2210 Data Structures and Algorithms

CS2210 Data Structures and Algorithms CS2210 Data Structures and Algorithms Lecture 3: ADT and Java interfaces Instructor: Olga Veksler 2004 Goodrich, Tamassia Outline Review Data Structures Abstract Data Types 2 Principles of OO Programming

More information

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation Review from Lectures 7 Algorithm Analysis, Formal Definition of Order Notation Simple recursion, Visualization

More information

CS24 Week 4 Lecture 2

CS24 Week 4 Lecture 2 CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track

More information

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017 COMP 250 Lecture 6 doubly linked lists Sept. 20/21, 2017 1 Singly linked list head tail 2 Doubly linked list next prev element Each node has a reference to the next node and to the previous node. head

More information

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

More information

lecture09: Linked Lists

lecture09: Linked Lists lecture09: Largely based on slides by Cinda Heeren CS 225 UIUC 24th June, 2013 Announcements mp2 due tonight mt1 tomorrow night! mt1 review instead of lab tomorrow morning mp3 released tonight, mp3.1 extra

More information

Casting in C++ (intermediate level)

Casting in C++ (intermediate level) 1 of 5 10/5/2009 1:14 PM Casting in C++ (intermediate level) Casting isn't usually necessary in student-level C++ code, but understanding why it's needed and the restrictions involved can help widen one's

More information

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++ Session 6 - Inheritance in C++ The most important slide of the lecture Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Why use inheritance?

More information

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING Question No: 1 ( Marks: 1 ) - Please choose one Classes like TwoDimensionalShape and ThreeDimensionalShape would normally be concrete,

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Sample Exam 2 75 minutes permitted Print your name, netid, and

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

AIMS Embedded Systems Programming MT 2017

AIMS Embedded Systems Programming MT 2017 AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation Lecture 15 COP 3014 Fall 2017 November 6, 2017 Allocating memory There are two ways that memory gets allocated for data storage: 1. Compile Time (or static) Allocation Memory

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo Chapter 10 Pointers and Dynamic Arrays 1 Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this

More information

Ch. 12: Operator Overloading

Ch. 12: Operator Overloading Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42

More information

CGS 2405 Advanced Programming with C++ Course Justification

CGS 2405 Advanced Programming with C++ Course Justification Course Justification This course is the second C++ computer programming course in the Computer Science Associate in Arts degree program. This course is required for an Associate in Arts Computer Science

More information

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let

More information