c_-_06 - Chapter 10 More on Objects and Classes Objectives...

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

View Full Document Right Arrow Icon
Chapter 10 More on Objects and Classes
Background image of page 1

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

View Full DocumentRight Arrow Icon
Objectives To create immutable objects from immutable classes (§10.2). To prevent multiple declarations using the #ifndef include guard directive (§10.3). To understand the difference between instance and static variables and functions (§10.4). To implement destructors to perform customized operations (§10.5). To create objects using copy constructors with initial data copied from another object of the same type (§10.6). To customize copy constructors to perform deep copy (§10.7).
Background image of page 2
Immutable Objects and Classes class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); void setRadius(double dRadius); double getRadius(); private : double m_dRadius; }; If the contents of an object cannot be changed (except through memberwise copy) once the object is created, the object is called an immutable object and its class is called an immutable class.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Immutable Objects and Classes TRUE or FALSE: A class with all private data fields and no mutators is immutable.
Background image of page 4
Immutable Objects and Classes class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); double *getRadius(); private : double m_dRadius; }; double *CCircle::getRadius() { return &m_dRadius; }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Immutable Objects and Classes void main() { CCircle circle1(50); cout << *(circle1.getRadius()) << endl; *(circle1.getRadius()) = 15; cout << *(circle1.getRadius()) << endl; }
Background image of page 6
Preventing Multiple Declarations class CDate { public: CDate(int year, int month, int day); int getYear(); void setYear(int year); private: int m_iYear; int m_iMonth; int m_iDay; }; #include "Date.h" class CPerson { public: CPerson(int id, int year, int month, int day); int getID(); CDate *getBirthDate(); private: int m_iID; CDate *m_pBirthDate; }; Date.h Person.h
Background image of page 7

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

View Full DocumentRight Arrow Icon
Preventing Multiple Declarations #include <iostream> #include "person.h" #include "Date.h" using namespace std; void main() { CPerson person(1234, 1970, 5, 3); CDate *pDate = person.getBirthDate(); pDate->setYear(2000); cout << person.getBirthDate()->getYear() << endl; } Main.cpp
Background image of page 8
Preventing Multiple Declarations #ifndef DATE_H #define DATE_H { public: CDate(int year, int month, int day); void setYear(int year); private: }; #endif #ifndef PERSON_H #define PERSON_H #include "Date.h" class CPerson { public: CPerson(int id, int year, int month, int day); int getID(); CDate *getBirthDate(); private: int m_iID; CDate *m_pBirthDate; }; #endif Date.h Person.h Inclusion guard
Background image of page 9

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

View Full DocumentRight Arrow Icon
Preventing Multiple Declarations #include <iostream> #include "person.h" #include "Date.h" using namespace std; void main() { CPerson person(1234, 1970, 5, 3); CDate *pDate = person.getBirthDate(); pDate->setYear(2000); cout << person.getBirthDate()->getYear() << endl; } Main.cpp
Background image of page 10
Instance and Static Members class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); void setRadius(double dRadius); double getRadius(); static int getNumberOfCircle(); private : double m_dRadius; static int m_iNumberOfCircle; };
Background image of page 11

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

View Full DocumentRight Arrow Icon
Instance and Static Members class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); void setRadius(double dRadius); double getRadius(); static int getNumberOfCircle(); private : double m_dRadius; static int m_iNumberOfCircle; }; int CCircle::m_iNumberOfCircle = 0; CCircle::CCircle() { m_dRadius = 10; m_iNumberOfCircle++; } CCircle::CCircle(double dRadius) {
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/12/2011 for the course ECON 101 taught by Professor Professor during the Spring '10 term at Cisco Junior College.

Page1 / 47

c_-_06 - Chapter 10 More on Objects and Classes Objectives...

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

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