csci261e f
play

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


  1. CSCI261E/F Lecture 19: Classes & Objects November 1, 2010

  2. ?

  3. Programs & Things • Variables (ints, doubles, chars, etc) • Data Structures (arrays) • Things (strings, ...what else?)

  4. Any Thing* * aka Object • Ape • Person • Yuppie • ColdHeartedExecutiveRobot • Cyborg

  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. Program w/ a ‘Table’ int main() { int height = 3; int width = 6; int depth = 2; string material = “wood”; Instantiate a new Table object for me mytable = new Table(height, width, material); Call it mytable . cout << table.width << endl; cout << table.height << endl; A table object has properties. cout << table.material << endl; You access object properties with the ‘dot’ operator. return 0; }

  11. Dot-This, Dot-That string name = clown.name; access int age = clown.age; assignment clown.name = “Homie”; clown.juggle(); functions clown.tell_joke(joke);

  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)

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

  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”

  15. Table Class Prototype or Interface in Table.h class Table { “Computer, a Table is a thing.” private: const int MAX_THINGS = 10; string on_the_table[MAX_THINGS] = {“”}; public: “It has a height, width and length.” int height, width, length; “It has a price.” double price; string material_name; “It is made of some kind of material.” bool support(string item_name); “It can support things (by name).” };

  16. Table Class Implementation in Table.cpp “Computer, a Table is a thing that is #include “Table.h” described in Table.h.” Table::support(string item_name) { for (int i = 0; i < MAX_THINGS; ++i) { “I said it can support things, and if on_the_table[i] == “” { here’s how.” on_the_table[i] = item_name; return true; } } return false; }

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

  18. Constructors • A special function • Named after the class name • Called automatically upon initialization

  19. Using Constructors int main() { int height(3); int width(6); Instantiate a new string object for me with int depth(2); string material(“wood”); the value “wood” Instantiate a new Table object for me and pass Table mytable(height, width, material); these values to the constructor function. // ... return 0; }

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

  21. Example SolidBeginningsClass

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

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