week 3: classes, constructors, destructors, new operator operator and function overloading

28
Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Upload: astro

Post on 24-Feb-2016

61 views

Category:

Documents


0 download

DESCRIPTION

Week 3: Classes, constructors, destructors, new operator Operator and function overloading . Previous Knowledge. What is a class and why they are useful Difference between class methods and instance methods Public vs private and accessors and mutators ( “getters” and “setters”) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Week 3: Classes, constructors, destructors, new operator

Operator and function overloading

Page 2: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Previous Knowledge

• What is a class and why they are useful• Difference between class methods and

instance methods• Public vs private and accessors and mutators

(“getters” and “setters”)• Function override (i.e. __str__ in python)• Function overload

Page 3: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Objectives

• Understand the syntax of:– Creating a class (template for type)– Providing default constructors and destructors– Adding other constructors– Accessing state variables using public accessors

and mutators– Follow flow of execution for instance method

invocation• Instantiating an instance of a class• Overload << operator to control printing

Page 4: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Classes

• Expanded concept of data structure• Creates new data type composed of other data

types• Combines data and operations on data• Cohesive structure that better models natural

relationships• Requires a different kind of source file

organization – Object oriented analysis and design

Page 5: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Initial class definition considerations

• What data/fields defines a specific instance?• What operations are naturally used in

conjunction with class?• Look over set of operators and consider if

each has a natural context– Can they be added?– What does it mean to be equal?– Is there a natural ordering (used for comparison)?– If I print this object, what would be output?

Page 6: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Example

• Point (2 dimensions)– Has x and y dimension <= data that defines a specific

point– Can define the inverse point <= operation on data– We can add one point to another <= Addition has

specific meaning in this context– Printing of a point should include value of x and y

