csci261
play

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


  1. CSCI261 Lecture 21: Introduction to Classes

  2. ?

  3. Object-Oriented Programming (OOP)

  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)

  5. What is a Thing?

  6. Properties Chair Table • Height • Height • Width • Width • Depth • Depth • Material Type • Material Type • Style • Style • Price • Price • Location • Location

  7. Behavior Chair Table • Support ____ • Support ____ • cup • person • candle • bag • person • whoopee cushion • etc • etc

  8. Composition “has a” Dining Set • Consists of ____ • Table • Chair • Chair • Chair

  9. Inheritance “is a” Desk Table Piece Of Furniture • Height DiningTable Chair • Width NightStand Bed • Depth • Material Type

  10. Objects • Behavior • Data • Abstraction

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

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

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

  14. Classes • “Define” objects • Declare the “rules” objects must follow

  15. Class v. Object class Homo-Sapien DavidHasselhoff int age; age: 61 string name; name: “David Hasselhoff” void walk(); void burp(); walk() burp() (he moves with confident gait) “uuurrrrrpp!” RachelRay age: 42 name: “Rachel Ray” walk() burp() “rrrruuuuuuuhhp!” (she moves quickly)

  16. String Class v. String Object The class just defines what values its objects can have, and what functions/behaviors its objects have. In general, we work with objects only (to get stuff done)... ... but the computer uses classes (to give us our objects).

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

  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”

  19. Table Class Prototype or Interface in table.h class Table { “Computer, a Table is a thing.” public: int height, width, length; “It has a height, width and length.” double price; “It has a price.” }; Note the semicolon. Very important

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

  21. Constructors • A special function • Named after the class name • Called automatically upon instantiation • Has no return type!

  22. Using Constructors int main() { Instantiate a new Book object for me in the Book b; default manner. Instantiate a new Book object for me and pass Book myBook(“Love Stinks”, 2); these values to the constructor function. // ... return 0; }

  23. Declaring Constructors • Declared in implementation file, with other functions • Must be named after the class • So the machine knows it’s a constructor

  24. Table Class Prototype or Interface in table.h class Table { “Computer, a Table is a thing.” public: int height, width, length; “It has a height, width and length.” double price; “It has a price.” };

  25. Table Class Implementation in table.cpp #include “Table.h” “Computer, a Table is a thing that is described in Table.h.” Table::Table() { “By default, construct a table like height = 1; this.” width = 1; depth = 1; } “Here’s how to instantiate a table Table::Table(int h, int w, int d) { with a specific height, width and height = h; depth.” width = w; depth = d; }

  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?

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend