Title: Constructors
1Constructors Destructors
- Controlling initialization destruction
2Operator Overloading
- Most operators in C can be overloaded
- Two ways
- Member function of user defined class
- Non-member "stand alone" function
- Any operator overloading has to involve a
user-defined class object - Has to be at least one operand that is not
built-in - Most operators are binary (i.e. the have a left
and a right-hand side), and the expression
3Operator Overloading
- x op y can be defined either
- Stand-alone function call
- operatorop(x,y)
- Member-function call for x
- x.operatorop(y)
- If define overloaded operator as member function,
it must be member function of left-hand-side
object
4Interface Implementation
- We want to limit access to data members
- Done with private keyword
- We want to separate interface from implementation
- Constructors automatically called when new object
created - Destructors automatically called when object
destroyed
5Creation
- Objects are created in a few ways
- double x
- Creates an object of type double
- C provides mechanism to control what happens
when user defined classes are created in
statement like above - Constructor
6Constructors
- Same name as class
- No return type (not even void!)
- Can be overloaded
- Same name, differ by arguments they take
- One with no arguments is default constructor
7Example
int main() ToD x, y return 0
class ToD private int h, m bool
PM public ToD() cout ltlt "ToD
object created!" ltlt endl
Whats on the screen when the program runs?
8Use?(besides cheesy messages)
- Initialization
- Sometimes objects need to have values or perform
some operation before they can be used
9Isolating Implementation
- User cant manipulate values
- User doesnt need to do anything to initialize
- User cant put object in invalid state
10Destructors
- Only one per class
- Class name with in front
- Doesnt take any arguments
- Controls what happens when object destroyed
- Called automatically
11Destructor Example
class Silly private string name
public Silly() cout ltlt"A silly object
is born!"ltlt endl Silly() cout
ltlt"Silly object "ltltnameltlt" dies!"ltlt endl
int main() Silly p if (1gt0) Silly
first first.name Tom" p new
Silly2 p0.name "John" p1.name
Sara" Silly last last.name Tom
Jr" delete p return 0
12Use?
- When is destructor useful?
- Executed when object destroyed
- Can do anything, but interesting when deallocate
memory - Want to delete items created using new to free up
memory
13Destructor Example
/ DEFINITION OF CLASS NODE / class Node
public int data Node next
Node(int val, Node p) data val
next p
ListList() while(head ! 0) Node
p head head head-gtnext delete p
14Letting the User Define
- What about letting the user choose initialization
values? - Example
- Point A, B, CA.x 2.4A.y -0.5B.x
0.0B.y 3.3C.x (A.x B.x)/2C.y (A.y
B.y)/2
15Wouldnt It Be Nice?
- Point A(2.4,-0.5), B(0.0,3.3)Point C((A.x
B.x)/2,(A.y B.y)/2) - Constructors make this possible
16To the point (class)
- Class Point public double x, y Point()
x y 0 Point(double a) x a y
0 Point(double a, double b) x a y
b
17Calling Constructors
- Called three ways
- Previously mentioned
- ex. Point p
- Using new Operator
- ex. Point p new Point(3.3,7.1)
- As function that returns object
- Point(3.3,7.1).magnitude()
Class Pointpublic double x, y
Point(double a, double b) x a y
b double magnitude() return
sqrt(xx yy)
18Default Arguments
- In any function prototype, can follow name of
parameter with default value
Class Point public double x, y
Point(double a 0, double b 0) x
a y b