CSCI261 Lecture 21: Introduction to Classes ? Object-Oriented - - PowerPoint PPT Presentation

csci261
SMART_READER_LITE
LIVE PREVIEW

CSCI261 Lecture 21: Introduction to Classes ? Object-Oriented - - PowerPoint PPT Presentation

CSCI261 Lecture 21: Introduction to Classes ? Object-Oriented Programming (OOP) Ivan Sutherland "A display connected to a digital computer gives us a chance to gain familiarity with concepts not realizable in the physical world. It is a


slide-1
SLIDE 1

CSCI261

Lecture 21: Introduction to Classes

slide-2
SLIDE 2

?

slide-3
SLIDE 3

Object-Oriented Programming

(OOP)

slide-4
SLIDE 4

Ivan Sutherland

"A display connected to a digital computer gives us a chance to gain familiarity with concepts not realizable in the physical world. It is a looking glass into a mathematical wonderland." "The ultimate display would, of course, be a room within which the computer can control the existence of matter. A chair displayed in such a room would be good enough to sit in. Handcuffs displayed in such a room would be confining, and a bullet displayed in such a room would be fatal." (1965)

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

Objects

  • Behavior
  • Data
  • Abstraction
slide-11
SLIDE 11

Person buddy(“John”, 23); Book odm(“One-Dimensional Man”, 200); buddy.readBook(odm);

slide-12
SLIDE 12

Where do objects come from? (How can we define our own “things” for the machine?)

slide-13
SLIDE 13

Objects are distinct “instances” of a “class” of things. Each one of you is an instance of homo-sapiens.

a class of mammals

slide-14
SLIDE 14

Classes

  • “Define” objects
  • Declare the “rules” objects must follow
slide-15
SLIDE 15

Class v. Object

class Homo-Sapien int age; string name; void walk(); void burp(); DavidHasselhoff age: 61 name: “David Hasselhoff”

walk()

(he moves with confident gait)

burp()

“uuurrrrrpp!”

RachelRay age: 42 name: “Rachel Ray”

walk()

(she moves quickly)

burp()

“rrrruuuuuuuhhp!”

slide-16
SLIDE 16

The class just defines what values its objects can have, and what functions/behaviors its objects have.

String Class v. String Object

In general, we work with objects only (to get stuff done)... ... but the computer uses classes (to give us our objects).

slide-17
SLIDE 17

Say 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

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-19
SLIDE 19

Table Class

class Table { public: int height, width, length; double price; };

“Computer, a Table is a thing.” “It has a height, width and length.” “It has a price.”

Prototype or Interface in table.h

Note the semicolon. Very important

slide-20
SLIDE 20

So You Want a Table

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

slide-21
SLIDE 21

Constructors

  • A special function
  • Named after the class name
  • Called automatically upon instantiation
  • Has no return type!
slide-22
SLIDE 22

Using Constructors

int main() { Book b; Book myBook(“Love Stinks”, 2); // ... return 0; }

Instantiate a new Book object for me and pass these values to the constructor function. Instantiate a new Book object for me in the default manner.

slide-23
SLIDE 23

Declaring Constructors

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

Table Class

class Table { public: int height, width, length; double price; };

“Computer, a Table is a thing.” “It has a height, width and length.” “It has a price.”

Prototype or Interface in table.h

slide-25
SLIDE 25

Table Class

#include “Table.h” Table::Table() { height = 1; width = 1; depth = 1; } Table::Table(int h, int w, int d) { height = h; width = w; depth = d; }

“Computer, a Table is a thing that is described in Table.h.”

Implementation in table.cpp

“By default, construct a table like this.” “Here’s how to instantiate a table with a specific height, width and depth.”

slide-26
SLIDE 26

#include "box.h" int main() { Box b; cout << "Height is: " << toyBox.height << endl; cout << "Width is: " << toyBox.width << endl; cout << "Depth is: " << toyBox.depth << endl; Box toyBox(10, 10, 20); cout << "Height is: " << toyBox.height << endl; cout << "Width is: " << toyBox.width << endl; cout << "Depth is: " << toyBox.depth << endl; return 0; }

How can we teach the machine what a “Box” is?

slide-27
SLIDE 27

Homework

  • Read Etter 8.1 - 8.2 & 8.4 (skip 8.3)
  • Complete 26_boxTest