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.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Object-Oriented Programming OOP. 2 Object-oriented programming is a new paradigm for program development in which the focus is on the data instead of.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Object-Oriented PHP (1)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Definition Class In C++, the class is the construct primarily used to create objects.
ASP.NET Programming with C# and SQL Server First Edition
C++ fundamentals.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Classes CMSC 202. Version 9/12 2 Programming & Abstraction All programming languages provide some form of abstraction. –Also called information hiding.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
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.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
CMSC202 Computer Science II for Majors Lecture 06 – Classes and Objects Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Structures and Classes
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Abstract Data Types Programmer-created data types that specify
About the Presentations
Introduction to Classes
This technique is Called “Divide and Conquer”.
Introduction to Classes
Classes and Data Abstraction
Object-Oriented Programming
CMSC 202 Lesson 7 Classes I.
Learning Objectives Classes Constructors Principles of OOP
Object-Oriented Programming
Object-Oriented Programming
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Classes C++ representation of an object
CMSC 202 Lesson 8 Classes II.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

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 in the function) _____ findDVD(_________ Titles, _________ titleToFind) { for (unsigned int i = 0; i < Titles.size(); ++i) { if (Titles.at(i) == titleToFind) return i; } return Titles.size(); }

Object-Oriented Programming (OOP)  Software Development Goals Code Reuse – save money and time Abstraction – reduce something to essentials  Examples:  Driving a car  Controlling a television  OOP Key Components (Classes) Data Operations (methods/functions)  Usually associated with the data Encapsulation  Details of implementation (both data and algorithms) is hidden from the user of a class

OOP Example  Driving a car… Car Data  Amount of Gas  Current Gear  Automatic or Manual  Mileage  Current Amount of Turn Car Operations  Accelerate  Brake  Change Gear  Add Gas  Turn right  Turn left Encapsulation?

OOP Example  What properties (data) does a chair have?  What actions (operations) does a chair have?

Practice  Box of DVDs  What data does the box have?  What operations does the box have?

Structures  What about structs? Collection of data No operations explicitly related struct DayOfYear { int month; int day; }; DayOfYear july4th; july4th.month = 7; july4th.day = 4; No typedef! Members

Structures  Good Simple Can be parameters to functions Can be returned by functions Can be used as members of other structs  Bad No operations Data is not protected  Any code that has access to the struct object has direct access to all members of that object

Classes  Class Collection of data and Operations on that data  Object Instance of a class  Examples (on right!)  Class Blueprint, pattern, classification  Object Thing you built, made, constructed ClassObject CarMy physical car BuildingITE Building ChairThis particular chair MarkerA particular marker DateJuly 4 th, 05

Classes – a Struct Replacement  Good Simple Objects can be parameters to functions Objects can be returned by functions Objects can be members of other classes Operations linked to data Data is protected  Code that uses an object MUST use the operators of the class to access/modify data of the object (usually)  Bad Nothing really…

Class Example class Car { public: bool AddGas(float gallons); float GetMileage(); // other operations private: float m_currGallons; float m_currMileage; // other data }; Operations Data Class-name Protection Mechanism

Struct vs. Class struct DayOfYear { int month; int day; }; // Code from main() DayOfYear july4th; july4th.month = 7; july4th.day = 4; class DayOfYear { public: int m_month; int m_day; }; // Code from main() DayOfYear july4th; july4th.m_month = 7; july4th.m_day = 4;

Class Rules – Coding Standard  Class names Always begin with capital letter Use mixed case for phrases General word for class (type) of objects  Ex: Car, Boat, Building, DVD, List, Customer, BoxOfDVDs, CollectionOfRecords, …  Class data Always begin with m_  Ex: m_fuel, m_title, m_name, …  Class operations/methods Always begin with capital letter  Ex: AddGas(), Accelerate(), ModifyTitle(), RemoveDVD(), …

Class - DayOfYear // Represents a Day of the Year class DayOfYear { public: void Output(); int m_month; int m_day; }; // Output method – displays a DayOfYear void DayOfYear::Output() { cout << m_month << “/” << m_day; } // Code from main() DayOfYear july4th; july4th.m_month = 7; july4th.m_day = 4; july4th.Output();

Method Implementation void DayOfYear::Output() { cout << m_month << “/” << m_day; } Class Name Scope Resolution Operator: indicates which class this method is from Method Name Method Body

Classes // Represents a Day of the Year class DayOfYear { public: void Output(); int m_month; int m_day; }; // Output method – displays a DayOfYear void DayOfYear::Output() { cout << m_month << “/” << m_day; } Class Definition Goes in file ClassName.cpp Class Declaration Goes in file ClassName.h

Practice  You are working on an Inventory system for a department store Declare a class to represent a Pair of Shoes  What data do we need?  Assume the only operation will be to display their data to the screen. Implement the Output() method Create a Pair of Shoes  Assign the shoes data  Print their data using the Output() method

Challenge  You are working for the Bookstore…  Design Challenge Design a class to represent a textbook  What data should it have?  What operations should it have?  Implementation Challenge I Write the class declaration for a textbook  Implementation Challenge II Write the class definition for a textbook  You need not implement any of the functions… I just want to see their signatures  Use only what we know so far…