Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.

Similar presentations


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

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

2 Lecture outline Announcements / reminders  Lab 3 due Monday, 10/01 Last time  Data members  Set/get functions Today  Constructors  Using classes 4/16/2015 ECE 264: Lecture 10 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 Discussed “set” & “get” (mutator/accessor) functions Data/functions can be public or private  Private members only accessible within member functions  Private functions also known as helper functions 4/16/2015 ECE 264: Lecture 10 3

4 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 courseName ); // function that gets the course name string getCourseName(); // function that displays a welcome message void displayMessage(); private: string name; // course name for this GradeBook }; // end class GradeBook 4/16/2015 ECE 264: Lecture 10 4

5 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 courseName ) { name = courseName; // store the course name in the object } // end function setCourseName // function that gets the course name string GradeBook::getCourseName() { return name; // 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" << name << "!" << endl; } // end function displayMessage 4/16/2015 ECE 264: Lecture 10 5

6 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 4/16/2015 ECE 264: Lecture 10 6 Initial course name is: Please enter the course name: ECE 264 Welcome to the grade book for ECE 264! Input/Output:

7 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 4/16/2015 ECE 264: Lecture 10 7

8 Example: constructors (GradeBook.h) // NOTE: See web for #includes // GradeBook class interface class GradeBook { public: GradeBook(); // Default constructor GradeBook(string courseName); // 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 4/16/2015 ECE 264: Lecture 10 8

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

10 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 4/16/2015 ECE 264: Lecture gradeBook1 created for course: CS 101 Introduction to C++ Programming gradeBook2 created for course: CS 102 Data Structures in C++ Output:

11 Examples: using classes Using GradeBook, which statements would be valid in a main() program written in the same file? #include using std::string; class GradeBook { public: GradeBook( ); GradeBook( string name ); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string name; }; // end class GradeBook 4/16/2015 ECE 264: Lecture a.GradeBook g1(264); b.GradeBook g2; c.setCourseName(g2); d.g2.name = “ECE 461”; e.string s = g2.getCourseName(); f.g2.displayMessage;

12 Examples: using classes a. GradeBook g1(264);  Invalid—constructor takes string as argument  Valid alternative: GradeBook g1(“264”); b. GradeBook g2;  Valid—creates new GradeBook object using default constructor c. setCourseName(g2);  Invalid—improper way to call member function  Valid alternative: g2.setCourseName(“ECE 264”); 4/16/2015 ECE 264: Lecture 10 12

13 Examples: using classes (cont.) d. g2.name = “ECE 461”;  Invalid— name is private data  Must use public “set” function to assign value e. string s = g2.getCourseName();  Valid—correct syntax for calling member function, and type for s matches return type for getCourseName(); f. g2.displayMessage;  Invalid— displayMessage is a function and therefore needs parentheses after the function name: g2.displayMessage(); 4/16/2015 ECE 264: Lecture 10 13

14 Example: Creating a class Say we have a class to represent a point in a 2-dimensional plane  What data should this class hold?  What should the constructor look like?  How would we write the mutator function(s)?  How would we write the accessor function(s)? 4/16/2015 ECE 264: Lecture 10 14

15 Final notes Next time Code examples: creating a class 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. 4/16/2015 ECE 264: Lecture 10 15


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

Similar presentations


Ads by Google