11 Introduction to Object Oriented Programming (Continued) Cats.

Slides:



Advertisements
Similar presentations
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
Advertisements

1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
True or false A variable of type char can hold the value 301. ( F )
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
CS-2303 System Programming Concepts
Basic Elements of C++ Chapter 2.
11 Introduction to Object Oriented Programming (Continued) Cats II.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
February 11, 2005 More Pointers Dynamic Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
ITEC 320 C++ Examples.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 CSC103: Introduction to Computer and Programming Lecture No 24.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
1 CSC241: Object Oriented Programming Lecture No 05.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Getting Started with C++ Part 2 Linux. 2 Getting Started on Linux Now we will look at Linux. See how to copy files between Windows and Linux Compile.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
1 Project 2: Sorting Cats. Write a C++ console application to read a text file containing information about cats and output the information to the screen.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Introduction to Object Oriented Programming Chapter 10.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 The Standard Template Library Drozdek Section 3.7.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
1 Implementing Ticket Printer. Download project from last class Downloads/2016_02_12_In_Class/
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Classes.
Reading from and Writing to Files
Today’s Objectives 28-Jun-2006 Announcements
Class rational part2.
Reading from and Writing to Files
Presentation transcript:

11 Introduction to Object Oriented Programming (Continued) Cats

22 Objectives You will be able to: Write and use classes with multiple member variables. Use the "this" pointer in methods. Use "const" correctly in various places within a method declaration. Understand #pragma once Create objects dynamically. Use dynamically created objects in a C++ program. Get keyboard input from a user in a C++ program.

3 Class Cat Let's create a class to represent cats. Perhaps for use in a veterinarian's office. Each cat has: Name Weight Date of birth

4 New Project Create a new C++ console project in Visual Studio.

5 New Project

6

7

Add main 8

9

10 Start with a Stub Build and run

11 Program Running We have a working program!

12 Add Class Cat

13 Add Class Cat

14 Add Class Cat Note that class name is a singular noun.

15 Initial Class Cat Note #pragma once.

16 #pragma once Same as the traditional guard: #ifndef CAT_H #define CAT_H... // Definition of class Cat... #endif Not ISO Standard C++, but widely supported by current compilers.

17 Add Member Variables Each cat has: Name Weight Date of birth For Name, use a C++ String For Weight, use a double. What about Date of Birth?

18 Date of Birth The C standard library has a way to represent date/time values and functions to manipulate them. But it's complicated Let's define a simple, easy to use, struct to represent dates in a user-friendly form: Day Month Year

19 Cat.h #pragma once #include using namespace std; struct Date { int Day; int Month; int Year; }; class Cat { private: string name; Date date_of_birth; double weight; public: Cat(string name_, Date dob_, double weight_); string Name() const { return name; }; Date Date_of_Birth() const { return date_of_birth; }; double Weight() const { return weight; }; ~Cat(void); };

The C++ string class C++ supports C style strings Arrays of char Used in Chapter 10 of the textbook Also has a string class More user friendly Less subject to problems and errors See page 131 and 353 of the textbook 20

21 Cat.cpp #include "Cat.h" Cat::Cat (string name_, Date dob_, double weight_) { name = name_; date_of_birth = dob_; weight = weight_; } Cat::~Cat(void) { } Build and run the project

22 Program Running

23 Accessor Functions We can create Cat objects now, but we can't do anything with them. To make the attributes of a Cat visible outside the class, create public we need to provide accessor functions. Functions that just return the value of a member variable.

24 Accessor Functions public: Cat(string name_, Date dob_, double weight_); string Name() const { return name; }; Date Date_of_Birth() const { return date_of_birth;}; double Weight() const {return weight;}; ~Cat(void); }; What are all those const's ? Note that we are putting the implementation of the methods in the class definition. Methods will be compiled in line.

25 main.cpp #include #include "Cat.h" using namespace std; int main() { cout << "This is program Cats\n"; Date dob = {12,1,2008}; Cat Fluffy("Fluffy", dob, 8.4); cout << "Cat: " << Fluffy.Name() << " "; cout << "DoB: " << Fluffy.Date_of_Birth().Month << "/" << Fluffy.Date_of_Birth().Day << "/" << Fluffy.Date_of_Birth().Year << " "; cout << "Weight: " << Fluffy.Weight(); cin.get(); // Hold the window open. return 0; }

26 Program Running

