Lecture11 - CS213: C++ Programming Lecture #11: More On...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS213: C++ Programming Lecture #11: More On Classes + The I/O Library Chapter 7, Section 7.7-7.9, Chapter 8 What We Will Cover Today Agenda:- Constructors- Member Function Overloading- Pointers to Functions- I/O Streams Review of Classes Review of our Course class deFnition from last time: // Course.h -- Header file for Course class class Course { public: // These can be seen outside the class // Define member functions string getCourseName(); string getInstructor(); int getStudentCount(); void setCourseName(string theName); void setInstructor(string theInstructor); void setStudentCount(int theCount); private: // These can be seed inside the class only string name,instructor; int numStudents; Member Variable Initialization What would happen if we did the following? #include <iostream> #include Course.h using namespace std; int main(int argc, char *argv) { Course myCourse; cout << Student count is: << myCourse.getStudentCount() << endl; return 0; } DEMONSTRATION #1 First use of Course Member Variable Initialization Since we never called Course::setStudentCount(), the member variable count had an undefned (random) value stored in it. IF we add the call to Course::setStudentCount(), all is well... DEMONSTRATION #2 Making sure we initialize the count... Constructors That works, but its cumbersome to require the user of a given class to call an initialization function before using the class. Fortunately, C++ -- like Java -- has something called a constructor that helps us here. A constructor is a special member function in a class that is called every time a new instance of a class is created. A constructor is formed by dening a member function in a class that has the same name as the class. Constructors In our Course class, that would look like this: // Course.h -- Header file for Course class class Course { public: // These can be seen outside the class // Constructor Course() { numStudents = 0; // 0 is a good initial value } // Define member functions string getCourseName(); string getInstructor(); int getStudentCount(); void setCourseName(string theName); void setInstructor(string theInstructor); void setStudentCount(int theCount); Constructors Constructors have no return type. As such, they cannot return a value. A constructor that takes no argument is called a simple constructor....
View Full Document

This note was uploaded on 02/06/2008 for the course CS 2024 taught by Professor Dinapoli during the Fall '07 term at Cornell University (Engineering School).

Page1 / 36

Lecture11 - CS213: C++ Programming Lecture #11: More On...

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

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