CMSC 202 Lesson 10 Classes IV. Warmup Class Oven { public Oven( int initTemp = 0 ); void SetTemp( int newTemp ); int GetTemp() const; private int m_temp.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
1 Object-Oriented Programming Using C++ CLASS 5. 2 Object Composition Object composition occurs when a class contains an instance of another class. Creates.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
CMSC 202 Lesson 7 Classes I. Warmup  Add the correct parameter list and return type to the following function: (hint: all the information you need is.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Object-Oriented Programming in C++ More examples of Association.
C++ Classes and Data Structures Jeffrey S. Childs
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Objects and Classes Project 2 description.
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of 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?
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
11 Introduction to Object Oriented Programming (Continued) Cats.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
CSIS 123A Lecture 10 Inheritance. Organizing Code Why should programs be organized? –Humans are "complexity challenged" Need simplicity, clarity, efficiency.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Classes CMSC 202. Version 9/12 2 Programming & Abstraction All programming languages provide some form of abstraction. –Also called information hiding.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using 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?
Creational Pattern: Prototype
Operator Overloading CMSC 202.
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Review: Two Programming Paradigms
Inheritance II CMSC 202.
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?
Lesson 14 Copy and Assignment
Lists The List ADT.
Object-Oriented Programming (OOP) Lecture No. 39
Domain Classes Chapter 9.
Polymorphism 1 CMSC 202.
Friends, Overloaded Operators, and Arrays in Classes
Inheritance: Polymorphism and Virtual Functions
CS410 – Software Engineering Lecture #5: C++ Basics III
CMSC 202 Lesson 6 Functions II.
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
CS 144 Advanced C++ Programming February 21 Class Meeting
CMSC 202 Lesson 8 Classes II.
CMSC 202 Lesson 19 Polymorphism 2.
CSG2H3 Object Oriented Programming
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

CMSC 202 Lesson 10 Classes IV

Warmup Class Oven { public Oven( int initTemp = 0 ); void SetTemp( int newTemp ); int GetTemp() const; private int m_temp = 0; } Oven( int initTemp = 0 ) : m_temp(initTemp) { } void setTemp( int newTemp ); { newTemp = m_temp; } int GetTemp() { return m_temp; } There are 9 compiler errors (not counting duplicated errors), can you find them all? There is 1 logic error, can you spot it?

Warmup (Corrected) class Oven { public: Oven( int initTemp = 0 ); void SetTemp( int newTemp ); int GetTemp() const; private: int m_temp; }; Oven::Oven( int initTemp ) : m_temp(initTemp) { } void Oven::SetTemp( int newTemp ) { m_temp = newTemp; } int Oven::GetTemp() const { return m_temp; }

Review What term is used for “instance of a class”? What is another term for “information hiding”? What is a name for functions in a class? What is a default constructor? What are the limitations of a const object? What does “const” mean with a method?

Student Class Designing a Student… What data do we need? Name SSN Address Phone ID Course list … Let’s think about the Address, how can we represent that?

Aggregation Objects can hold other objects! Class defines a private data member of another Class-type “has-a” relationship Example class Student { public: // some methods… private: Address m_address; // more data… };

Aggregation We have 3 classes for this project MazeCell Maze MazeCrawler How can we use aggregation here?

Aggregation – Another Look class Vacation { public: Vacation( int month, int day, int nbrOfDays ); // more methods… private: DayOfYear m_startDay; int m_lengthOfTrip; // more data… }; Vacation::Vacation( int month, int day, int nbrOfDays ) : m_startDay(month, day), m_lengthOfTrip(nbrOfDays) { // code… } What’s going on here? Implicit call to the Constructor! Remember – initializer lists were important! Only way to call Constructor!

Aggregation class Vacation { public: Vacation( int month, int day, int nbrOfDays ); // more methods… private: DayOfYear m_startDay; int m_lengthOfTrip; // more data… }; Can Vacation access DayOfYear’s private data members?

Aggregation House “has-a” Front Door Set of bedrooms Garage Address Garage “has-a” Lawnmower Rake Car Car “has-a” Driver Set of passengers Driver “has-a” Name Address … You can have as many layers of aggregation as you need – until you get to a set of primitive types!

Static int foobar() { int a = 10; ++a; return a; } int foobar() { static int a = 10; ++a; return a; } What is returned? Ah…tricky… ‘a’ retains its value between calls to foobar… 11, 12, 13, 14, 15, … 11, 11, 11, 11, 11, …

Static and Classes? Static data member ALL objects share data If one changes, affects all Static methods Can access static data CANNOT access non-static data or methods Regular methods Can access static data Can access non-static data and methods

Static Example class Person { public: static bool SpendMoney(int amount); private: static Wallet m_wallet; Wallet m_moneyClip; }; // In Person.h Wallet Person::m_wallet(0); bool Person::SpendMoney( int amount ) { m_wallet.RemoveMoney(amount); m_moneyClip.RemoveMoney(amount); // compiler error!!! } // In main // Create a person Person Bob; // Bob adds money to the wallet Bob.AddMoney(100); // Anyone can call SpendMoney! Person::SpendMoney(100); // Bob has no money! Bob.SpendMoney(10); // fails!! If any money is spent, everyone has lost that money!

Incremental / Modular Development & Compilation General Programming Approach Bottom-Up Development Work on one class Write one method at a time Develop, test, repeat Test class in isolation Bottom-Up Testing Test one class in isolation Test two classes in isolation (when they are connected) … Test all classes together

Stubbed Class class Stapler { public: Stapler(); bool Staple(); void Fill(); bool AddStaples(int nbrStaples); int GetNbrStaples(); private: int m_nbrStaples(); }; Stapler::Stapler() { } bool Stapler::Staple() { return true; } void Stapler::Fill() { } bool Stapler::AddStaples(int nbrStaples) { return true; } int Stapler::GetNbrStaples() { return 0; } // Testing main int main() { Stapler stapler; cout << stapler.GetNbrStaples() << endl; cout << stapler.Staple() << endl; cout << stapler.GetNbrStaples() << endl; cout << stapler.AddStaples(10) << endl; cout << stapler.GetNbrStaples() << endl; stapler.Fill(); cout << stapler.GetNbrStaples() << endl; cout << stapler.AddStaples(10) << endl; cout << stapler.GetNbrStaples() << endl; return 0; }

P2 - Design Test cases Use these with your Testing main Run tests on your class EVERY time you modify it Implementation Write 5 lines Save Compile Test Repeat

Challenge Come up with 1 GOOD example for each of the following: Class that uses aggregation Class that uses static data This one may be tough… Do not use examples from class, slides, text, or lecture notes…