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 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)
What is a Thing?
Properties Chair Table • Height • Height • Width • Width • Depth • Depth • Material Type • Material Type • Style • Style • Price • Price • Location • Location
Behavior Chair Table • Support ____ • Support ____ • cup • person • candle • bag • person • whoopee cushion • etc • etc
Composition “has a” Dining Set • Consists of ____ • Table • Chair • Chair • Chair
Inheritance “is a” Desk Table Piece Of Furniture • Height DiningTable Chair • Width NightStand Bed • Depth • Material Type
Objects • Behavior • Data • Abstraction
Person buddy(“John”, 23); Book odm(“One-Dimensional Man”, 200); buddy.readBook(odm);
Where do objects come from? (How can we define our own “things” for the machine?)
Objects are distinct “instances” of a “class” of things. Each one of you is an instance of homo-sapiens. a class of mammals
Classes • “Define” objects • Declare the “rules” objects must follow
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)
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).
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? (?)
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”
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
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? (?)
Constructors • A special function • Named after the class name • Called automatically upon instantiation • Has no return type!
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; }
Declaring Constructors • Declared in implementation file, with other functions • Must be named after the class • So the machine knows it’s a constructor
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.” };
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; }
#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?
Homework • Read Etter 8.1 - 8.2 & 8.4 (skip 8.3) • Complete 26_boxTest
Recommend
More recommend