Title: Module 12
1Module 12
2By the end of this lecture, you should be able to
- Distinguish classes from objects
- Know why a class hierarchy is useful
- Know what overloading is
- Know what constructors and destructors do
3Recall
- In the last module, we noted that there were
things hard to do in a procedural paradigm - maintain code
- support things like windows
- So why would supporting pop-up windows be so hard
to do?
4Object Oriented Programming Revisited
- Paradigm
- A way of thinking Assumptions Rules
- Procedural Languages
- Examples C, Pascal, Basic, Fortran
- A program is like a recipe in this paradigm
- OOP, the new paradigm
- The world is made up of objects and the
interaction between them - Objects can share behavior (code)
5So what is an Object?
- Notice that when we had the stream class, we
could build more than one stream, just like we
can build many ints - Each individual thing built from a class is
called an object or instance - Each one has its own data (its data members), and
each has access to the member functions of the
class
6Classes
- What's the relationship between a Class and an
Object? - A class is a template for creating objects
- Class Examples Their Objects
- Blue Prints Buildings
- DNA Humans
- Sewing patterns Shirt
- Cookie Cutter Chocolate Chip Cookie
7The Class Hierarchy
- Why have classes? Animals, mammals, and Giraffes
- We can use these to simplify programming
- Reuse code that others have written and tested
- Inherit features of complex programs
- But customize features we wish to change!
- Special names we use superclass, subclass,
parent class, child class
8Example of Inheritance
- You already know a little about file streams
- There is a file stream classfstream
- Two subclasses you have used ifstream, ofstream
- Each of these classes shares code inherited from
the fstream class - Well talk more about inheritance a bit later
lets focus on objects a bit more
9Building Objects With Constructors
- A constructor is any function with the same name
as the class name - If a constructor function is called, it returns a
new object of the class it belongs to - This new object includes
- memory allocated to hold its data members
- links to class functions
10Default Constructor
- If I forget to declare a constructor for my class
(as on the next slide), the compiler supplies a
default constructor - The default constructor takes no arguments, and
has an empty function body - The term default constructor refers to any
constructor which does not require arguments when
called - Slightly different from your intuition that it is
one I havent defined
11Example Class With Default Constructor
- What does this classs constructor look like?
- class Square // Declaration Section of Square
-
- private
- int sideLength, xPosition, yPosition
- public
- void setLength( )
- int getXPos( )
- int getYPos( )
12Using a Constructor
EXAMPLE 1 Square mySquare( ) RectangleShape
frontYard(42, 21,Yellow, 8, 10) ALTERNATE
SYNTAX // my personal preference Square
mySquare Square( ) RectangleShape frontYard
RectangleShape(42, 21,Yellow, 8,
10) DANGEROUS!! // AVOID this syntax Square
mySquare 5 // Uses constructor with one
int argument
13Overloaded Constructors
- We say a function is overloaded when there is
more than one function with the same name - The way the compiler tells apart the overloaded
functions is that each must have a unique return
type, number of arguments, or types of
argumentssometimes called the signature of the
function
14Overloading, Continued
- Some operators in C are overloaded, too
- Can you think of one? How can you prove to
someone that it is overloaded? - Note you can overload regular functions, too,
not just constructors
15Destructors
- A destructor function destroys an object
- why would we do that?
- why would the compiler do that?
- Destructor functions use the class name preceded
by a tilde () - EXAMPLE Square( )
- Limit of one destructor, and it cannot return a
value or take a parameter
16Example With Destructor
class BuildTest public BuildTest( )
cout ltlt default contructed ltlt
endl BuildTest( ) cout ltlt default
destructed ltlt endl private int x main(
) int x 5 BuildTest a int y
4 BuildTest b // what happens here?
// what happens here?