Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13COSC 181 – Foundations of Computer ProgrammingClass 8Initializing Classes with “Constructors”Figure 3.5PreviouslycourseName was set to empty when we instantiated the object GradeBook myGradeBook;cout << “course name is:” << myGradeBook.getCourseName() << endl;Now – Figure 3.7Instead we are going to useGradeBook myGradeBook(“COSC 181: C++”);How do we do this?Fig. 3.7 (lines 16 – 17)public:GradeBook (string name){setCourseName (name);} …Created a new special member functionHas the same name as the classCalled a constructorCalled when an object of the class is instantiatedWhat you didn’t knowALL classes with no other constructors have constructors called “default constructors”Classes can have either implicitly or explicitly defined default constructorsDefault constructors are those that take no parametersClasses with defined constructors will not be implicitly given default constructorsUpdated Class DiagramNow with Constructor<<constructor>> + GradeBook (name : String)Placing a class in a separate fileSo far in the same .cpp source file: class ClassName {//private: public: class functions and attributes }; int main() {// different variables and calls, etc. system(“PAUSE”); return 0; }The class declaration and the main function don’t have to be in the same source file (.cpp file)Using “header” filesInstead, place the class definition(s) in a separate fileclass ClassName {//private: public: class functions and attributes };Place the entire class definition in a file and save it as GradeBook.hDo not compile or attempt to run GradeBook.hHas no main() functionInstead, header files must be included in .cpp filesSee Fig 3.9 and Fig 3.10Note, figure 3.9 is saved as a header fileIt has a .h file extensionFigure 3.10 is saved as a .cpp file.It is the file that is going to be compiledIt has the main() functionLine 7 of Fig 3.10#include “GradeBook.h”This allows the instantiations that happen in lines 13 and 14Seperating Interface from FunctionalityFurther divide the code into 3 filesGradeBook.h fileGradeBook.cpp fileMainprogram.cpp fileWhy?A person working on the Mainprogram.cpp file will have to have access to GradeBook.hGradeBook.h has the implementation code for the classes methodsThis is generally not desirableMainprogram programmer only needs to know how to call the functionsFig. 3.11GradeBook.h fileGive function “declarations”Fig 3.12GradeBook.cppGives function “instantiations”Fig 3.13Fig03_13.cppMakes use of the classNote: Compilation must be handled differently with this approachHowever, we will not make immediate use of this method, though details about the method may appear on a test in limited formReview


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?