cmsc 202 l.
Download
Skip this Video
Download Presentation
CMSC 202

Loading in 2 Seconds...

play fullscreen
1 / 18

CMSC 202 - PowerPoint PPT Presentation


  • 98 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CMSC 202' - magnar


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cmsc 202

CMSC 202

Lesson 7

Classes I

warmup
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
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
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 example5
OOP Example
  • What properties (data) does a chair have?
  • What actions (operations) does a chair have?
practice
Practice
  • Box of DVDs
  • What data does the box have?
  • What operations does the box have?
structures
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

structures8
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
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
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 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;

Struct vs. Class
class rules coding standard
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
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
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

classes16
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

practice17
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
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…