1 CSC 222: Computer Programming II Spring 2004  classes and objects  abstract data types, classes and objects  using existing classes, #include, member.

Slides:



Advertisements
Similar presentations
1 CSC 222: Computer Programming II Spring 2004 HW1 review arrays vs. vectors class design object-oriented design data objects + functionality example:
Advertisements

 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Working with Strings Lecture 2 Hartmut Kaiser
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
C++ Programming: Basic Elements of C++.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: User-Defined Classes Problem Solving, Abstraction, and Design.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
38 4/11/98 CSE 143 Modules [Chapter 2]. 39 4/11/98 What is a Module?  Collection of related items packaged together  Examples:  Stereo System Components.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CSC 222: Computer Programming II Spring 2004  Review of C++ basics  program structure, input, output  variables, expressions, functions, parameters.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 11 Separate Compilation and Namespaces. Learning Objectives Separate Compilation –Encapsulation reviewed –Header and implementation files Namespaces.
Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
SEEM Java – Basic Introduction, Classes and Objects.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
CSC 221: Computer Programming I Fall 2001  arrays  homogeneous collection of items, accessible via an index  initializing/traversing/displaying arrays.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
CSC 221: Computer Programming I Fall 2001  C++ libraries  cmath, cctype  classes and objects  abstract data types, classes and objects  data fields,
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Chapter 13 - The Preprocessor
Working with Strings Lecture 2 Hartmut Kaiser
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Summary of what we learned so far
Working with Strings Lecture 2 Hartmut Kaiser
C Preprocessor(CPP).
Introduction to Classes and Objects
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
C++ Programming Basics
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Copyright  1997 Oxford University Press All Rights Reserved
Presentation transcript:

1 CSC 222: Computer Programming II Spring 2004  classes and objects  abstract data types, classes and objects  using existing classes, #include, member function calls  user-defined classes  data fields, member functions  private vs. public, constructor, separate compilation  examples: Die, MarbleJar

2 Abstract data types (ADTs) for primitive types such as numbers and characters, libraries of stand-alone functions suffice (e.g., cmath, cctype ) for defining/manipulating more complex types, a better (more generalizable) approach is needed an abstract data type (ADT) is a collection of data and the associated operations that can be applied to that data e.g., an integer is a number (series of digits) with assumed operations: addition, subtraction, multiplication, division, compare with other integers, … e.g., a string is a sequence of characters enclosed in quotes with operations: concatenation, determine it length, access a character, access a substring, …

3 C++ classes and ADT's in C++, new abstract data types can be defined as classes  once a class has been defined and #included into a program, it is indistinguishable from the predefined data types of C++  by defining a new class, you can extend the expressibility of C++  when you create an instance of a class, that object encapsulates the data and operations of that class (a.k.a. member functions ) EXAMPLE: the C++ string class in DATA: a sequence of characters (enclosed in quotes) MEMBER FUNCTIONS: + >> << // operators for concatenation, input, and output int length(); // returns number of chars in string char at(int index); // returns character at index (first index is 0) string substr(int pos, int len);// returns substring starting at pos of length len int find(string substr); // returns position of first occurrence of substr, // returns constant string::npos if not found int find(char ch); // similarly finds index of character ch...

4 Using a class to use a class, must:  #include its definition  know how to create an object (constructor arguments?)  know how to call its member functions in general, to call a member function: object.funtionCall e.g., word.length() word.at(0) word.find(ch) #include using namespace std; string Capitalize(string str); int main() { string word = "foobar"; cout << word << " contains " << word.length() << " characters." << endl; cout << "Capitalized, it's: " << Capitalize(word) << endl; char ch; cout << "Enter a character to search for: "; cin >> ch; int index = word.find(ch); if (index == string::npos) { cout << word << " does not contain " << ch << endl; } else { cout << ch << " is found at index " << index << endl; } return 0; } //////////////////////////////////////////////////////////// string Capitalize(string str) // Assumes: str is a single word (no spaces) // Returns: a copy of str with the first char capitalized { char cap = toupper(str.at(0)); return cap + str.substr(1, str.length()-1); }

