Structured Programming
Using C++
Lecture 13 : Classes & ObjectsDr. Amal Khalifa
Lecture Contents:
2
Classes
Defining, member functions Public and private members Accessor and mutator functions Structures vs. classes
Classes, Pointers, Dynamic Arrays
The this pointer
Destructors, copy constructors
Classes
Similar to structures
Adds member FUNCTIONS
Not just member data
Integral to object-oriented programming
Focus on objects
Object: Contains data and operations
In C++, variables of class type are objects
6-3
Encapsulation
Any data type includes
Data (range of data)
Operations (that can be performed on data)
Example:
int data type has:
Data: +-32,767
Operations: +,-,*,/,%, logical, etc.
Same with classes
Collection of data values together with set of basic
operations to be allowed for the values
Encapsulation
• The bundling of data and
procedures(functions) into a single unit(called class).
• Class is an example of
encapsulation, where various data elements and member functions are wrapped up together.
• used in object oriented programming
• Closely related to
information hiding.
•Abstract Data Types
Dr. Amal Khalifa, 2012 5
Class data Types
Dr. Amal Khalifa, 2012
6
Type
Class Definitions
Defined similar to structures
Example:
class DayOfYear name of new class type
{
public:
void output(); member function!
int month; int day; };
Notice only member function’s prototype
Function’s implementation is elsewhere
6-7
Declaring Objects
Declared same as all variables
Predefined types, structure types
Example:
DayOfYear today, birthday;
Declares two objects of class type DayOfYear
Objects include:
Data
Members month, day
Operations (member functions) output()
Objects & Classes
• Class == blueprint
• Object == concrete
building
• One class type many
objects
Dr. Amal Khalifa, 2012 9
Class Member Access
Members accessed same as structures Example:
today.month
today.day
And to access member function:
today.output(); Invokes member function
Class Member Functions
Must define or "implement" class member functions
Like other function definitions
Can be after main() definition
Must specify class:
void DayOfYear::output() {…}
:: is scope resolution operator
Instructs compiler "what class" member is from
Item before :: called type qualifier
6-11
Example: DayOfYear class
Dr. Amal Khalifa, 2012 12
Define a class that represents a date of a specific year as a combination of day, and month.
Example:
•Class type definition
• Data • functions
6-13
Example
• object declaration
•Member access
•Member function call
Example
•Member function’sdefinition :
• Refers to
member data of class with no qualifiers
• used for all
objects of the class
• Will refer to
"that object’s" data when invoked
6-15
Dot and Scope Resolution Operators
Used to specify "of what thing" they are members
Dot operator (.):
Specifies member of particular object
Scope resolution operator (::) :
Specifies what class the function
definition comes from
Public and Private Members
Data in class almost always designated private in
definition!
Upholds principles of OOP Hide data from user
Allow manipulation only via operations Which are member functions
Public items (usually member functions) are
"user-accessible"
6-17
Private & public
Dr. Amal Khalifa, 2012
Public and Private Example
• Data now private
• Objects have no
direct access
• Object today can
ONLY access public members 6-19 class DayOfYear { public: void input(); void output(); private: int month; int day; }; Void main() { DayOfYear today;
cin >> today.month; // NOT ALLOWED! cout << today.day; // NOT ALLOWED!
//public members OK
today.input(); today.output(); }
Public and Private Style
Can mix & match public & private
More typically place public first
Allows easy viewing of portions that can be USED by
programmers using the class
Private data is "hidden", so irrelevant to users
Outside of class definition, cannot change (or even
access) private data
Accessor and Mutator Functions
•Object needs to "do
something" with its data
•Call accessor member
functions
• Allow object to read data
• Also called "get
member functions"
• Simple retrieval of member data
•Mutator member
functions
• Allow object to change
data
• Manipulated based on application
6-21
Example: set/get functions
Dr. Amal Khalifa, 2012 22
Define more member functions on the DayOfYear class: setDay : replace the object’s day information with its argument value.
setMonth : replace the object’s month information with its argument
A Class’s Place
Class is full-fledged type!
Just like data types int, double, etc.
Can have variables of a class type
We simply call them "objects"
Can have parameters of a class type
Pass-by-value
Pass-by-reference
Can use class type like any other type!
6-23
Example: More member functions
Dr. Amal Khalifa, 2012 24
Define more member functions on the DayOfYear class: IncrementDay : update the object’s data by advancing the date one day ahead.
IncrementMonth : update the object’s data by advancing the date one month ahead.
AddDays: update the object’s data by advancing the date by a number of days.
CopyFrom: copy the data members of a given object to “this” object
and Implementation
User of class need not see details of how class is
implemented
Principle of OOP encapsulation
User only needs "rules"
Called "interface" for the class In C++ public member functions and
associated comments
Implementation of class hidden
Member function definitions elsewhere User need not see them
6-25
Example: set/get functions
Dr. Amal Khalifa, 2012 26
Change the definition of the DayOfYear class to include the following:
year data member
setYear : replace the object’s year information with its argument
Structures versus Classes
Structures
Typically all members public No member functions
Classes
Typically all data members private Interface member functions public
Technically, same
Perceptionally, very different mechanisms
6-27
Thinking Objects
Focus for programming changes
Before algorithms center stage
OOP data is focus
Algorithms still exist
They simply focus on their data
Are "made" to "fit" the data
Designing software solution
Define variety of objects and how they interact
Back to pointers !!
The -> operator
Shorthand notation
Combines dereference operator, *, and dot
operator
Specifies member of class "pointed to” by given
pointer
Example:
MyClass *p;
p = new MyClass;
p->grade = "A"; //Equivalent to: (*p).grade = "A";
10-29
The this Pointer
10-30
Member function definitions might need to refer to
calling object
Use predefined this pointer
Automatically points to calling object:
Class Simple {
public:
void showStuff() const; private:
int stuff; };
Two ways for member functions to access:
cout << stuff;
Array of Objects
Dr. Amal Khalifa, 2012
31
Example:
DayOfYear Days[10], *MyDays;
Days[0].input(); Days[0].output(); //dynamic..
MyDays = new DayOfYear[10]; MyDays[0].input();
MyDays[0].output();
Dr. Amal Khalifa, 2012 32
That’s all for today !!