CMSC 202 Lesson 7 Classes I.

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 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
Definition Class In C++, the class is the construct primarily used to create objects.
C++ fundamentals.
 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:
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
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.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
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.
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.
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.
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
Andrew(amwallis) Classes!
Structures and Classes
Procedural and Object-Oriented Programming
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Inheritance CMSC 202, Version 4/02.
Templates.
Classes and OOP.
Abstract Data Types Programmer-created data types that specify
Review What is an object? What is a class?
Andy Wang Object Oriented Programming in C++ COP 3330
COMP 51 Week Twelve Classes.
Objectives Identify the built-in data types in C++
About the Presentations
Implementing Functions from a Detailed Design Quick Tips
Introduction to Classes
This technique is Called “Divide and Conquer”.
Classes In C#.
Introduction to Classes
Classes and Data Abstraction
Object-Oriented Programming
Learning Objectives Classes Constructors Principles of OOP
Defining Classes I Part A.
Constructors and Other Tools
Object-Oriented Programming
COP 3330 Object-oriented Programming in C++
Session 2: Introduction to 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.
Data Structures and ADTs
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 8 Object Oriented Programming (OOP)
Introduction to Computer Science and Object-Oriented Programming
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 Car Operations Encapsulation? 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? struct DayOfYear { int month; int day; 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 Bad 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 Object Car My physical car Building ITE Building Chair Collection of data and Operations on that data Object Instance of a class Examples (on right!) Blueprint, pattern, classification Thing you built, made, constructed Class Object Car My physical car Building ITE Building Chair This particular chair Marker A particular marker Date July 4th, 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 }; Class-name Protection Mechanism Operations Protection Mechanism Data

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 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; } Scope Resolution Operator: indicates which class this method is from Class Name 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 Declaration Goes in file ClassName.h Class Definition Goes in file ClassName.cpp

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…