CSCI261
Lecture 21: Introduction to Classes
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
Lecture 21: Introduction to Classes
"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)
Table
Chair
Table Chair
cushion
Dining Set “has a”
Piece Of Furniture
Table Chair Bed Desk DiningTable NightStand “is a”
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
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!”
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).
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? (?)
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
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? (?)
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.
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
#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.”
#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?