CSCI261E/F
Lecture 19: Classes & Objects November 1, 2010
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
Lecture 19: Classes & Objects November 1, 2010
int main() { int height = 3; int width = 6; int depth = 2; string material = “wood”; mytable = new Table(height, width, material); cout << table.width << endl; cout << table.height << endl; cout << table.material << endl; return 0; }
Instantiate a new Table object for me Call it mytable. A table object has properties. You access object properties with the ‘dot’ operator.
string name = clown.name; int age = clown.age; clown.name = “Homie”; clown.juggle(); clown.tell_joke(joke); access assignment functions
string word = “revolution”; word.length();
(A function that belongs to an object.) (aka method)
class Table { private: const int MAX_THINGS = 10; string on_the_table[MAX_THINGS] = {“”}; public: int height, width, length; double price; string material_name; bool support(string item_name); };
“Computer, a Table is a thing.” “It has a height, width and length.” “It has a price.” “It is made of some kind of material.” “It can support things (by name).”
#include “Table.h” Table::support(string item_name) { for (int i = 0; i < MAX_THINGS; ++i) { if on_the_table[i] == “” {
return true; } } return false; }
“Computer, a Table is a thing that is described in Table.h.” “I said it can support things, and here’s how.”
int main() { int height(3); int width(6); int depth(2); string material(“wood”); Table mytable(height, width, material); // ... return 0; }
Instantiate a new Table object for me and pass these values to the constructor function. Instantiate a new string object for me with the value “wood”