classes II - EECE 230 Lecture Notes: S. Karaki 29 7....

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

View Full Document Right Arrow Icon
EECE 230 – Lecture Notes: S. Karaki 29 7. Classes: Part II 7.2 Const Objects and Member Functions Constant objects can only be accessed by constant member functions. A constant member function is not allowed to modify any member of an object; the compiler does not allow it. class date { public: int day, month, year; date (int= 1, int= 1, int= 2000); void printDate(); }; date::date (int d, int m, int y) { day= d; month= m; year= y; } void data::printDate() { cout << day << ’/’ << month << ‘/’ << year; } const date newYear; newYear.printDate(); // Error: constant object invoking a non-constant member // function. For the above to work properly the function printDate() should be declared as const in the prototype and function definition: void printDate() const; void data::printDate() const { cout << day << ’/’ << month << ‘/’ << year; } Syntax errors: Defining a const member function that modifies a data member of an object. Defining a const member function that calls a non-const member function. A const object invoking a non-const member function. Note: A const member function can be overloaded with a non-const version. The compiler decides which one to invoke depending whether the object is const or not. When class member variables are ‘const’ or ‘reference’ types, the constructor must be defined using a member initializer list, as illustrated in the following example:
Background image of page 1

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

View Full DocumentRight Arrow Icon
EECE 230 – Lecture Notes: S. Karaki 30 class series { public: int number; const int delta; series(int n= 0, int d= 1); void increment(); void print() const; }; series::series (int n, int d): delta(d) { number= n; } void series::print() const { cout << “number= “ << number << “, delta= “ << delta << endl; } void series::increment() { number+= delta; } Note that all variables may be initialized using the member initialization list as follows: series::series (int n, int d): delta(d), number(n) { } 7.3 Composition: Objects as Members of Classes A class can have objects of other classes as members. In the following example the class box has as base a rectangle object: #include <iostream> using std::cout; using std::endl; class rectangle { public: int width, length; int area( ); rectangle (int, int); rectangle( ){ } }; rectangle:: rectangle (int a, int b) { width = a; length = b;} int rectangle:: area( ) { return width* length;} class box { public: rectangle base; int height; int volume( ); box(rectangle, int); };
Background image of page 2
EECE 230 – Lecture Notes: S. Karaki 31 box:: box(rectangle r, int s) { base = r; height = s;} int box:: volume( ) { return base. area( )* height;}
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 02/26/2010 for the course EECE 230 taught by Professor Samikaraki during the Spring '07 term at American University of Beirut.

Page1 / 8

classes II - EECE 230 Lecture Notes: S. Karaki 29 7....

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