Title: Classes
1Classes
2Intro to Class
- What is a Class?
- A class defines a new type
- State plus Behavior (methods)
- Have we seen classes before?
- String Class
- What State did a string hold?
- Contents of the string
- What behaviors did it support?
- substr(), length(), find()
- An Object is an instance of a Class
- My string name is an object. It is an instance
of class string - Today we will define new types
3Example of an Object
- Class is Sprite
- Instance is Scratchy
- State includes position, direction, costumes,
sounds - Behaviors are kept in scripts
4Classes vs Structs
- Both Classes and Structs store state in fields
(month, day, year below) - Struct does not have methods
- class Date
-
- private
- int month
- int day
- int year
- public
- Date(int m, int d, int y) // Constructor
-
- month m
- day d
- year y
-
By default, everything in a class is private Use
the keyword public to mark methods you want to
use outside the class
Constructors are used to initialize objects
5Classes Date
- class Date
-
- int month, day, year
- public
- Date(int m, int d, int y) // Constructor
-
- month m
- day d
- year y
-
- void print()
-
- cout ltlt month ltlt '/' ltlt day ltlt '/' ltlt
year -
-
- int main()
-
- Date today(4, 2, 2008)
- today.print()
A constructor is a method used to initialize a
new instance of a class Name of constructor is
the same as class name Date in this case
Don't forget
6Instance methods
Instance methods act on an instance of the class.
Instance methods have one fewer parameter than
the equivalent function working on structs
compare print below
- class Date
-
- int month, day, year
- public
- ...
- void print()
-
- cout ltlt month ltlt '/' ltlt day ltlt '/' ltlt
year -
-
- int main()
-
- Date today Date(4, 2, 2008)
- today.print()
-
struct date int month int day int
year void printDate(const date d)
cout ltlt d.month ltlt '/' ltlt d.day ltlt '/' ltlt
d.year int main() date today ...
printDate(today)
7Classes Date
Instance method equal compares two Dates, but
only takes one parameter
- class Date
-
- int month, day, year
- public
- ...
- bool equal(Date other)
-
- if (month ! other.month) // If my
month ! your month... - ...
-
-
- int main()
-
- Date today Date(4, 2, 2008)
- Date yesterday Date(4, 1, 2008)
- today.print()
- yesterday.print()
- if (today.equal(yesterday))
- cout ltlt "Equal\n"
8Classes Date
Instance method greater compares two Dates, but
only takes one parameter By default, everything
in a class is private Use the keyword public to
mark methods you want to use outside the class
- class Date
-
- int month, day, year
- public
- ...
- bool greater(Date other)
-
- if (year gt other.year) // If my
year gt your year... - ...
-
-
- int main()
-
- Date today Date(4, 2, 2008)
- Date yesterday Date(4, 1, 2008)
- today.print()
- yesterday.print()
- if (today.greater(yesterday))
- cout ltlt "Equal\n"
9Class fraction
- class fraction // Defined in KFC
-
- private
- // Data members (attributes)
- int num // private data field
- int denom // private data field
- public
- // Member functions
- // Constructors
- fraction(int n, int d)
-
- num n
- denom d
-
10Class fraction
- // Multiply fractions
- fraction multiply(const fraction f)
-
- fraction temp(num f.num, denom
f.denom) - return temp
-
- // Add fractions
- fraction add(const fraction f)
-
- fraction temp(num f.denom f.num
denom, - denom f.denom)
- return temp
-
- ...
- int main()
-
- fraction first(1, 2) // We are calling the
Constructor - fraction second(2, 3)
11Including a header file
- The textbook introduces a class Fraction
- In their example, they use an Include file
- // File Fraction.cpp
- // Fraction class implementation
- include "C\CPlus ver 4\sourcecodefromAW\fraction
.h" - include ltiostreamgt
- using namespace std
- // Member functions
- // Constructors
- fractionfraction()
-
- num 0
- denom 0
12Header file
This is a common idiom we are using the
preprocessor to protect ourselves from multiple
inclusions of a header file You will see this
idiom in most professional header files
- ifndef FRACTION_H
- define FRACTION_H
- class fraction
-
- public
- // Member functions
- // Constructors
- fraction()
- fraction(int)
- fraction(int, int)
- // Set numerator and denominator
- void setNum(int)
- void setDenom(int)
- ...
-
- endif // FRACTION_H
13Header file
The header file defines the interface of a
class Other programs can include the header file,
much as we include ltstringgt or ltiomanipgt Allows
the compiler to check usage
- class fraction
-
- public
- // Constructors
- fraction()
- fraction(int)
- fraction(int, int)
- fraction multiply(const fraction f)
- fraction divide(const fraction f)
- fraction add(const fraction f)
- fraction subtract(const fraction f)
- void readFrac()
- void displayFrac() const
- int getNum() const
- int getDenom() const
- ...
- private // Data members (attributes)
14Classes
- The promise of Object Oriented Programming is
that we can define common behavior once, and
inherit that behavior in subclasses