CSCI-1200 Data Structures - Fall 2016 Lecture 3 - Classes I: Announcements
CSCI-1200 Data Structures - Fall 2016 Lecture 3 - Classes I: Announcements
Lecture 3 — Classes I
Announcements
• Please register your iClicker at: http://www1.iclicker.com/register-an-iclicker
Use your RCS ID (your email username), not your RIN.
• Questions about Homework 1?
Today’s Lecture
• Classes in C++ – Types and defining new types; Example: A Date class;
• Class declaration: member variables and member functions; Class scope;
• Using the class member functions; Member function implementation; Classes vs. structs; Designing classes
#include <iostream>
#include "date.h"
int main() {
std::cout << "Please enter today's date.\n"
<< "Provide the month, day and year: ";
int month, day, year;
std::cin >> month >> day >> year;
Date today(month, day, year);
Date Sallys_Birthday(9,29,1995);
if (sameDay(tomorrow, Sallys_Birthday)) {
std::cout << "Hey, tomorrow is Sally's birthday!\n";
}
std::cout << "The last day in this month is " << today.lastDayInMonth() << std::endl;
return 0;
}
• Important: Each object we create of type Date has its own distinct member variables.
• Calling class member functions for class objects uses the “dot” notation. For example, tomorrow.increment();
• Note: We don’t need to know the implementation details of the class member functions in order to understand
this example. This is an important feature of object oriented programming and class design.
3.5 Exercise
Add code to date_main.cpp to read in another date, check if it is a leap-year, and check if it is equal to tomorrow.
Output appropriate messages based on the results of the checks.
2
3.6 Class Declaration (date.h) & Implementation (date.cpp)
A class implementation usually consists of 2 files. First we’ll look at the header file date.h
// File: date.h
// Purpose: Header file with declaration of the Date class, including
// member functions and private member variables.
class Date {
public:
Date();
Date(int aMonth, int aDay, int aYear);
// ACCESSORS
int getDay() const;
int getMonth() const;
int getYear() const;
// MODIFIERS
void setDay(int aDay);
void setMonth(int aMonth);
void setYear(int aYear);
void increment();
// prototypes for other functions that operate on class objects are often
// included in the header file, but outside of the class declaration
bool sameDay(const Date &date1, const Date &date2); // same day & month?
And here is the other part of the class implementation, the implementation file date.cpp
// File: date.cpp
// Purpose: Implementation file for the Date class.
#include <iostream>
#include "date.h"
// array to figure out the number of days, it's used by the auxiliary function daysInMonth
const int DaysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date::Date(int aMonth, int aDay, int aYear) { // construct from month, day, & year
month = aMonth;
day = aDay;
year = aYear;
}
3
int Date::getDay() const {
return day;
}
void Date::setDay(int d) {
day = d;
}
void Date::setMonth(int m) {
month = m;
}
void Date::setYear(int y) {
year = y;
}
void Date::increment() {
if (!isLastDayInMonth()) {
day++;
} else {
day = 1;
if (month == 12) { // December
month = 1;
year++;
} else {
month++;
}
}
}
4
3.7 Class scope notation
• Date:: indicates that what follows is within the scope of the class.
• Within class scope, the member functions and member variables are accessible without the name of the object.
3.8 Constructors
These are special functions that initialize the values of the member variables. You have already used constructors
for string and vector objects.
• The syntax of the call to the constructor mixes variable definitions and function calls. (See date main.cpp)
• “Default constructors” have no arguments.
• Multiple constructors are allowed, just like multiple functions with the same name are allowed. The compiler
determines which one to call based on the types of the arguments (just like any other function call).
• When a new object is created, EXACTLY one constructor for the object is called.
5
3.11 Constant member functions
Member functions that do not change the member variables should be declared const
• For example: bool Date::isEqual(const Date &date2) const;
• This must appear consistently in both the member function declaration in the class declaration (in the .h file)
and in the member function definition (in the .cpp file).
• const objects (usually passed into a function as parameters) can ONLY use const member functions. Remember,
you should only pass objects by value under special circumstances. In general, pass all objects by reference so
they aren’t copied, and by const reference if you don’t want/need them to change.
• While you are learning, you will probably make mistakes in determining which member functions should or
should not be const. Be prepared for compile warnings & errors, and read them carefully.
3.12 Exercise
Add a member function to the Date class to add a given number of days to the Date object. The number should be
the only argument and it should be an unsigned int. Should this function be const?
Rule for the duration of the Data Structures course: You may not declare new struct types, and class member
variables should not be made public. This rule will ensure you get plenty of practice writing C++ classes with good
programming style.
3.17 Exercise
What happens if the user inputs 2 30 2012 into the program? How would you modify the Date class to make sure
illegal dates are not created?