Beruflich Dokumente
Kultur Dokumente
Object Oriented
Programming in C++
Fall 2008
Struct Time {
int hour;
int minute;
int second;
};
Time Todays_Time;
Time Time_Array[10];
Introduction to Object
Oriented Programming
(OOP)
So what is the difference:
In C, Data and functions are
always separate
In C++, you can build
objects that have both the
data and functions put
together.
C++ Libraries can contain
useful classes that store data
in useful and reusable ways.
Classes then become the
building blocks for more
complex programs.
Introduction to Object
Oriented Programming
(OOP)
There are three parts in a
C++ program related to
objects:
DDU
Declare: Define the types of
data that will be used, give
them names, declare the
functions and their interface.
Define: Put the details into
the class. Write the code and
the implementation details.
Use: The actual code that
uses the class.
Introduction to Object
Oriented Programming
(OOP)
Protected levels in a class:
Public: Can be accessed both
within and external to the
object.
Private: Can only be used by
the object itself.
The public section of a class
is usually called the interface.
It is what the programmers
sees.
Normally, All data is private
and only the bare essential
functions are made public.
Introduction to Object
Oriented Programming
(OOP)
Reasons for hiding
Reduces complexity much like
the detailed workings of an
automobile are hidden from the
average driver.
Principle of least privilege
(need-to-know)
More Secure. Less likely that
someone will alter or misuse
something.
Easier to change the
implementation without
affecting other modules that
use it.
Introduction to Object
Oriented Programming
(OOP)
class <className>
{
public:
// public data
// public functions
private:
// private data
// private functions
};
Introduction to Object
Oriented Programming
(OOP)
Example of a Definition of a
class TimeType
class TimeType
{
public: void Set(int, int, int)
void Increment();
void Display();
int gethours();
int getminutes();
int getseconds();
private:
int hours;
int minutes;
int seconds;
};
Introduction to Object Oriented
Programming (OOP)
Constructors
Is a special member function
visible to the user that has the
same name as the class.
It has no return type nor void
It is called when an object is
declared and is used to set up
the object initially. Only called
once during initialization.
You can have more than one
constructor but each must
have unique parameters.
Introduction to Object Oriented
Programming (OOP)
Example of a Definition of a class
TimeType with contstructor
class TimeType
{
public:
TimeType();
TimeType(int h; int m; int s);
void Set(int, int, int)
void Increment();
void Display();
int gethours();
int getminutes();
int getseconds();
private:
int hours;
int minutes;
int seconds;
};
Introduction to Object
Oriented Programming (OOP)
// Examples
TimeType TodaysTime;
TimeType Yesterday(12,24,26);