DOC PREVIEW
Yale CPSC 427 - Lecture 3
School name Yale University
Pages 20

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineHeader fileImplementation FileMain ProgramBuilding Your CodeOutline Header file Implementation File Main Program Building Your CodeCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 3September 9, 2010CPSC 427a 1/20Outline Header file Implementation File Main Program Building Your CodeHeader fileImplementation FileMain ProgramBuilding Your CodeCPSC 427a 2/20Outline Header file Implementation File Main Program Building Your CodeC++ versionSee code demo 02-InsertionSortCpp and following notes.CPSC 427a 3/20Outline Header file Implementation File Main Program Building Your CodedataPack.hpp#pragma onceA more efficient but non-standard replacement for include guards:#ifndef DATAPACK_H#define DATAPACK_H// rest of header#endifCPSC 427a 4/20Outline Header file Implementation File Main Program Building Your Codeclass DataPackclass DataPack {...};defines a new class named DataPack.By convention, class names are capitalized.Note the required semicolon following the closing brace.If omitted, here’s the error comment:../datapack.hpp:11: error: new types may not be defined in a return type../datapack.hpp:11: note: (perhaps a semicolon is missing after thedefinition of ’DataPack’)../datapack.cpp:12: error: two or more data types in declaration of’readData’CPSC 427a 5/20Outline Header file Implementation File Main Program Building Your CodeClass elementsIA class contains declarations for data members and functionmembers (or methods).Iint n; declares a data member of type int.Iint getN(){ return n; } is a complete member functiondefinition.Ivoid sortData(); declares a member function that must bedefined elsewhere.IBy convention, member names begin with lower case lettersand are written in camelCase.CPSC 427a 6/20Outline Header file Implementation File Main Program Building Your CodeInline functionsIMethods defined inside a class are inline (e.g., getN()).IInline functions are recompiled for every call.IInline avoids function call overhead but results in larger codesize.Iinline keyword makes following function definition inline.IInline functions must be defined in the header (.hpp) file.Why?CPSC 427a 7/20Outline Header file Implementation File Main Program Building Your CodeVisibilityIThe visibility of declared names can be controlled.Ipublic: declares that following names are visible outside ofthe class.Iprivate: restricts name visibility to this class.IPublic names define the interface to the class.IPrivate names are for internal use, like local names infunctions.CPSC 427a 8/20Outline Header file Implementation File Main Program Building Your CodeConstructorA constructor is a special kind of method.Automatically called whenever a new class instance is allocated.Job is to initialize the raw data storage of the instance to becomea valid representation of an initial data object.In dataPack example, store must point to storage of max bytes,n of which are currently in use.CPSC 427a 9/20Outline Header file Implementation File Main Program Building Your CodeConstructorDataPack(){n = 0;max = LENGTH;store = new BT[max]; cout << "Store allocated.\n";readData();}new does the job of malloc() in C.cout is name of standard output stream (like stdout in C).<< is output operator.readData() is private function to read data set from user.Design question: Is this a good idea?CPSC 427a 10/20Outline Header file Implementation File Main Program Building Your CodeDestructorA destructor is a special kind of method.Automatically called whenever a class instance about to bedeallocated.Job is to perform any final processing of the data object and toreturn any previously-allocated storage to the system.In dataPack example, the storage block pointed to by store mustbe deallocated.CPSC 427a 11/20Outline Header file Implementation File Main Program Building Your CodeDestructor~DataPack(){delete[] store;cout << "Store deallocated.\n";}Name of the destructor is class name prefixed with ~.delete does the job of free() in C.Empty square brackets [] are for deleting an array.CPSC 427a 12/20Outline Header file Implementation File Main Program Building Your CodedataPack.cppOrdinary (non-inline) functions are defined in a separateimplementation file.Function name must be prefixed with class name followed by :: toidentify which class’s member function is being defined.Example: DataPack::readData() is the member functionreadData() declared in class DataPack.CPSC 427a 13/20Outline Header file Implementation File Main Program Building Your CodeFile I/OC++ file I/O is described in Chapter 3 of textbook. Please read it.ifstream infile( filename ); creates and opens an inputstream infile.The Boolean expression !infile is true if the file failed to open.This works because of a built-in coercion from type ifstream totype bool. (More later on coercions.)readData() has access to the private parts of class dataPack andis responsible for maintaining their consistency.CPSC 427a 14/20Outline Header file Implementation File Main Program Building Your Codemain.cppAs usual, the header file is included in each file that needs it:#include "datapack.hpp"banner(); should be the first line of every program you write forthis course. It helps debugging and identifies your output.(Remember to modify tools.hpp with your name as explained inChapter 1 of textbook.)Similarly, bye(); should be the last line of your program beforethe return statement (if any).The real work is done by the statements DataPack theData; andtheData.sortData();. Everything else is just printout.CPSC 427a 15/20Outline Header file Implementation File Main Program Building Your CodeManual compiling and linkingOne-line versiong++ -o isort main.cpp datapack.cpp tools.cppSeparate compilationg++ -c -o main.o main.cppg++ -c -o datapack.o datapack.cppg++ -c -o tools.o tools.cppg++ -o isort main.o datapack.o tools.oCPSC 427a 16/20Outline Header file Implementation File Main Program Building Your CodeMakefilemake is a tool to automate the build process.It is controlled by a Makefile or makefile.A minimal example:OBJ = main.o datapack.o tools.oisort: $(OBJ)g++ -o isort $(OBJ)main.o: main.cpp datapack.hpp tools.hppdatapack.o: datapack.cpp datapack.hpp tools.hpptools.o: tools.cpp tools.hppNote: The g++ line must begin with a tab character.CPSC 427a 17/20Outline Header file Implementation File Main Program Building Your CodeIntegrated Development Environment (e.g., Eclipse)AdvantagesISupports notion of project — all files needed for anapplication.IProvides graphical


View Full Document

Yale CPSC 427 - Lecture 3

Download Lecture 3
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 Lecture 3 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 Lecture 3 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?