03_classes_I - CSCI-1200 Data Structures Fall 2010 Lecture...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCI-1200 Data Structures — Fall 2010 Lecture 3 — Classes I Partial Review from Lecture 2 Vectors are dynamically-sized arrays Vectors, strings and other containers should be: passed by reference when they are to be changed, and passed by constant reference when they aren’t. If you forget the and pass by value, the object will be copied which is expensive for containers with lots of elements. Note: This is unlike arrays, which are not copied when passed by value . Vectors can “contain” any type of objects, including strings and other vectors. Today’s Lecture: C++ classes Types and defining new types A Date class. Class declaration: member variables and member functions Using the class member functions Class scope Member function implementation Classes vs. structs Designing classes 3.1 Types and Defining New Types What is a type? It is a structuring of memory plus a set of operations (functions) that can be applied to that structured memory. Examples: integers, doubles, strings, and vectors. In many cases, when we are using a class we don’t know how that memory is structured. Instead, what we really think about is the set of operations (functions) that can be applied. To clarify, let’s focus on strings and vectors. These are classes. We’ll outline what we know about them: The structure of memory within each class object The set of operations defined We are now ready to start defining our own new types using classes. 3.2 Example: A Date Class Many programs require information about dates. Information stored about the date includes the month, the day and the year. Operations on the date include recording it, printing it, asking if two dates are equal, flipping over to the next day (incrementing), etc.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3.3 C++ Classes A C++ class consists of a collection of member variables, usually private , and a collection of member functions, usually public , which operate on these variables. public member functions can be accessed directly from outside the class, private member functions and member variables can only be accessed indirectly, through public member functions. We will look at the example of the Date class declaration. 3.4 Using C++ classes We have been using C++ classes (from the standard library) already this semester, so studying how the Date class is used is straightforward: // Program: date_main.cpp // Purpose: Demonstrate use of the Date class. #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 tomorrow(today.getMonth(), today.getDay(), today.getYear()); tomorrow.increment(); std::cout << "Tomorow is "; tomorrow.print(); std::cout << std::endl; Date Sallys_Birthday(9,29,1995); if (sameDay(tomorrow, Sallys_Birthday)) { std::cout << "Hey, tomorrow is Sally’s birthday!\n";
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

03_classes_I - CSCI-1200 Data Structures Fall 2010 Lecture...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online