150 likes | 222 Views
Learn about classes, constructors, and objects in software development with examples and explanations. Study data members, member functions, and the role of constructors in initializing objects.
E N D
ECE 264Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes
Lecture outline • Announcements / reminders • Lab 3 due Monday, 10/01 • Last time • Data members • Set/get functions • Today • Constructors • Using classes ECE 264: Lecture 10
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 ECE 264: Lecture 10
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 ECE 264: Lecture 10
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 ECE 264: Lecture 10
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 Input/Output: Initial course name is: Please enter the course name: ECE 264 Welcome to the grade book for ECE 264! ECE 264: Lecture 10
Constructors • 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 ECE 264: Lecture 10
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 ECE 264: Lecture 10
Example: constructors (GradeBook.cpp) Add the following to GradeBook.cpp: // Default constructor GradeBook() { courseName = “”; } // Parameterized constructor GradeBook(string courseName) {name = courseName; } ECE 264: Lecture 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 Output: gradeBook1 created for course: CS 101 Introduction to C++ Programming gradeBook2 created for course: CS 102 Data Structures in C++ ECE 264: Lecture 10
Examples: using classes • Using GradeBook, which statements would be valid in a main()program written in the same file? #include <string> using std::string; class GradeBook { public: GradeBook( ); GradeBook( string name ); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string name; }; // end class GradeBook GradeBook g1(264); GradeBook g2; setCourseName(g2); g2.name = “ECE 461”; string s = g2.getCourseName(); g2.displayMessage; ECE 264: Lecture 10
Examples: using classes • GradeBook g1(264); • Invalid—constructor takes string as argument • Valid alternative: GradeBook g1(“264”); • GradeBook g2; • Valid—creates new GradeBook object using default constructor • setCourseName(g2); • Invalid—improper way to call member function • Valid alternative: g2.setCourseName(“ECE 264”); ECE 264: Lecture 10
Examples: using classes (cont.) • g2.name = “ECE 461”; • Invalid—name is private data • Must use public “set” function to assign value • string s = g2.getCourseName(); • Valid—correct syntax for calling member function, and type for s matches return type for getCourseName(); • g2.displayMessage; • Invalid—displayMessage is a function and therefore needs parentheses after the function name: g2.displayMessage(); ECE 264: Lecture 10
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)? ECE 264: Lecture 10
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, 8th ed. • Etter & Ingber, Engineering Problem Solving with C++, 2nd ed. ECE 264: Lecture 10