assn6 - MIT OpenCourseWare http:/ocw.mit.edu 6.096...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.096: Introduction to C++ IAP 2009 PROBLEM SET 6 Initializer syntax: Some parts of the initialization of a class should not be performed directly in code. In particular, when a derived class is instantiated (i.e. when its constructor is called), the constructors of all its base classes are also called, but these constructors may require arguments, and the constructors are called automatically even before the code in the derived constructor is executed. Similarly, any reference data members must be initialized as they are created; they cannot be reassigned to point to different data within the constructor. C++ provides the initializer syntax to allow custom initialization of constructors and data members: DerivedClass::DerivedClass() : BaseClass(someNumber) { } This allows calling the base class constructor with an argument. For initializing data members, the structure of this sort of statement is as follows: class_name::class_name(argument_list) : firstDataMemberToInitialize(initialValue1), secondDataMemberToInitialize(initialValue2) { } This initialization method – the colon after the constructor arguments, followed by a comma-separated list of initializations to perform – can be used for any base class constructor or data member. It can also be used to call one constructor from another. To give one more example of the use of this syntax, a colorable Square class that inherits from a black- and-white Rectangle class might want to define a constructor like this: Square::Square(int sideLength, int colorCode) : Rectangle(sideLength, sideLength), color(colorCode) { } Though some data members can be set directly with assignment statements within the constructor, base class constructors, data member constructors, references, and const data members must be initialized with this syntax. Problem 1 [15 points] a. Create a Time class that stores a time as a single number of type time_t (this is just another name for a certain type of integer). The constructor should take one argument – the initial value
Background image of page 2
to set the internally stored time to. This argument should have a default value of time(0) (the time function is defined in the C++ Standard Library header <ctime> , and the 0 indicates that it should use the current time). Also create a getter function for the time stored in Time objects, and a setter function to allow changing the time the Time object stores later on. b. Rewrite the constructor to use the member initializer syntax instead of an assignment statement. Problem 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/14/2011 for the course DDDD h0322 taught by Professor Whoknow during the Spring '11 term at Bina Nusantara University.

Page1 / 7

assn6 - MIT OpenCourseWare http:/ocw.mit.edu 6.096...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online