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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Chapter 9 Objects and Classes (II) 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) 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 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 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 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; }; 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? Data Field Encapsulation void main() { CCircle circle1(10); … … circle1.setRadius(-1); } void main() { CCircle circle1(10); … … circle1.m_dRadius = -1; } 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; } 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; } UML Class name Data fields Functions CCircle-radius: double...
View Full 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 / 41

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

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

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