180 likes | 331 Views
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) {
E N D
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
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 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
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; } 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…