27 Display Method We often want a method in a class that outputs the member variables to the screen. Add a new public method to Cat.h: void Display() const;

28 In Cat.cpp #include #include "Cat.h"... void Cat::Display() const { cout << "Cat: " << name << " "; cout << "DoB: " << date_of_birth.Month << "/" << date_of_birth.Day << "/" << date_of_birth.Year << " "; cout << "Weight: " << weight; }

29 main.cpp #include #include "Cat.h" using namespace std; int main() { cout << "This is program Cats\n"; Date dob = {12,1,2008}; Cat Fluffy("Fluffy", dob, 8.4); … cout << endl; Fluffy.Display(); cout << endl; cin.get(); // Hold window open. return 0; } Build and run.

30 Program Running

31 Dynamic Allocation We can dynamically create objects of any class using new. Similar to malloc in C. Allocates space on the heap. Invokes constructor to initialize the object. Returns a pointer to the object.

32 Dynamic Allocation #include #include "Cat.h" using namespace std; int main() { cout << "This is program Cats\n"; Date dob = {12,1,2008}; Cat* Fluffy = new Cat("Fluffy", dob, 8.4); Fluffy->Display(); cout << endl; cin.get(); // Hold window open. return 0; } Note arrow. Fluffy is now a pointer. Build and run

Program Cats 33

34 Object Lifetime Objects allocated by declaration are deallocated when the function exits. Like all local variables. Objects allocated with new continue to exist until explicitly deleted (or until the program ends.) delete c1; c1 is a pointer to the object that is to be deleted.

35 Explicitly Deleting an Object #include #include "Cat.h" using namespace std; int main() { cout << "This is program Cats\n"; Date dob = {12,1,2008}; Cat* Fluffy = new Cat("Fluffy", dob, 8.4); Fluffy->Display(); cout << endl; delete Fluffy; Fluffy = 0; cin.get(); // Hold window open. return 0; }

36 Getting User Input What if we want the user to specify the attributes of a Cat. Could overload the constructor and provide a version that asks for input. Better to provide a separate function outside the class definition. Separate UI from class logic. Let’s write a function that asks the user for information about a Cat, creates a Cat object, and returns a pointer to the object.

37 main.cpp Cat* Create_Cat() { string name; Date date_of_birth; double weight; cout << "Please enter information for new Cat\n"; cout << "Name: "; cin >> name; cout << "Date of Birth:\n"; cout << " Month: "; cin >> date_of_birth.Month; cout << " Day: "; cin >> date_of_birth.Day; cout << " Year: "; cin >> date_of_birth.Year; cout << "Weight: "; cin >> weight; Cat* cat = new Cat(name, date_of_birth, weight); return cat; }

38 Getting User Input int main() { cout << "This is program Cats\n"; Cat* Fluffy = Create_Cat(); Fluffy->Display(); cout << endl; delete Fluffy ; Fluffy = 0; cin.get(); // Hold window open. cin.get(); return 0; } In file main.cpp

39 Running Program Cats

40 Multiple Cats int main() { cout << "This is program Cats\n"; while (true) { Cat* Fluffy = Create_Cat(); Fluffy->Display(); cout << endl; delete Fluffy; Fluffy = 0; }...

41 A Problem with Numeric Input The x in 8.x was left in the keyboard input buffer and read as name of the next cat.

A Problem with Numeric Input 42

43 Solution After numeric input, clear the keyboard input buffer before doing the next real input. while (cin.get() != '\n') ; Do nothing

44 Clear_Keyboard_Input_Buffer() void Clear_Keyboard_Input_Buffer() { while (cin.get() != '\n') ; } Call this function after getting a numeric value to clear any characters following the numeric text.

45 Using Clear_Keyboard_Input_Buffer Cat* Create_Cat() {... cout << "Date of Birth:\n"; cout << " Month: "; cin >> date_of_birth.Month; Clear_Keyboard_Input_Buffer(); cout << " Day: "; cin >> date_of_birth.Day; Clear_Keyboard_Input_Buffer(); cout << " Year: "; cin >> date_of_birth.Year; Clear_Keyboard_Input_Buffer(); cout << "Weight: "; cin >> weight; Clear_Keyboard_Input_Buffer(); Cat* cat = new Cat(name, date_of_birth, weight); return cat; }

46 Program in Action Garbage at end of numeric inputs was silently ignored.

47 Assignment Do today's example for yourself if you have not done it in class. Read Chapter 10. End of Presentation