Sie sind auf Seite 1von 15

COP330

Object Oriented
Programming in C++
Fall 2008

Dr. David A. Gaitros


dgaitros@admin.fsu.e
du
Introduction to Object
Oriented Programming
(OOP)
OOP combines the data and
functions that manipulate the
data into a single package
called objects.
Data The objects attributes
Functions The objects behavior
The Packages have the
favorable Software Engineering
property of Information Hiding
Objects know how to
communicate with each other via
well-defined interfaces
Implementation details are
hidden
Introduction to Object
Oriented Programming
(OOP)
C++ is a language that
allows you to program using
the OOP style.
You can program in C++
using non-OOP styles.
In OOP the unit of
programming is called a class.
An object is an item that is an
instantiation (actually define a
variable ) of a class.
Remember that a class has
both data and functions
combined.
Introduction to Object
Oriented Programming
(OOP)
C++ Classes
A programmer creates their own
user-defined types called classes.
Classes do not set aside storage or
define variables. They only tell the
compiler what a class looks like.
These are also called programmer-
defined types.
Each class contains data and
functions to manipulate the data
Data components of a class are
called data members.
Function are called member
functions.
An instance of a user-defined
type is called an object.
Introduction to Object
Oriented Programming
(OOP)
Remember that the C
language is a proper subset
of the C++ language. So you
can take a C program and
give it to a C++ compiler and
it will compile fine.
You can actually do OOP style
in the C language but it is
more difficult because of the
lack of definition of a class.
We use C struct concepts.
Introduction to Object
Oriented Programming
(OOP)
C Struct

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);

// The first one uses the


// constructor with no parameters.
// We assume the code will assign
// todays date.
// The second uses the constructor with
// three parameters to set the actual
// hour, minute, and second.

Das könnte Ihnen auch gefallen