5 User-defined classes // doubles.cpp ///////////////////////////////////////////// #include using namespace std; int DieRoll(int numSides = 6); int main() { srand(unsigned(time(0))); int roll1 = -1; int roll2 = -2; int rollCount = 1; while (roll1 != roll2) { roll1 = DieRoll(); roll2 = DieRoll(); cout << "You rolled " << roll1 << " and " << roll2 << endl; rollCount++; } cout << "It took " << rollCount << " roll(s)." << endl; return 0; } /////////////////////////////////////////////////// int DieRoll(int numSides) { return rand()%numSides + 1; } similar to string, you can define your own classes in C++ example: suppose we wanted to conduct dice simulations ugly code  lots of detail  repetition also, we would like to make it easy to  create dice with different # of sides  keep track of # of rolls

6 Die class can encapsulate behavior of a die in a new class (type) DATA: number of sides, count of number of rolls OPERATIONS: roll the die, determine # sides, determine # rolls so far in order to use a class (type), you must  #include its definition #include "Die.h" quotes around file name specify a user-defined class (compiler will look in local directory)  know how to create an object and apply the member functions int Roll(); returns random roll of die int NumSides(); returns number of sides on die int numRolls(); returns number of times has been rolled

7 Dice simulation (with Die objects) // doubles.cpp ///////////////////////////////////////////// #include #include "Die.h" using namespace std; int main() { Die d1, d2; int roll1 = -1; int roll2 = -2; while (roll1 != roll2) { roll1 = d1.Roll(); roll2 = d2.Roll(); cout << "You rolled " << roll1 << " and " << roll2 << endl; } cout << "It took " << d1.NumRolls() << " roll(s)." << endl; return 0; } cleaner & more general  hides ugly details of getting time, calling srand, and squeezing the random number into the range 1 to 6  hides inclusion of low-level libraries  makes it easy to create different sized dice, e.g., Die d1(8), d2(8);  encapsulates the code for keeping track of # sides and # of rolls

8 Die class implementation (overview) class Die { public: Die(int sides = 6); int Roll(); int NumSides() const; int NumRolls() const; private: int rollCount; int numSides; }; In C++, a class definition defines the structure and behavior of a new type class CLASSNAME { public: MEMBER FUNCTION PROTOTYPES; private: DATA FIELDS; }; data fields (or instance variables ) maintain the state of the object member functions (or methods ) define the operations on the data generally, member functions are public (meaning accessible to client programs), while data fields are private (meaning hidden from client programs) each class has a special member function called a constructor same name as class, no return type performs any necessary initializations for an object

9 Die.h // Die.h // // A class for simulating an N-sided die. #ifndef _DIE_H_ #define _DIE_H_ #include using namespace std; class Die { public: Die(int sides = 6); int Roll(); int NumSides() const; int NumRolls() const; private: int rollCount; // # times die rolled int numSides; // # sides on die }; #endif generally, a class definition is broken into 2 files  header (.h) file contains the class definition w/ function prototypes  implementation (.cpp) file contains the member function definitions #ifndef, #define, #endif are preprocessor directives – ensure that the file doesn't get included more than once in a project

10 Die.cpp #include #include "Die.h" using namespace std; Die::Die(int sides = 6) // constructor: initializes data fields { srand(unsigned(time(0))); rollCount = 0; numSides = sides; } Die::int Roll() // Returns: a random 'die' roll (and incr. count) { int dieRoll = rand()%numSides + 1; rollCount++; return dieRoll; } Die::int NumSides() const // Returns: # of sides on the die { return numSides; } Die::int NumRolls() const // Returns: # of times die has rolled { return rollCount; }.cpp file contains the definitions of the member functions  class membership is specified using :: when creating a.NET project, all.cpp files must be added.NET performs smart compilation –.cpp files are only recompiled if they changed since the last compilation *actually, Die.cpp contains additional code to ensure srand only called once

11 In-class exercise create a Win-32 Console Application named rolls (on T: drive) create a new files named Die.h and Die.cpp,cut-and-paste code from create a new file named rolls.cpp  write code that performs 10,000 dice rolls, counts & displays number of 7's add rolls.cpp and Die.cpp to project

12 Private data information hiding (via private data & public member functions) is not just a good thing, it's a necessary thing for implementing ADTs  ADT operations specify all ways to manipulate data  must protect form unauthorized access e.g., shouldn't be able to change the # of sides of an existing die another example: Marble puzzles Suppose a jar contains 50 black marbles & 50 white marbles. If you draw 2 marbles at random, how likely is it that they are the same color? if we wished to simulate a marble jar, what operations should it support?

