{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture11 - CS213 C Programming Lecture#11 More On Classes...

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

View Full Document Right Arrow Icon
CS213: C++ Programming Lecture #11: More On Classes + The I/O Library Chapter 7, Section 7.7-7.9, Chapter 8
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
What We Will Cover Today Agenda: - Constructors - Member Function Overloading - Pointers to Functions - I/O Streams
Background image of page 2
Review of Classes Review of our Course class definition 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;
Background image of page 3

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

View Full Document Right Arrow Icon
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; }
Background image of page 4
DEMONSTRATION #1 First use of Course
Background image of page 5

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

View Full Document Right Arrow Icon
Member Variable Initialization Since we never called Course::setStudentCount(), the member variable count had an undefined (“random”) value stored in it. If we add the call to Course::setStudentCount(), all is well...
Background image of page 6
DEMONSTRATION #2 Making sure we initialize the count...
Background image of page 7

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

View Full Document Right Arrow Icon
Constructors That works, but it’s 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 defining a member function in a class that has the same name as the class.
Background image of page 8
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);
Background image of page 9

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

View Full Document Right Arrow Icon
Constructors Constructors have no return type. As such, they cannot return a value. A constructor that takes no argument is called a simple constructor.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}