1 / 18

CMSC 202

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) {

magnar
Download Presentation

CMSC 202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Lesson 7 Classes I

  2. 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(); }

  3. 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

  4. 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?

  5. OOP Example • What properties (data) does a chair have? • What actions (operations) does a chair have?

  6. Practice • Box of DVDs • What data does the box have? • What operations does the box have?

  7. 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

  8. 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

  9. 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

  10. 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…

  11. 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

  12. 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; Struct vs. Class

  13. 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(), …

  14. 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();

  15. 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

  16. 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

  17. 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

  18. 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…

More Related