C++ For Science and Engineering Lecture 27

Similar documents
C++ For Science and Engineering Lecture 12

C++ For Science and Engineering Lecture 15

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

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

Programsystemkonstruktion med C++: Övning 2. Karl Palmskog september 2010

CPSC 427: Object-Oriented Programming

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSCE 110 PROGRAMMING FUNDAMENTALS

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 18 Tao Wang 1

ADTs: Stacks and Queues

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

SFU CMPT Topic: Class Templates

CPSC 427: Object-Oriented Programming

COEN244: Class & function templates

CS3157: Advanced Programming. Outline

CMSC 4023 Chapter 11

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Chapter 11. Abstract Data Types and Encapsulation Concepts

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

EECE.3220: Data Structures Spring 2017

September 10,

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

การทดลองท 8_2 Editor Buffer Array Implementation

COMP 2355 Introduction to Systems Programming

Introduction to Programming

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)

Data Structures and Algorithms

Chapter 10 Introduction to Classes

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

AN OVERVIEW OF C++ 1

6.096 Introduction to C++ January (IAP) 2009

Data Structures And Algorithms

Midterm Review. PIC 10B Spring 2018

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

Short Notes of CS201

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

Today s lecture. CS 314 fall 01 C++ 1, page 1

CS201 - Introduction to Programming Glossary By

Data Structures and Algorithms

Lab 2: ADT Design & Implementation

Abstract Data Types and Encapsulation Concepts

Tokens, Expressions and Control Structures

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Fast Introduction to Object Oriented Programming and C++

Lecture 23: Pointer Arithmetic

Pointers, Dynamic Data, and Reference Types

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CSE 303 Lecture 23. Inheritance in C++ slides created by Marty Stepp

Introduction to Programming using C++

CS349/SE382 A1 C Programming Tutorial

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Programming in C and C++

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

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 C++ Systems Programming

OOP- 5 Stacks Individual Assignment 35 Points

Abstract Data Types. Different Views of Data:

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

CE221 Programming in C++ Part 1 Introduction

Fundamentals of Programming. Lecture 19 Hamed Rasifard

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Input And Output of C++

Creating a C++ Program

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSCE 206: Structured Programming in C++

Programming, numerics and optimization

CPSC 427: Object-Oriented Programming

C++_ MARKS 40 MIN

CSI33 Data Structures

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

Advanced Systems Programming

Programming in C/C Lecture 3

CSc Introduction to Computing

Common Misunderstandings from Exam 1 Material

CSCE 110 PROGRAMMING FUNDAMENTALS

III. Classes (Chap. 3)

C++ Final Exam 2017/2018

AIMS Embedded Systems Programming MT 2017

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Variables. Data Types.

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Introduction to Classes

Due Date: See Blackboard

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Array Elements as Function Parameters

COMP322 - Introduction to C++ Lecture 07 - Introduction to C++ classes

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Transcription:

C++ For Science and Engineering Lecture 27 John Chrispell Tulane University Monday November 1, 2010

Classes and the This pointer Every C++ object has a curious pointer called this. If we want to extend our stock class we could write a member function that returns the greater valued of two investments. Thus, the member function would take an instance of the stock class as an argument. Compare the two stocks and return the greater of the two investments. The prototype for such a function is: const Stock & Stock : : t o p v a l ( const Stock & s ) const ; And we will call the function as: top = s t o c k 1. t o p v a l ( s t o c k 2 ) ; top = s t o c k 2. t o p v a l ( s t o c k 1 ) ; Where these two functions return the pointer to the higher valued investment. Each call explicity one object, and implicity calling the other. John Chrispell, Monday November 1, 2010 slide 3/25

