{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

c_-_05 - Chapter 9 Objects and Classes(II Objectives To...

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

View Full Document Right Arrow Icon
Chapter 9 Objects and Classes (II)
Background image of page 1

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

View Full Document Right Arrow Icon
Objectives To declare private data fields with appropriate get and set functions for data field encapsulation to make classes easy to maintain (§9.9). To understand the scope of data fields (§9.10). To reference hidden data field using the this pointer (§9.11). To develop functions with object arguments (§9.12). To store and process objects in arrays (§9.13). To apply class abstraction to develop software (§§9.14-9.15). To initialize data fields with a constructor initializer (§9.16)
Background image of page 2
Data Field Encapsulation class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); double m_dRadius; }; void main() { CCircle circle1(5); circle1.m_dRadius = 1; } Now, I want to change the m_dRadius of circle1 to 1
Background image of page 3

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

View Full Document Right Arrow Icon
Data Field Encapsulation class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); private : double m_dRadius; }; void main() { CCircle circle1(5); circle1.m_dRadius = 1; } Now, I want to change the m_dRadius of circle1 to 1
Background image of page 4
Data Field Encapsulation class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); void setRadius(double dRadius); double getRadius(); private : double m_dRadius; }; void CCircle::setRadius(double dRadius) { m_dRadius = dRadius; } double CCirce::getRadius() { return m_dRadius; } the set function setter accessor the get function getter mutator
Background image of page 5

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

View Full Document Right Arrow Icon
Data Field Encapsulation void main() { CCircle circle1(5); circle1.setRadius(1); } Now, I want to change the m_dRadius of circle1 to 1 class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); void setRadius(double dRadius); double getRadius(); private : double m_dRadius; };
Background image of page 6
Data Field Encapsulation class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); void setRadius(double dRadius); double getRadius(); private : double m_dRadius; }; class CCircle { public : CCircle(); CCircle(double dRadius); double m_dRadius; double getArea(); }; Which one is better? Why?
Background image of page 7

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

View Full Document Right Arrow Icon
Data Field Encapsulation void main() { CCircle circle1(10); circle1.setRadius(-1); } void main() { CCircle circle1(10); circle1.m_dRadius = -1; }
Background image of page 8
Data Field Encapsulation class CCircle { public : CCircle(); CCircle(double dRadius); double getArea(); void setRadius(double dRadius); double getRadius(); private : double m_dRadius; }; void CCircle::setRadius(double dRadius) { if (dRadius >=0) { m_dRadius = dRadius; } } double CCircle::getRadius() { return m_dRadius; }
Background image of page 9

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

View Full Document Right Arrow Icon
Data Field Encapsulation void main() { CCircle circle1(10); cout <<circle1.getRadius() << endl; cout << circle1.getArea()<<endl; circle1.setRadius(-1); cout <<circle1.getRadius() << endl; cout << circle1.getArea()<<endl; } void main() { CCircle circle1(10); cout << circle1.m_dRadius <<endl; cout << circle1.getArea()<<endl; circle1.m_dRadius = -1; cout << circle1.m_dRadius <<endl; cout << circle1.getArea()<<endl; }
Background image of page 10
UML Class name Data fields Functions CCircle -radius: double +CCircle() +CCircle(dRadius: double) +getArea(): double +getRadius(): double +setRadius(dRadius: double)
Background image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}