cmsc 202

18
CMSC 202 Lesson 7 Classes I

Upload: zephania-estrada

Post on 31-Dec-2015

16 views

Category:

Documents


0 download

DESCRIPTION

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) { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CMSC 202

CMSC 202

Lesson 7Classes I

Page 2: CMSC 202

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

}

Page 3: CMSC 202

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

Page 4: CMSC 202

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?

Page 5: CMSC 202

OOP Example What properties (data)

does a chair have? What actions

(operations) does a chair have?

Page 6: CMSC 202

Practice Box of DVDs

What data does the box have?

What operations does the box have?

Page 7: CMSC 202

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

Page 8: CMSC 202

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

Page 9: CMSC 202

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

Class Object

Car My physical car

Building ITE Building

Chair This particular chair

Marker A particular marker

Date July 4th, 05

Page 10: CMSC 202

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…

Page 11: CMSC 202

Class Exampleclass 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

Protection Mechanism

Page 12: CMSC 202

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

Page 13: CMSC 202

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

Page 14: CMSC 202

Class - DayOfYear// Represents a Day of the Yearclass DayOfYear{

public:void Output();int m_month;int m_day;

};

// Output method – displays a DayOfYearvoid DayOfYear::Output(){

cout << m_month << “/” << m_day;}

// Code from main()DayOfYear july4th;july4th.m_month = 7;july4th.m_day = 4;july4th.Output();

Page 15: CMSC 202

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

Page 16: CMSC 202

Classes// Represents a Day of the Yearclass DayOfYear{

public:void Output();int m_month;int m_day;

};

// Output method – displays a DayOfYearvoid DayOfYear::Output(){

cout << m_month << “/” << m_day;}

Class Definition

Goes in file ClassName.cpp

Class Declaration

Goes in file ClassName.h

Page 17: CMSC 202

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

Page 18: CMSC 202

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…