13 MarbleJar class implementation class MarbleJar { public: MarbleJar(int black, int white); string DrawMarble(); void AddMarble(string marble); bool IsEmpty() const; private: int numBlack, numWhite; }; class definition provides:  constructor in which the initial number of black & white marbles is specified  member functions for adding & removing a marble, checking to see if jar is empty  private data fields to keep track of number of marbles of each color

14 Separate compilation // MarbleJar.h #ifndef _MARBLEJAR_H_ #define _MARBLEJAR_H_ #include using namespace std; class MarbleJar { public: MarbleJar(int black, int white); string DrawMarble(); void AddMarble(string marble); bool IsEmpty() const; private: int numBlack, numWhite; }; #endif for user-defined classes, it's good practice to divide the definition into 2 files:  header (.h) file: contains class definition with member function prototypes  implementation (.cpp) file: contains member function definitions advantages  header file is simple, serves as outline for the class .NET performs smart compilation – will compile.cpp files only as needed

15 MarbleJar.cpp // Marblejar.cpp #include "Die.h" #include "MarbleJar.h" MarbleJar::MarbleJar(int black, int white) // precondition : black >= 0, white >= 0 // postcondition: numBlack = black, numWhite = white { numBlack = black; numWhite = white; } string MarbleJar::DrawMarble() // precondition : jar is not empty (numBlack+numWhite > 0) // postcondition: draws a random marble and returns its color // (either "black" OR "white"). As a side effect, // the corresponding color count is decremented. { Die myDie(numBlack+numWhite); if (myDie.Roll() <= numBlack) { numBlack--; return "black"; } else { numWhite--; return "white"; }.cpp file contains member function definitions  class ownership specified using :: note: DrawMarble uses a Die object  think of all marbles as numbered, black first  uses Die roll to pick a random marble number  then determines if number corresponds to black or white

16 MarbleJar class implementation (cont.) // CONTINUING Marblejar.cpp void MarbleJar::AddMarble(string marble) // precondition : marble == "black" or "white" // postcondition: adds a marble of the specified color to the jar, // incrementing the color count. { if (marble == "black") { numBlack++; } else if (marble == "white") { numWhite++; } bool MarbleJar::IsEmpty() const // precondition : jar contains numBlack and numWhite marbles // postcondition: returns true if jar is empty (no marbles) { return (numBlack + numWhite == 0); } adding a marble simply increments the corresponding data field what if an invalid marble color is specified?

17 MarbleJar simulation // marbles.cpp #include #include "MarbleJar.h" using namespace std; const int NUM_REPS = ; int main() { int numMarbles; cout << "Enter initial number of marbles of each color: "; cin >> numMarbles; assert(numMarbles >= 1); int sameCount = 0; for (int rep = 0; rep < NUM_REPS; rep++) { MarbleJar jar(numMarbles, numMarbles); if (jar.DrawMarble() == jar.DrawMarble()) { sameCount++; } cout << "The marbles were the same " << 100.0*sameCount/NUM_REPS << "% of the time" << endl; return 0; } here, use the assert function from to verify that the jar is not initially empty

18 Enumerated types enum Color { BLACK, WHITE }; class MarbleJar { public: MarbleJar(int black, int white); Color DrawMarble(); void AddMarble(Color marble); bool IsEmpty() const; private: int numBlack, numWhite; }; ////////////////////////////////////////// #include "Die.h" #include "MarbleJar.h" MarbleJar::MarbleJar(int black, int white) { numBlack = black; numWhite = white; } Color MarbleJar::DrawMarble() { Die myDie(numBlack+numWhite); if (myDie.Roll() <= numBlack) { numBlack--; return BLACK; } else { numWhite--; return WHITE; } void MarbleJar::AddMarble(Color marble) { if (marble == BLACK) { numBlack++; } else { numWhite++; } bool MarbleJar::IsEmpty() const { return (numBlack + numWhite == 0); } could use enum to define a Color type safer, since limits legal values unfortunately, can't output directly

19 In-class exercise create a Win-32 Console Application named marbles (on T: drive) create new files and cut-and-paste code from add MarbleJar.cpp, Die.cpp, and marbles.cpp to the project QUESTION: does the number of marbles affect the percentage? How?