Introduction to Classes
Gaddis Ch. 13 CS 2308 :: Spring 2016 Molly O'Neil
Introduction to Classes Gaddis Ch. 13 CS 2308 :: Spring 2016 Molly - - PowerPoint PPT Presentation
Introduction to Classes Gaddis Ch. 13 CS 2308 :: Spring 2016 Molly O'Neil Procedural Programming Programming paradigm centered on the actions that take place in a program Data is stored in variables Perhaps using arrays and structs
Gaddis Ch. 13 CS 2308 :: Spring 2016 Molly O'Neil
2
functions that operate on that data structure
structures that no longer look similar?
3
4
addSong() playSong() removeSong()
songs
MP3 Player:
listOfSongs __songCount__ addSong(Song) playSong(title) removeSong(title)
Song:
title artist size lyrics __playCount__ playSong()
Song:
title artist size lyrics __playCount__ playSong()
MP3 Player
model, transmission, and mileage
have the same values for the attributes
5
accesses the object in the same way
6
prototype; doesn't need to know the details of the function body
7
8
methods) shared by all objects of the class
9
// create two string instances string cityName1 = "Austin", cityName2 = "San Marcos"; // use the string class's member functions int size = cityName1.length(); cityName2.append(", TX");
(.) operator
body can be defined elsewhere
10
11
class Time { private: int hour; int minute; bool isAM; void addHour(); public: // accessors int getHour() const; int getMinute() const; bool getIsAM() const; // mutators void setHour(int); void setMinute(int); void setAM(); void setPM(); string toString() const; void addMinute(); };
class (i.e., functions that are members of the class)
a class declaration
their definitions from outside the class
access to the hidden/private data
12
13
int getHour() const; int getMinute() const; int getIsAM() const; void addMinute();
variables via the dot operator, because they're private - must access through the public interface)
14
15
16
void Time::setHour(int hr) { if(hr < 1 || hr > 12) { // Error } hour = hr; }
Note that at first glance, hour above looks undeclared! It refers to the member variable of the Time class
17
// accessors int Time::getHour() const { return hour; } int Time::getMinute() const { return minute; } bool Time::getIsAM() const { return isAM; } // mutators void Time::setHour(int hr) { if(hr < 1 || hr > 12) { // Error } hour = hr; } void Time::setMinute(int min) { if(min < 0 || min > 59) { // Error } minute = min; } void Time::setAM() { isAM = true; } void Time::setPM() { isAM = false; }
(this code would be in Time.cpp)
18
// private member functions void Time::addHour() { if(hour == 12) { hour = 1; isAM = !isAM; } else hour++; } // public member functions void Time::addMinute() { if(minute == 59) { minute = 0; addHour(); // call to private member function } else minute++; }
19
string Time::toString() const { // returns time formatted to (H)H:MM (A/P)M
strstr.fill('0'); // sets the padding char for setw strstr << hour << ":" << setw(2) << minute; strstr << (isAM ? " AM" : " PM"); return strstr.str(); // returns the stream's string }
"outputting" to it with << (exactly as you would to cout), including I/O formatting must #include <sstream>
20
class Time { private: int hour; int minute; bool isAM; ... public: ... void addMinute(); // adds one more minute void addMinute(int n); // adds n more minutes };
Time.h
define a class member function inline (inside the class declaration, rather than outside the class)
21
class Time { private: int hour; int minute; // ... public: // accessors int getHour() const { return hour; } int getMinute() const { return minute; } // mutators void setHour(int hr) { hour = hr; } void setMinute(int min) { minute = min; } void addMinute(); // NOT INLINE // ... };
Time.h
22
Time t1;
t1.setHour(3); t1.setMinute(42); t1.setIsPM();
23
int main() { Time t; t.setHour(11); t.setMinute(58); t.setPM(); cout << t.toString() << endl; t.addMinute(); cout << t.toString() << endl; t.addMinute(); cout << t.toString() << endl; return 0; } 11:58 PM 11:59 PM 12:00 AM
Output:
24
Time t1; cout << t1.getHour();
class Time { public: Time(); // constructor prototype };
25
#include <sstream> #include <iomanip> #include "Time.h" using namespace std; Time::Time() { // initializes hour, minute, and isAM hour = 12; minute = 0; isAM = true; }
Note that the constructor function has no return type Name of constructor function is just the name of the class ( still need the ClassName:: before the definition)
Time.cpp
26
#include <iostream> #include "Time.h" using namespace std; int main() { Time t; // constructor called implicitly here cout << t.toString() << endl; t.addMinute(); cout << t.toString() << endl; } 12:00 AM 12:01 AM
Output:
27
class Time { private: ... public: Time(int, int, bool); // constructor prototype ... }; Time::Time(int hr, int min, bool am) { if(hr < 1 || hr > 12 || min < 0 || min > 59) { // Error } hour = hr; minute = min; isAM = am; }
Time.cpp Time.h
28
int main() { Time t(10, 59, false); cout << t.toString() << endl; } 10:59 PM 11:00 PM
Output:
class constructors as you like, as long as they all have unique parameter lists
29
Time::Time() { hour = 12; minute = 0; isAm = true; } Time::Time(int hr, int min, bool am) { if(hr < 1 || hr > 12 || min < 0 || min > 59) { // Error } hour = hr; minute = min; isAM = am; } Time::Time(int hr) { if(hr < 1 || hr > 12) // Error hour = hr; minute = 0; isAM = true; } class Time { private: ... public: Time(); Time(int, int, bool); Time(int); ... };
Time.h Time.cpp
30
int main() { Time t1; Time t2(5, 28, false); Time t3(2); cout << t1.toString() << endl; cout << t2.toString() << endl; cout << t3.toString() << endl; } 12:00 AM 5:28 PM 2:00 AM
Output:
the member variables
31
Time t1; Time t2(); // both use the default constructor
constructor in order to instantiate an object
member variables
32
class Time { public: Time(int, int, bool); // the only constructor }; int main() { Time t1(10, 0, true); // OK Time t2; // NOT OK - DOES NOT COMPILE }
33
34
struct Song { string title; string artist; double size; }; class MP3Player { private: Song *songs; // ptr for dynamically allocated array double maxMB; int maxSongs; int count; public: MP3Player(int, double); // no default constructor ~MP3Player(); bool addSong(Song); ... };
MP3Player.h
35
#include "MP3Player.h" MP3Player::MP3Player(int numSongs, double numMB) { songs = new Song[numSongs]; // dynamic allocation maxSongs = numSongs; maxMB = numMB; count = 0; } MP3Player::~MP3Player() { delete [] songs; }
MP3Player.cpp
36
#include <iostream> #include "MP3Player.h" using namespace std; int main() { MP3Player player(50, 25.0); // calls constructor // do stuff with player here return 0; // main goes out of scope, player object is } // destroyed here, its destructor is called // (which deletes the internal songs array)
37
int main() { Time recentCalls[10]; // times of last 10 calls }
38
int main() { Time recentCalls[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; }
initializer must take the form of a function call to the constructor:
39
int main() { Time recentCalls[5] = { Time(8, 5, true), Time(9, 12, true), Time(11, 21, true), Time(12, 1, false), Time(12, 57, false) }; }
40
int main() { Time recentCalls[7] = { 8, Time(9, 12, true), 11, Time(12, 1, false), Time(12, 57, false) }; Time recentDeliveries[5] = { Time(), Time(10), Time(10, 43, true), Time(), Time(12, 30, false) }; }
41
recentCalls[2].addMinute(); cout << recentCalls[4].toString() << endl;