Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures 11/5/10. Reading  Read pp.219-226  Review of scope ch6/review.cpp ch6/review.cpp What is the output? What is the output?

Similar presentations


Presentation on theme: "Structures 11/5/10. Reading  Read pp.219-226  Review of scope ch6/review.cpp ch6/review.cpp What is the output? What is the output?"— Presentation transcript:

1 Structures 11/5/10

2 Reading  Read pp.219-226  Review of scope ch6/review.cpp ch6/review.cpp What is the output? What is the output?

3 Structures  Record Information pertaining to an entity Information pertaining to an entity  Structure The programmer defines new data type. The programmer defines new data type. Data type for a record composed of multiple components. Data type for a record composed of multiple components.

4 Structures  Example: Represent Date Represent Date Components: Month, Day and Year Components: Month, Day and Year In C++ the components are called data membersIn C++ the components are called data members ch6/sec6.1/struct.cpp ch6/sec6.1/struct.cpp

5 Structures  Format: struct Name { type component; type component;..};

6 Structures  Accessing Structure components  Use member access operator The period The period  e.g. holiday.mo = 11;  Another example in sec6.1/metal.cpp

7 Exercises  p. 222 #1, 2

8 Classes and Objects

9 Class  Model Real-World Objects  Take struct one step further  Have: data members data members member functions member functions

10 Object  Class is a type that you define as a programmer.  Object is a variable of that type.

11 Class and Object  example: ch6/sec6.2/temperature.cpp

12 Class Definition class Sample { public: fnc_prototype(); public: fnc_prototype(); fnc_prototype2(); fnc_prototype2(); //... etc. //... etc. member_var1; member_var1; member_var2; member_var2; // … etc. // … etc.};

13 Member Function Definition return_type class_name :: function_name(parmlist)‏ {statements}  :: is scope resolution operator

14 Calling a function Object_name.function_name(arg list); e.g. High.input_T( );

15 Scope of data members  Can use data members in any of the functions in the class.  There is no need to pass them to functions.

16 Example  Create a class for a phone number. Have a char that tells if it is work, 'w', home. 'h', or cell, 'c'. Have a char that tells if it is work, 'w', home. 'h', or cell, 'c'. Have the number and area code as separate ints. Have the number and area code as separate ints. Write member functions to: Write member functions to:  Input a phoneNumber  Output a phoneNumber.  Change a phoneNumber.

17 Encapsulation  Like case on my watch Protect from outside Protect from outside Have member functions control data Have member functions control data Like buttons on watchLike buttons on watch Protect data from outside world. Protect data from outside world. Like the internal workings of my watch are protected.Like the internal workings of my watch are protected. Don’t let the outside functions change the data.Don’t let the outside functions change the data.

18 Access Specifiers  Indicate accessibility of class members  public: Open access to outside functions Open access to outside functions Member functions are usually public. Member functions are usually public.  private: Access restricted to other members of class Access restricted to other members of class Member variables private Member variables private  Example: sec6.2/rectangle.cpp

19 Correction  rectangle1.cpp – has accessor functions.

20 Accessor Functions  Main program can't directly access private data members.  Create accessor function to allow access. Name usually begins with “get” Name usually begins with “get”  dog.cpp


Download ppt "Structures 11/5/10. Reading  Read pp.219-226  Review of scope ch6/review.cpp ch6/review.cpp What is the output? What is the output?"

Similar presentations


Ads by Google