lecture 10 - purdue university€¦ · lecture 10 xiaoguang wang stat 598w feb 25th, 2014 (stat...

49
Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49

Upload: others

Post on 01-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Lecture 10

Xiaoguang Wang

STAT 598W

Feb 25th, 2014

(STAT 598W) Lecture 10 1 / 49

Page 2: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Outline

1 Some details

2 Operator Overloading

3 Inheritance

4 Exercises

(STAT 598W) Lecture 10 2 / 49

Page 3: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Outline

1 Some details

2 Operator Overloading

3 Inheritance

4 Exercises

(STAT 598W) Lecture 10 3 / 49

Page 4: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Class Scope

Every class has a different scope.

We can access members by using the dot or arrow operator:

class Obj;

class *ptr=&Obj;

ptr->member;

obj.member;

We can define function-members:

type class::name(arguments) const{

...code...

}

(STAT 598W) Lecture 10 4 / 49

Page 5: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Access Specifiers

class class_name{

access_specifier_1:

member 1;

access_specifier_2:

member 2;

...

}

Access Specifier: public, protected, private

(STAT 598W) Lecture 10 5 / 49

Page 6: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Access Specifiers

The specifiers modify the access rights that the members following themacquire:

private: private members of a class are accessible only from withinother members of the same class or from their friends.

protected: protected members are accessible from members of theirsame class and from their friends, but also from members of theirderived classes.

public: public members are accessible from anywhere where theobject is visible.

(STAT 598W) Lecture 10 6 / 49

Page 7: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Functions

In principle, private and protected members of a class cannot beaccessed from outside the same class in which they are declared.

This rule does not affect friends.

Friends: functions or classes declared with the friend keyword.

A friend function of a class has access to the private and protectedmembers of this class.

We can declare an external function as friend of a class by declaring aprototype of this external function within the class, and preceding itwith the keyword friend.

(STAT 598W) Lecture 10 7 / 49

Page 8: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Functions: example

// friend functions

#include <iostream>

using namespace std;

class CRectangle {

int width, height;

public:

void set_values (int, int);

int area () {return (width * height);}

friend CRectangle duplicate (CRectangle);

};

void CRectangle::set_values (int a, int b) {

width = a;

height = b;}

(STAT 598W) Lecture 10 8 / 49

Page 9: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Functions: Example (Continued)

CRectangle duplicate (CRectangle rectparam)

{

CRectangle rectres;

rectres.width = rectparam.width*2;

rectres.height = rectparam.height*2;

return (rectres);

}

int main () {

CRectangle rect, rectb;

rect.set_values (2,3);

rectb = duplicate (rect);

cout << rectb.area();

return 0;

}

(STAT 598W) Lecture 10 9 / 49

Page 10: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Functions

Remarks:

The duplicate function is NOT a member of class CRectangle! Itsimply has access to its private and protected members without beinga member.

The friend functions can serve, for example, to conduct operationsbetween two different classes.

Generally, the use of friend functions is out of an object-orientedprogramming methodology, so whenever possible it is better to usemembers of the same class to perform operations with them.

(STAT 598W) Lecture 10 10 / 49

Page 11: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Classes: Example

// friend class

#include <iostream>

using namespace std;

class CSquare;

class CRectangle {

int width, height;

public:

int area ()

{return (width * height);}

void convert (CSquare a);

};

(STAT 598W) Lecture 10 11 / 49

Page 12: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Classes: Example (Continued)

class CSquare {

private:

int side;

public:

void set_side (int a)

{side=a;}

friend class CRectangle;

};

void CRectangle::convert (CSquare a) {

width = a.side;

height = a.side;

}

(STAT 598W) Lecture 10 12 / 49

Page 13: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Classes: Example (Continued)

int main () {

CSquare sqr;

CRectangle rect;

sqr.set_side(4);

rect.convert(sqr);

cout << rect.area();

return 0;

}

(STAT 598W) Lecture 10 13 / 49

Page 14: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Classes

Remarks:

Declaration of class CSquare at the beginning of the program isnecessary.

CRectangle is considered as a friend class by CSquare, butCRectangle does not consider CSquare to be a friend.

Friendships is not transitive: The friend of a friend is not consideredto be a friend unless explicitly specified.

(STAT 598W) Lecture 10 14 / 49

Page 15: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Friend Class members

A friend can be a non-member function, a member function of apreviously defined class or an entire class. We can declare for example:

friend void CRectangle::convert(const CSquare& a);

inside the CSquare class definition or declaration.

(STAT 598W) Lecture 10 15 / 49

Page 16: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Outline

1 Some details

2 Operator Overloading

3 Inheritance

4 Exercises

(STAT 598W) Lecture 10 16 / 49

Page 17: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Overloaded operator

Functions with special names:+,-,*,%,^,&,~,!,=,==,++,<,>,<=,>=,&&,+=,-=,*=,[],(),->,etc.

Example:

double operator+(double& ,double& );

The overloaded functions can be defined inside or outside of a class.

Example1& Example1::operator+=(const Example1&);

Example1 operator+(const Example1&,const Example1&);

Usually if a overloaded operator is defined outside a class it is a friendmember of such class.

(STAT 598W) Lecture 10 17 / 49

Page 18: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Overloaded operator

The overloaded operators are used in the usual way:

cout << item1+item2 <<endl;

Example1 a+=b;

or they can be used as functions:

cout << operator+(item1,item2) <<endl;

Example1 a.operator+=(b);

(STAT 598W) Lecture 10 18 / 49

Page 19: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Arithmetic and Relational Operators

Usually the arithmetic and relational operators are defined asnon-member functions.

Arithmetic operators. Arguments: Constant and Reference-typeclasses. Output: new class type.

Relational operators: Arguments: Constant and Reference-typeclasses. Output: Boolean.

(STAT 598W) Lecture 10 19 / 49

Page 20: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Arithmetic and Relational Operators

bool Example1::operator==(const Example1& ex1, _

const Example1& ex2){

return ex1.member1=ex2.member1 && ex1.member2==ex2.member2;

}

bool Example1::operator!=(const Example1& ex1, _

const Example1& ex2){

return !(ex1==ex2);

}

(STAT 598W) Lecture 10 20 / 49

Page 21: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Assignment Operator

The assignment operators can be overloaded.

They should be defined as member functions, and they should returna reference to *this.

Example:

Example1& Example1::operator=(const Example1& ex1){

member1=ex1.member1;

member2=ex1.member2;

return *this;

}

(STAT 598W) Lecture 10 21 / 49

Page 22: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Subscript Operator

The subscript operator should be defined as a member function.

Example:

double& Foo::operator[](const int ix){

if(ix==0){

return member1;

}else{

return member2;

}

}

(STAT 598W) Lecture 10 22 / 49

Page 23: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Outline

1 Some details

2 Operator Overloading

3 Inheritance

4 Exercises

(STAT 598W) Lecture 10 23 / 49

Page 24: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Inheritance

Inheritance allows to create classes which are derived from other classes,so that they automatically include some of its ”parent’s” members, plus itsown.

(STAT 598W) Lecture 10 24 / 49

Page 25: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Format and syntax

Format:

class derived_class_name: public base_class_name

{ /*...*/ };

derived class name: name of the derived class;

base class name: name of the class on which the derived class isbased;

public access specifier: may be replaced by any one of the otheraccess specifiers protected and private. It limits the most accessiblelevel for the members inherited from the base class.

(STAT 598W) Lecture 10 25 / 49

Page 26: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Derived Class: Example

#include <iostream>

using namespace std;

class CPolygon {

protected:

int width, height;

public:

void set_values (int a, int b)

{ width=a; height=b;}

};

class CRectangle: public CPolygon {

public:

int area ()

{ return (width * height); }

};

(STAT 598W) Lecture 10 26 / 49

Page 27: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Derived Class: Example (Continued)

class CTriangle: public CPolygon {

public:

int area ()

{ return (width * height / 2); }

};

int main () {

CRectangle rect;

CTriangle trgl;

rect.set_values (4,5);

trgl.set_values (4,5);

cout << rect.area() << endl;

cout << trgl.area() << endl;

return 0;

}

(STAT 598W) Lecture 10 27 / 49

Page 28: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Derived Class: Example (Continued)

protected vs. private: When a class inherits from another one, themembers of the derived class can access the protected membersinherited from the base class, but not its private members.

In our example, the members inherited by CRectangle and CTrianglehave the same access permissions as they had in their base classCPolygon:

CPolygon::width // protected access

CRectangle::width // protected access

CPolygon::set_values() // public access

CRectangle::set_values() // public access

(STAT 598W) Lecture 10 28 / 49

Page 29: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Derived Class: Example (Continued)

class CRectangle: public CPolygon { ... }

This public keyword after the colon (:) denotes the most accessiblelevel the members inherited from the class that follows it (in this caseCPolygon) will have.

If we specify a more restrictive access level like protected, all publicmembers of the base class are inherited as protected in the derivedclass.

(STAT 598W) Lecture 10 29 / 49

Page 30: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Inheritance

In principle, a derived class inherits every member of a base class except:

its constructor and its destructor

its operator=() members

its friends

(STAT 598W) Lecture 10 30 / 49

Page 31: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Multiple Inheritance

In C++ it is perfectly possible that a class inherits members frommore than one class.

Example:

class CRectangle: public CPolygon, public COutput;

class CTriangle: public CPolygon, public COutput;

(STAT 598W) Lecture 10 31 / 49

Page 32: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Example

Based on cplusplus.com examples:

We want to create a class that describes a “small” polygons (trianglesand rectangles). They share 2 properties: base and height.

class Polygon {

public:

Polygon();

Polygon(double a,double b,string s):

width(a), height(b), type(s) {};

virtual double perimeter() const;

virtual ~Polygon();

protected:

double width, height;

private:

string type;

};

(STAT 598W) Lecture 10 32 / 49

Page 33: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Virtual functions

The virtual functions can be used by the derived classes without anyrestriction.

Virtual members produce dynamic binding: the compiler delays therun-time until the selection of a proper function to execute.

If a function is virtual then the compiler selects the function’sdefinition until the class is properly defined.

Any virtual function can be re-defined in the derived class. If it is notre-defined, the base definition is used.

(STAT 598W) Lecture 10 33 / 49

Page 34: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

More details

A class should be defined before it can be used as a base class.

Any derived class can be used as a base class.

The derived classes are declared in the usual way: name_class name;

(STAT 598W) Lecture 10 34 / 49

Page 35: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Dynamic binding

If we define a pointer (or reference) to base class, we can use it topoint (or to reference) to their derived classes. Example:

void print_dim(const Polygon& pp);

Polygon p1;

print_dim(p1);

Polygon *point1=&p1;

Triangle t1;

print_dim(t1);

point1=&t1;

The compiler always “thinks” that the pointer refers to a base-classobject.

(STAT 598W) Lecture 10 35 / 49

Page 36: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Dynamic binding

The pointers/references to base-class types have two different forms:

Static type: type treated by the compiler.Dynamic type: run-time type, or type treated by the object file.

In this case:

Static type: pointer/reference to base-class.Dynamic type: pointer/reference to derived-class.

The same applies to the virtual functions or members. (the sameobject can refer to different class definitions.)

It is fundamental that we pass the arguments as pointers/referencesin our virtual functions. In this way the virtual objects are determinedat run-time.

(STAT 598W) Lecture 10 36 / 49

Page 37: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Dynamic binding

The virtual mechanism can be overriden using the scope operator:

double result1=t1->Polygon::perimeter();

this code will be resolved at compile-time.

(STAT 598W) Lecture 10 37 / 49

Page 38: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Polymorphism

Ability to create a variable, function, or an object that has more thanone form. (Wikipedia)

We have used polymorphism in C++ several times:

Operator overloading.Constructor overloading (function overloading in general).Dynamic binding in virtual members.Base-class reference and pointer types.

The polymorphism aims for simplicity in the programming language.

(STAT 598W) Lecture 10 38 / 49

Page 39: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Constructors

The constructors of a base class can be defined in the usual way. Butthey can be made protected or private.

If we use the default derived-class constructor, example:Triangle();, it performs two tasks:

It invokes the base default constructor Polygon() and it initializes allits members.It initializes the derived-class members based on their current types.

Also we can initialize partially the derived class.

Or we can initialize all the class members by using the base-classconstructor within the derived-class constructor.

(STAT 598W) Lecture 10 39 / 49

Page 40: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Example

Triangle(double a, const double&b, const double& value):

Polygon(a,b,’tri’), value1(value) {}

(STAT 598W) Lecture 10 40 / 49

Page 41: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Copy constructor

The default copy constructor of a derived class is based on thecorresponding constructor of the base class.

We can create copy constructors as well. This definitions override thedefault contructors. Example:

class Base{.....};

class Derived: public Base{

public:

Derived(const Derived& d):

Base(d), ...... {}

}

(STAT 598W) Lecture 10 41 / 49

Page 42: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Assignment operator

Derived & Derived::operator=(const Derived& rhs){

if(this!=&rhs){

Base::operator=(rhs);

...

...

}

return *this;

}

(STAT 598W) Lecture 10 42 / 49

Page 43: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Destructors

The destructors have a different behavior. They cannot be inherited:the destructor only cleans up its class members.

If we delete a pointer to a class, we have to possible responses:

If the class is a base: members of the class are clean up.If the class is derived: behavior is undefined. We fix this by declaring avirtual destructor.

Example:

Base *pointer1=new Base;

delete pointer1; //base-class destructor

Derived *pointer2=new Derived;

delete pointer2; //derived-class destructor

(STAT 598W) Lecture 10 43 / 49

Page 44: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Calling Order of constrcutors and destructors

Consider the code below:

#include<iostream>

using namespace std;

class base{

public:

base(){

cout<<"A"<<endl

}

~ base(){

cout<<"B"<<endl

}

}

(STAT 598W) Lecture 10 44 / 49

Page 45: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Calling order of constructors and destructors

class derived : public base{

public:

derived(){

cout<<"C"<<endl

}

~derived(){

cout<<"D"<<endl

}

}

int main(){

derived d1;

return 0;

}

(STAT 598W) Lecture 10 45 / 49

Page 46: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Outline

1 Some details

2 Operator Overloading

3 Inheritance

4 Exercises

(STAT 598W) Lecture 10 46 / 49

Page 47: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Zero-Coupon Yield Models

Martellini et al (2003)

There are many ways to construct models on the zero-coupon yieldcurve r(t). Most of them are solutions of differential equations.

The simplest of such models is the Nelson-Siegel model:

r(t) = β0 + (β1 + β2)

[1 − e−

]+ β2

[e−

]where:

β0: long-term interest rate.β1: long-to-short-term spread.β2: curvature parameter.λ: scale parameter (decay rate of short and medium-term components)

(STAT 598W) Lecture 10 47 / 49

Page 48: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Zero-Coupon Yield Models

In order to have more interesting shapes in the zero-curve,Svensson(1994) defines:

r(t) = β0 + β1

[1 − e

− tλ1

tλ1

]+ β2

[1 − e

− tλ1

tλ1

− e− t

λ1

]

+ β3

[1 − e

− tλ2

tλ2

− e− t

λ2

]

(STAT 598W) Lecture 10 48 / 49

Page 49: Lecture 10 - Purdue University€¦ · Lecture 10 Xiaoguang Wang STAT 598W Feb 25th, 2014 (STAT 598W) Lecture 10 1 / 49. Outline 1 Some details 2 Operator Overloading 3 Inheritance

Exercise

Implement the Nelson-Siegel and Svensson models in your termstructure class. Take their parameters as vectors, and define them asdata-members in your class.

Test your code with the following parameters:

Nelson-Siegel: β0 = 0.07, β1 = −0.02, β2 = 0.01, λ = 3.33.Svensson:β0 = 0.07, β1 = −0.02, β2 = 0.01, β3 = −0.01, λ1 = 3.33, λ2 = 0.3.

(STAT 598W) Lecture 10 49 / 49