Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes."— Presentation transcript:

1 ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes

2 Lecture outline Announcements/reminders  Lab 3 posted; due 10/1 Review: File I/O, classes intro Today  Data members  Constructors 12/17/2015 ECE 264: Lecture 9 2

3 Classes: Programmer Defined Types Classes allow programmer to define their own types  Objects: instances of a class Each class typically contains  Data members: attributes for each object Each object has own copy of data members  Member functions: Tasks specific to class Data/functions can be public or private  Private members only accessible within member functions  Private functions also known as helper functions 12/17/2015 ECE 264: Lecture 9 3

4 Data members Local variables  Variables declared in a function definition’s body Cannot be used outside of that function body Lost when function terminates Attributes  Exist throughout the life of the object  Represented as data members Each object maintains its own copy of data members  Functions that change data members are called mutator functions (or “set” functions)  Functions that return data members are called accessor functions (or “get” functions)  Good programming practice: keep data private Use mutators / accessors to set / get data Allows programmer to control data accesses 12/17/2015 ECE 264: Lecture 9 4

5 Example: data members (GradeBook.h) // Adapted from Fig. 3.5: fig03_05.cpp // NOTE: See web for #includes // GradeBook class interface class GradeBook { public: // function that sets the course name void setCourseName( string name ); // function that gets the course name string getCourseName(); // function that displays a welcome message void displayMessage(); private: string courseName; // course name for this GradeBook }; // end class GradeBook 12/17/2015 ECE 264: Lecture 9 5

6 Example: data members (GradeBook.cpp) // Adapted from Fig. 3.5: fig03_05.cpp // NOTE: See web for #includes // GradeBook class implementation #include “GradeBook.h” // function that sets the course name void GradeBook::setCourseName( string name ) { courseName = name; // store the course name in the object } // end function setCourseName // function that gets the course name string GradeBook::getCourseName() { return courseName; // return the object's courseName } // end function getCourseName // function that displays a welcome message void GradeBook::displayMessage() { cout << "Welcome to the grade book for\n" << courseName << "!" << endl; } // end function displayMessage 12/17/2015 ECE 264: Lecture 9 6

7 Example (cont.) // function main begins program execution int main() { string nameOfCourse; // string of characters to store the course name GradeBook myGradeBook; // create a GradeBook object named myGradeBook // display initial value of courseName cout << "Initial course name is: " << myGradeBook.getCourseName() << endl; // prompt for, input and set course name cout << "\nPlease enter the course name:" << endl; getline( cin, nameOfCourse ); // read a course name with blanks myGradeBook.setCourseName( nameOfCourse ); // set the course name cout << endl; // outputs a blank line myGradeBook.displayMessage(); // display message with new course name return 0; // indicate successful termination } // end main 12/17/2015 ECE 264: Lecture 9 7 Initial course name is: Please enter the course name: ECE 264 Welcome to the grade book for ECE 264! Input/Output:

8 Constructors  Functions used to initialize an object’s data when it is created Call made implicitly when object is created Must be defined with the same name as the class Cannot return values  Not even void  Default constructor has no parameters The compiler will provide one when a class does not explicitly include a constructor  Compiler’s default constructor only calls constructors of data members that are objects of classes 12/17/2015 ECE 264: Lecture 9 8

9 Example: constructors (GradeBook.h) // NOTE: See web for #includes // GradeBook class interface class GradeBook { public: GradeBook(); // Default constructor GradeBook(string name); // Parameterized constructor // function that sets the course name void setCourseName( string name ); // function that gets the course name string getCourseName(); // function that displays a welcome message void displayMessage(); private: string courseName; // course name for this GradeBook }; // end class GradeBook 12/17/2015 ECE 264: Lecture 9 9

10 Example: constructors (GradeBook.cpp) Add the following to GradeBook.cpp: // Default constructor GradeBook() { courseName = “”; } // Parameterized constructor GradeBook(string name) { courseName = name; } 12/17/2015 ECE 264: Lecture 9 10

11 Example (cont.) // function main begins program execution int main() { // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; return 0; // indicate successful termination } // end main 12/17/2015 ECE 264: Lecture 9 11 gradeBook1 created for course: CS 101 Introduction to C++ Programming gradeBook2 created for course: CS 102 Data Structures in C++ Output:

12 Final notes Next time Continue with classes Data members Constructors Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 12/17/2015 ECE 264: Lecture 9 12


Download ppt "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes."

Similar presentations


Ads by Google