CSCI261E/F Lecture 19: Classes & Objects November 1, 2010 ? - - PowerPoint PPT Presentation

csci261e f
SMART_READER_LITE
LIVE PREVIEW

CSCI261E/F Lecture 19: Classes & Objects November 1, 2010 ? - - PowerPoint PPT Presentation

CSCI261E/F Lecture 19: Classes & Objects November 1, 2010 ? Programs & Things Variables (ints, doubles, chars, etc) Data Structures (arrays) Things (strings, ...what else?) Any Thing* * aka Object Ape Person


slide-1
SLIDE 1

CSCI261E/F

Lecture 19: Classes & Objects November 1, 2010

slide-2
SLIDE 2

?

slide-3
SLIDE 3

Programs & Things

  • Variables (ints, doubles, chars, etc)
  • Data Structures (arrays)
  • Things (strings, ...what else?)
slide-4
SLIDE 4

Any Thing*

  • Ape
  • Person
  • Yuppie
  • ColdHeartedExecutiveRobot
  • Cyborg

* aka Object

slide-5
SLIDE 5

What is a Thing?

slide-6
SLIDE 6

Properties

  • Height
  • Width
  • Depth
  • Material Type
  • Style
  • Price
  • Location

Table

  • Height
  • Width
  • Depth
  • Material Type
  • Style
  • Price
  • Location

Chair

slide-7
SLIDE 7

Behavior

  • Support ____
  • cup
  • candle
  • person
  • etc

Table Chair

  • Support ____
  • person
  • bag
  • whoopee

cushion

  • etc
slide-8
SLIDE 8

Composition

  • Consists of ____
  • Table
  • Chair
  • Chair
  • Chair

Dining Set “has a”

slide-9
SLIDE 9

Inheritance

Piece Of Furniture

  • Height
  • Width
  • Depth
  • Material Type

Table Chair Bed Desk DiningTable NightStand “is a”

slide-10
SLIDE 10

Program w/ a ‘Table’

int main() { int height = 3; int width = 6; int depth = 2; string material = “wood”; mytable = new Table(height, width, material); cout << table.width << endl; cout << table.height << endl; cout << table.material << endl; return 0; }

Instantiate a new Table object for me Call it mytable. A table object has properties. You access object properties with the ‘dot’ operator.

slide-11
SLIDE 11

Dot-This, Dot-That

string name = clown.name; int age = clown.age; clown.name = “Homie”; clown.juggle(); clown.tell_joke(joke); access assignment functions

slide-12
SLIDE 12

string Object

string word = “revolution”; word.length();

Instantiate a string whose content is “revolution.” Call the string’s member function length().

(A function that belongs to an object.) (aka method)

slide-13
SLIDE 13

So You Want a Table

How can I create a table in my program? (instantiation) What does the table look like? (properties) How do I put stuff on the table? (behavior) Most importantly: How can I teach the computer what a table is? (?)

slide-14
SLIDE 14

Declare a Table Class

  • A general description of what a table is
  • “furniture that can support things”
  • “has a width, height, depth”
  • “has some material type, like wood”
  • “costs money and has a price”
slide-15
SLIDE 15

Table Class

class Table { private: const int MAX_THINGS = 10; string on_the_table[MAX_THINGS] = {“”}; public: int height, width, length; double price; string material_name; bool support(string item_name); };

“Computer, a Table is a thing.” “It has a height, width and length.” “It has a price.” “It is made of some kind of material.” “It can support things (by name).”

Prototype or Interface in Table.h

slide-16
SLIDE 16

Table Class

#include “Table.h” Table::support(string item_name) { for (int i = 0; i < MAX_THINGS; ++i) { if on_the_table[i] == “” {

  • n_the_table[i] = item_name;

return true; } } return false; }

“Computer, a Table is a thing that is described in Table.h.” “I said it can support things, and here’s how.”

Implementation in Table.cpp

slide-17
SLIDE 17

So You Want a Table

How can I create a table in my program? (instantiation) What does the table look like? (properties) How do I put stuff on the table? (behavior) Most importantly: How can I teach the computer what a table is? (?)

slide-18
SLIDE 18

Constructors

  • A special function
  • Named after the class name
  • Called automatically upon initialization
slide-19
SLIDE 19

Using Constructors

int main() { int height(3); int width(6); int depth(2); string material(“wood”); Table mytable(height, width, material); // ... return 0; }

Instantiate a new Table object for me and pass these values to the constructor function. Instantiate a new string object for me with the value “wood”

slide-20
SLIDE 20

Declaring Constructors

  • Declared in implementation file, with other functions
  • Must be named after the class
  • So the machine knows it’s a constructor
slide-21
SLIDE 21

Example

SolidBeginningsClass

slide-22
SLIDE 22

Homework

  • Read 8.1 - 8.2, 8.4, 8.7
  • Skip p361 “Initialization Lists”
  • Complete assignment SolidBeginningsClass