Page 7: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point { double x, y;

public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

Page 8: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point { double x, y;

public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

class is keywordPoint is the name of the classclass definition is delimited by braces

Page 9: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point { double x, y;

public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

items defined after public keyword can be used by any instantiating code i.e. public for all to see

Page 10: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point { double x, y;

public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

classes have variables called attributesthink of class attributes as defining a structure

x y

Page 11: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point { double x, y;

public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

items defined before public or after private keyword are not visible to the public i.e. for private use only

Page 12: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point {private: double x, y; public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

items defined before public or after private keyword are not visible to the public i.e. for private use only

Page 13: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point { double x, y;

public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

Must initialize attributes to meaningful values** What happens if init is never called? (the only function that sets x and y)** Will it create a compile error?

Page 14: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point { double x, y;

public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

• Longer functions are defined outside of the class definition

• Classname:: denotes that the function is part of the class

• Must still have function prototype in class definition

Page 15: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Point classclass Point { double x, y;

public : void plus(Point c); //function prototype void print() { cout << "(" << x << "," << y << ")"; } void init(double u, double v) { x = u; y = v; }};

void Point::plus(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

• Since Point is a data type, it can be used as a parameter

• plus is a mutator (changes x and y)

Page 16: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Main functionint main() {

Point w1, w2;

w1.init(0, 0.5); w2.init(-0.5, 1.5);

cout << "\npoint w1 = "; w1.print(); cout << "\npoint w2 = "; w2.print();

w1.plus(w2); cout << "\npoint w1 after plus = "; w1.print(); cout<<endl;}

point w1 = (0,0.5)point w2 = (-0.5,1.5)point w1 after plus = (-0.5,2)

Page 17: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Main functionint main() {

//Declares/allocates w1 and w2 Point w1, w2; w1.init(0, 0.5); //sets the x and y value for w1 w2.init(-0.5, 1.5); //sets the x and y value for w2

cout << "\npoint w1 = "; w1.print(); //executes print with w1 data cout << "\npoint w2 = "; w2.print(); //executes print with w2 data

w1.plus(w2); //calls plus

cout << "\npoint w1 after plus = "; w1.print(); cout<<endl;}

junk junk

x y

20 21 junk junk

x y

22 23

w1 w2

Page 18: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Main functionint main() {

//Declares/allocates w1 and w2 Point w1, w2; w1.init(0, 0.5); //sets the x and y value for w1 w2.init(-0.5, 1.5); //sets the x and y value for w2

cout << "\npoint w1 = "; w1.print(); //executes print with w1 data cout << "\npoint w2 = "; w2.print(); //executes print with w2 data

w1.plus(w2); //calls plus cout << "\npoint w1 after plus = "; w1.print(); cout<<endl;}

0.0 0.5

x y

20 21 -0.5 1.5

x y

22 23

x y x yw1 w2

Page 19: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Main functionint main() {

Point w1, w2;

w1.init(0, 0.5); //sets the x and y value for w1 w2.init(-0.5, 1.5); //sets the x and y value for w2

cout << "\npoint w1 = "; w1.print(); //executes print with w1 data cout << "\npoint w2 = "; w2.print(); //executes print with w2 data

w1.plus(w2); //calls plus

cout << "\npoint w1 after plus = "; w1.print(); cout<<endl;}

void Point::plus(Point c) { //c is equal to w2 x += c.x; // x=0+-0.5 y += c.y;// y=0.5+1.5}

0.0 0.05

x y

20 21 -0.5 1.5

x y

22 23

x y x yx y x yw1 w2

w1 is the calling object so unqualified variables refer to w1

Page 20: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Main functionint main() {

Point w1, w2;

w1.init(0, 0.5); //sets the x and y value for w1 w2.init(-0.5, 1.5); //sets the x and y value for w2

cout << "\npoint w1 = "; w1.print(); //executes print with w1 data cout << "\npoint w2 = "; w2.print(); //executes print with w2 data

w1.plus(w2); //calls plus

cout << "\npoint w1 after plus = "; w1.print(); cout<<endl;}

void Point::plus(Point c) { //c is equal to w2 x += c.x; // x=0+-0.5 y += c.y;// y=0.5+1.5}

0.0 0.05

x y

20 21 -0.5 1.5

x y

22 23

x y x yx y x yw1 w2

Page 21: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Update using C++ constructs

• Creation of objects using constructors rather than init function

• Override adding one object to another using function overloading rather than creating print function

This will affect both the class declaration and the invocation (how we use the methods in the main function

Page 22: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Constructors

• Constructors are run when an object is created

• It is a special function that different from other methods

• Allows declaration, allocation and initialization of user defined types like native types

//Create point with initialized valuesPoint w1(0,0.5),w2(-0.5,1.5);

//Create points with uninitialized attributes //and initialize with method Point w1, w2; w1.init(0, 0.5); w2.init(-0.5, 1.5);

Page 23: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Updates to class definition

//Create point with initialized valuesPoint w1(0,0.5),w2(-0.5,1.5);

//Create point with default valuesPoint emptyPoint;

void init(double u, double v) { x = u; y = v;

}

//signature has no return type and name of class (exact match)

Point(double a, double b) {

//in scope are parameter and attributes//can initialize class attributes

x=a;y=b;

}

//Creates point with 0 and 0 as defaultsPoint() {

x=0;y=0;

}

Page 24: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Update addition to use + operator

• Update one point by adding another point to it (a bit awkward…will fix this Thursday)

• Can use the addition(+) operatorint main() {

Point emptyPoint,w1(0,0.5), w2(-0.5,1.5);

cout << "\nTesting creation of point with no parameters = "; emptyPoint.print(); cout << "\npoint w1 = "; w1.print(); cout << "\npoint w2 = "; w2.print();

w1+w2; cout << "\npoint w1 after plus = "; w1.print(); cout<<endl;}

Page 25: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Overloading addition operator//point2.ccclass Point { double x, y;

public : Point(){x=0;y=0;} Point(double a,double b) {x=a;y=b;} void operator+(Point c); //function prototype void print (){ cout << "(" << x << "," << y << ")";}};

void Point::operator+(Point c) { //definition not inline//offset the existing point by point c x += c.x; y += c.y;}

Page 26: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Updated main program//point2.ccint main() { Point emptyPoint,w1(0,0.5), w2(-0.5,1.5);

cout << "\nTesting creation of point with no parameters = "; emptyPoint.print(); cout << "\npoint w1 = "; w1.print(); cout << "\npoint w2 = "; w2.print();

w1+w2; cout << "\npoint w1 after plus = "; w1.print(); cout<<endl;}

Testing creation of point with no parameters = (0,0)point w1 = (0,0.5)point w2 = (-0.5,1.5)point w1 after plus = (-0.5,2)

Page 27: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Recap

• Class– user defined data type– combines data (attributes) and functions

(methods) together• Constructors allow us to control the

initialization of attributes upon creation• Redefine operator functions to use natural

semantics (operator overloading)

Page 28: Week 3: Classes, constructors, destructors, new operator Operator and function overloading

Thursday

• Destructors• Overloading the print function (operator <<)• Fixing addition to return a new object rather

than mutate the existing objectw3=w1+w2;

• Friend functions• Accessors and mutators for encapsulation