CMSC 202 Computer Science II, Fall 2015, Section 06
Lecture 7: Objects & Classes
Thu 09/17, 2015
Assigned Reading: 7.1-7.3
Slides As Shown:
- Intro to Classes.
- Demonstrate that private is not intended to be
secure in C++:
- The first example
shows that non-member functions cannot access private members.
Compiling this program is a syntax error.
- For the second example
we add a public constructor that initializes data members
and a dump() member function to print out data.
- In the third example,
we use a pointer to change the value of the public data member.
- In the fourth example,
we use change the pointer to point to private member
- In the fifth example,
we made both data members private, but we can still point
to the data members using the address of the object
and changing the type address.
- Most C++ programs have separate header files and
implementation files. Here,
has the declaration of the foo class.
The implemenation of the member functiosn are in
is compiled with
the effect is the same as with
Alternatively, we can use a modified version of the header file
which does not have private data members. Even though the
definition of class foo has been modified, it remains
compatible with the compiled implemenation of the member functions.
24 Sep 2015 07:42:13 EDT