Andy Wang Object Oriented Programming in C COP 3330 Object Encapsulation of data and functions that act upon that data An object consists of Name variable name Attributes member data that describe what the object ID: 573282
Download Presentation The PPT/PDF document "Classes and Objects" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Classes and Objects
Andy Wang
Object Oriented
Programming
in C
++
COP 3330Slide2
Object
Encapsulation of data and functions that act upon that data
An object consists of
Name (variable name)
Attributes (member data) that describe what the object
is
Behavior (member functions) that describes what the object
doesSlide3
Class
A blueprint for objects
A user-defined type, consists of
Declaration (typically in .h files)
Definition (typically in .
cpp
files)
An object is an instance of a class
Can create many objects from the same class
Can build many houses from the same blueprintSlide4
DDU
Design—Declare, Define, Use
A
declaration
gives an interface
A variable declaration specifies the type
A function declaration tells how to use it
N
ot how it works
A class declaration shows what an object will look like and what its available functions are
No implementation detailsSlide5
DDU Design—Declare, Define, Use
A
definition
consists of the implementation details
The user of the interface will not see this part
A function definition is the code that makes the function work
A class definition consists of definitions of its member functionsSlide6
DDU Design—Declare, Define, Use
The
use
of an item through its interface
The user of an program uses the graphical user interface, keyboard, and mouse
The user of a function is a programmer, who makes calls to the function (without knowing the implementation details)
The user of a class is a programmer, who uses the class by creating objects and calling available member functions of those objectsSlide7
Interface
What user sees
Not necessary the user of a program
Could be a programmer (user of a class)
Implementation details are hiddenSlide8
Protection Levels in a Class
Members of a class can
be public, private, etc.
Public
Can be accessed from inside or outside of the object
Is essentially the interface of the object (need to be simple)
The user is some other portion of code (other classes, functions, main program)
Want to provide functions that handle all necessary actions on the object Slide9
Protection Levels in a Class
Private
Can only be used by the object itself
Standard practice to protect member data of a class
Same for helper functions that do not need to be part of the interfaceSlide10
Reasons for Data Hiding
Simpler interface
Principle of least privilege (need-to-know)
More secure
Less chance of accidental or malicious misuse
E.g., front wheels of a car should turn in the same direction
Easier to change class implementation without affecting other modules that use itSlide11
Class Declaration Format
class <
className
> {
public:
// public member data and functions go here
private:
// private member data and functions go here
};
Remember this semicolon.Slide12
Example: class Circle
c
lass Circle {
public:
void
SetCenter
(double x, double y);
void
SetRadious
(double r);
void Draw();
private:
double
center_x
,
center_y
,
radious
;
};Slide13
Example TimeType
c
lass
TimeType
{
public:
void Set(
int
,
int
,
int
); // set the time
void Increment(); // increment by one sec
void Display(); // output the time
private:
int
hours, minutes, seconds;
};Slide14
Constructors
Special member function of class
Usually used to initialize the members of object
Has the same name as the class
Has no return typeSlide15
Example: class Circle
c
lass Circle {
public:
Circle(); // this is a constructor
Circle(double r); // this is also a constructor
void
SetCenter
(double x, double y);
void
SetRadious
(double r);
void Draw();
private:
double
center_x
,
center_y
,
radious
;
};Slide16
More on Constructors
A constructor is a member function
You can define anything you want
You do not
call
the constructor
function as a member function
It
is
automatically called
when you declare an object
Circle circ1;
Create an object named circ1
Runs the Circle() constructor functionSlide17
Example: Fraction Class
http://
www.cs.fsu.edu/~myers/cop3330/examples/frac
Directory content
frac.cpp // class definition
f
rac.h
// class declaration
m
ain.cpp // driver program to use the class
m
akefile
Slide18
frac.h
c
lass Fraction {
public:
Fraction(); // set numerator = 0, denominator = 1
Fraction(
int
n,
int
d = 1); // constructor with parameters
void Input(); // input a fraction from keyboard
void Show(); // display a fraction on screen
int
GetNumerator
();
int
GetDenominator
();
void
SetValue
(
int
n,
int
d); // set the fraction’s value
double Evaluate(); // return the decimal value
private:
int
numerator, denominator; // denominator != 0
};Slide19
frac.cpp
#include <
iostream
>
#include “
frac.h
”
using namespace
std
;
Fraction::Fraction() { // default constructor
numerator = 0; denominator = 1;
}
Fraction::Fraction(
int
n,
int
d) { // need error checking
numerator = n; denominator = d;
}Slide20
frac.cpp
v
oid Fraction::Input() { // need error checking
char
divSign
; // assume the use of ‘/’ during input
cin
>> numerator >>
divSign
>> denominator;
}
v
oid Fraction::Show() {
cout
<< numerator << ‘/’ << denominator;
}
i
nt
Fraction::
GetNumerator
() { return numerator; }
i
nt
Fraction::
GetDenominator
() { return denominator; }Slide21
frac.cpp
v
oid Fraction::
SetValue
(
int
n,
int
d) { // need error checking
numerator = n; denominator = d;
}
d
ouble Fraction::Evaluate() {
double n = numerator; // convert
int
to double
double d = denominator; // convert
int
to double
return (n/d);
}
What’s
(
int
) 1 / (
int
) 2?Slide22
main.cpp
#include <
iostream
>
#include “
frac.h
”
u
sing namespace
std
;
i
nt
main() {
Fraction f1, f2, f3(3,4), f4(6);
cout
<< “\n” The fraction f1 is “; f1.Show();
cout
<< “\n” The fraction
f2
is “;
f2.Show
();
cout
<< “\n” The fraction
f3
is “;
f3.Show
();
cout
<< “\n” The fraction
f4
is “;
f4.Show
();Slide23
main.cpp
cout
<< “\n Now enter first fraction: “; f1.Input();
cout
<< “\
nYou
entered “;, f1.Show();
cout
<< “\n Now enter second fraction: “; f2.Input();
cout
<< “\
nYou
entered “; f2.Show();
cout
<< “\n The value of fraction 1 is “ << f1.Evaluate()
<< ‘\n’;
cout
<< “\n The value of fraction
2
is “ <<
f2.Evaluate
()
<<
‘\n’;
cout
<< “Goodbye!\n”;
}Slide24
makefile
fraction_executable
:
frac.o
main.o
g++ -o
frac
frac.o
main.o
chmod
755
frac
frac.o
: frac.cpp
frac.h
g++ -c
frac.cpp
main.o
: main.cpp
frac.h
g++ -c main.cpp
clean:
rm
-f *.o
frac
Allow
frac
to be executed as a program.
Remember to use tabs to indent.
Type ‘make clean’ to clean up the temporary files.Slide25
Definitions vs. Declarations
f
rac.cpp defines member functions
void Fraction::Show() {
cout
<< numerator << ‘/’ denominator;
}
frac.h
declares this function
v
oid Show();Slide26
Syntax of Definitions
returnType
className
::
memberFunctionName
:: is called the
scope resolution operator
Specifying to which class a member function belongs
Example: void Fraction::Show()
In main.cpp, by using namespace
std
, we can type
cout
instead of
std
::
coutSlide27
Syntax of Using Member Functions
To create objects of type Fraction
Fraction f1, f2;
To call a member function, the syntax format is
objectName.memberFunctionName
Examples
f1.Show();
c
out
<< f2.Evaluate();Slide28
Constructor with Parameters
Constructor declarations
Fraction(); // default constructor
Fraction(
int
n,
int
d = 1);
// constructor with parametersSlide29
Constructor with Parameters
Default constructor
will always refer to a constructor with no parameters
Fraction f1, f2;
If a class has no constructor defined, a default constructor will be automatically created
If there are constructors, no default constructor will be generated automaticallySlide30
Constructor with Parameters
To use a constructor with parameters, just pass arguments when the object is declared
Fraction f1(2,3) passes 2 and 3 as parameters
Fraction f3(6) passes in the first value and uses the default value of 1 as the second parameter
int
x = 4, y = 8;
Fraction f2(x, y) passes in the values stored in x and ySlide31
Common Pitfalls
Fraction f1; // will call the default constructor
Fraction f2(); // compiler will treat it as a function
// declaration
f
1.Fraction(); // compiler error
f
1 = Fraction(3, 4); // a fraction of ¾ is created and
// copied to f1;Slide32
Error Checking
http://www.cs.fsu.edu/~
myers/cop3330/examples/frac2Slide33
Error Checking: frac.h
bool
SetValue
(
int
n,
int
d);Slide34
Error Checking: frac.cpp
Fraction::Fraction(
int
n,
int
d
) {
if
(
SetValue
(
n,d
) ==
false)
SetValue
(0,1
);
}
bool Fraction::
SetValue
(
int
n,
int
d
) {
if
(d == 0
) { return false }
numerator
= n;
denominator
= d;
return
true;
}Slide35
Error Checking: frac.cpp
void Fraction::Input
() {
char
divSign
;
do {
cin
>> numerator >>
divSign
>> denominator;
if (denominator == 0)
cout
<< "Illegal Fraction. Try again:
";
} while (denominator == 0);
}