Classes and the This pointer Each member function, including constructors and destructors, has a this pointer. The special propertiy of the this is that it points to the invoking object. const Stock & Stock : : t o p v a l ( const Stock & s ) const { i f ( s. t o t a l v a l > t o t a l v a l ){ return s ; // argument o b j e c t e l s e { return t h i s ; // i n v o k i n g o b j e c t Here the return type is a reference. Thus, the returned object is the invoking object itself rather than a copy passed by the return mechanism. John Chrispell, Monday November 1, 2010 slide 5/25

Arrays and classes. We can create an array of class objects using syntax we already know: Stock m y s t u f f [ 4 ] ; // c r e a t e s an a r r a y o f 4 Stock o b j e c t s. Note this uses the default class constructor when it creates class objects. We can also use a constructor that initializes the array elements: const i n t STKS 4 ; Stock s t o c k s [ STKS ] = { Stock ( Kermit, 1 2. 5, 20 ), Stock ( Piggy, 2 0 0. 0 0, 2. 0 ), Stock ( Fozzy, 2 3. 3 0, 45), Stock ( Beaker, 1 2. 2 4, 123) ; The array is created using the default constructor, and then the specialized constructor is used to update the specified elements in the array. John Chrispell, Monday November 1, 2010 slide 7/25

usestok2.cpp #i n c l u d e <i o s t r e a m > #i n c l u d e stock2. h const i n t STKS = 4 ; i n t main ( ) { using s t d : : cout ; // c r e a t e an a r r a y o f i n i t i a l i z e d o b j e c t s Stock stocks [ STKS ] = { Stock ( Kermit, 12, 2 0. 0 ), Stock ( Fozzy, 200, 2. 0 ), Stock ( Piggy, 130, 3. 2 5 ), Stock ( Beaker, 60, 6. 5 ) ; cout << Stock h o l d i n g s : \ n ; i n t st ; f o r ( s t = 0 ; s t < STKS ; s t++) s t o c k s [ s t ]. show ( ) ; Stock top = s t o c k s [ 0 ] ; f o r ( s t = 1 ; s t < STKS ; s t++) top = top. t o p v a l ( s t o c k s [ s t ] ) ; cout << \nmost v a l u a b l e h o l d i n g : \ n ; top. show ( ) ; return 0 ; John Chrispell, Monday November 1, 2010 slide 9/25

stock2.h #i f n d e f STOCK2 H #define STOCK2 H c l a s s Stock { p r i v a t e : char company [ 3 0 ] ; i n t s h a r e s ; double s h a r e v a l ; double t o t a l v a l ; void s e t t o t ( ) { t o t a l v a l = s h a r e s s h a r e v a l ; public : Stock ( ) ; // d e f a u l t c o n s t r u c t o r Stock ( const char co, i n t n = 0, double pr = 0. 0 ) ; Stock ( ) ; // do n o t h i n g d e s t r u c t o r void buy ( i n t num, double p r i c e ) ; void s e l l ( i n t num, double p r i c e ) ; void update ( double p r i c e ) ; void show ( ) const ; const Stock & t o p v a l ( const Stock & s ) const ; ; #e n d i f John Chrispell, Monday November 1, 2010 slide 11/25

stock2.cpp / Parts of the stock2. cpp f i l e /... void Stock : : show ( ) const { using s t d : : cout ; using s t d : : e n d l ; using s t d : : i o s b a s e ; // #.## format cout. p r e c i s i o n ( 2 ) ; cout. s e t f ( i o s b a s e : : f i x e d, i o s b a s e : : f l o a t f i e l d ) ; cout. s e t f ( i o s b a s e : : showpoint ) ; cout << Company : << company << Shares : << s h a r e s << e n d l << Share P r i c e : $ << s h a r e v a l << Total Worth : $ << t o t a l v a l << e n d l ; const Stock & Stock : : t o p v a l ( const Stock & s ) const { i f ( s. t o t a l v a l > t o t a l v a l ) return s ; e l s e return t h i s ; John Chrispell, Monday November 1, 2010 slide 13/25

Class Scope Constants You may want to have a symbolic constant with class scope. But the following code will not work: c l a s s Stock { p r i v a t e : const i n t Len = 3 0 ; // d e c l a r e a c o n s t a n t f a i l s! char company [ Len ] ;... John Chrispell, Monday November 1, 2010 slide 15/25

Class Scope Constants You may want to have a symbolic constant with class scope. But the following code will not work: c l a s s Stock { p r i v a t e : const i n t Len = 3 0 ; // d e c l a r e a c o n s t a n t f a i l s! char company [ Len ] ;... This will not work as declaring a class describes what an object looks like but doesn t create an object. Hence, until you create an object, there is no place to store the value. Work around! There are two ways to get past this issue. John Chrispell, Monday November 1, 2010 slide 15/25

Class Scope Constants You can use an enumeration within a class. c l a s s Stock { p r i v a t e : enum {Len = 30; // c l a s s s p e c i f i c c o n s t a n t works! char company [ Len ] ;... You can use the keyword static c l a s s Stock { p r i v a t e : s t a t i c const i n t Len = 3 0 ; // d e c l a r e a constan! WORKS char company [ Len ] ;... Stored staticly with the other static variables rather than in the object. John Chrispell, Monday November 1, 2010 slide 17/25

Abstract Data Types The Stock class is pretty specific. Classes are a good way to implement what a computer scientist would describe as abstract data types (ADTs). One abstract data type is called a stack. Lets look at a stack in general way. They hold several item making it a container: and it is characterized by the operations you can perform on it: You can create an empty stack. You can add an item to the top of the stack (called a push). You can remove an item from the top (called a pop). You can check whether the stack is full. You can check whether the stack is empty. What would a class for a stack look like? John Chrispell, Monday November 1, 2010 slide 19/25

stack.h #i f n d e f STACK H #define STACK H typedef unsigned long Item ; // c l a s s t e m p l a t e s a r e s t i l l to come! c l a s s Stack { p r i v a t e : enum {MAX = 10; // c o n s t a n t s p e c i f i c to c l a s s Item i t e m s [MAX] ; // h o l d s s t a c k i t e m s i n t top ; // i n d e x f o r top s t a c k item public : Stack ( ) ; bool isempty ( ) const ; // i s e m p t y ( ) r e t u r n s f a l s e i f s t a c k a l r e a d y i s empty bool i s f u l l ( ) const ; // i s f u l l ( ) r e t u r n s f a l s e i f s t a c k a l r e a d y i s f u l l ; bool push ( const Item & item ) ; // add item to s t a c k bool pop ( Item & item ) ; // pop top i n t o item #e n d i f John Chrispell, Monday November 1, 2010 slide 21/25

stack.cpp // s t a c k. cpp Stack member f u n c t i o n s #include stack. h Stack : : Stack ( ) { // c r e a t e an empty s t a c k top = 0 ; bool Stack : : isempty ( ) const { return top == 0 ; bool Stack : : i s f u l l ( ) const { return top == MAX; bool Stack : : push ( const Item & item ){ i f ( top < MAX) { items [ top++] = item ; return true ; e l s e { return false ; bool Stack : : pop ( Item & item ){ i f ( top > 0){ item = items[ top ] ; return true ; e l s e { return false ; John Chrispell, Monday November 1, 2010 slide 23/25

stacker.cpp #i n c l u d e <i o s t r e am > #i n c l u d e <cctype> // or ctype. h #i n c l u d e stack. h i n t main ( ) { using namespace std ; Stack st ; // create an empty stack char ch ; unsigned long po ; cout << Please enter A to add a purchase order, \ n << P to p r o c e s s a PO, or Q to q u i t. \ n ; while ( c i n >> ch && toupper ( ch )!= Q ){ while ( c i n. get ( )!= \n ){ continue ; i f (! i s a l p h a ( ch ) ) { cout << \a ; continue ; switch ( ch ){ case A : case a : cout << Enter a PO number to add : ; c i n >> po ; i f ( s t. i s f u l l ( ) ) cout << s t a c k a l r e a d y f u l l \n ; e l s e s t. push ( po ) ; break ; case P : case p : i f ( s t. isempty ( ) ) { cout << s t a c k a l r e a d y empty\n ; e l s e { s t. pop ( po ) ; cout << PO # << po << popped \n ; break ; cout << Please enter A to add a purchase order, \ n << P to p r o c e s s a PO, or Q to q u i t. \ n ; cout << Bye\n ; return 0 ; John Chrispell, Monday November 1, 2010 slide 25/25