csc241 object-oriented programming (oop) lecture no. 5

62
CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Upload: oswald-blankenship

Post on 21-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

CSC241 Object-Oriented Programming (OOP)

Lecture No. 5

Page 2: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Review

Member functions implementation

ConstructorsConstructors overloadingCopy constructors

Page 3: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Copy Constructor

Copy constructor are used when: Initializing an object at

the time of creation When an object is

passed by value to a function

Page 4: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

void func1(Student student){

}

int main(){

Student studentA(“Ahmad”);

Student studentB = studentA;

func1(studentA);

}

Page 5: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Copy Constructor (contd.)

Student::Student(const Student &obj){

rollNo = obj.rollNo;

name = obj.name;

GPA = obj.GPA;

}

Page 6: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Shallow Copy

When we initialize one object with another then the compiler copies state of one object to the other

This kind of copying is called shallow copying

Page 7: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example: Shallow Copy

Student studentA(“Ahmad”);

GPA

Name

RollNo

studentB

Heap

AHMAD

GPA

Name

RollNo

studentA

Student studentB = studentA;

Page 8: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example: Shallow Copyint main(){

Student studentA(“Ahmad”, 1); {

Student studentB = studentA;

GPA

Name

RollNo

studentB

Heap

AHMAD

GPA

Name

RollNo

studentA

Page 9: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

int main(){

Student studentA(“Ahmad”, 1); {

Student studentB = studentA;}

}

HeapGPA

Name

RollNo

studentA

Example: Shallow Copy

Page 10: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Student::Student(const Student &obj){

int len = strlen(obj.name);

name = new char[len + 1]

strcpy(name, obj.name);

/*copy rest of the data members*/

}

Example: Deep Copy

Page 11: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

int main(){

Student studentA(“Ahmad”, 1); {

Student studentB = studentA;

AHMAD

GPA

Name

RollNo

studentA

GPA

Name

RollNo

studentB

AHMAD

Example: Deep Copy

Page 12: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

int main(){Student studentA(“Ahmad”, 1);

{Student studentB = studentA;

}}

HeapGPA

Name

RollNo

studentA

AHMAD

Example: Deep Copy

Page 13: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Copy Constructor (contd.)

Copy constructor is normally used to perform deep copy

If we do not make a copy constructor then the compiler performs shallow copy

Page 14: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Destructor

Destructor is used to free memory that is allocated through dynamic allocation

Destructor is used to perform house keeping operations

Page 15: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Destructor (contd.)

Destructor is a function with the same name as that of class, but preceded with a tilde ‘~’

Page 16: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

class Student

{

public:

~Student(){

if (name){

delete[]name;

}

}

}

Page 17: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Overloading

Destructors cannot be overloaded

Page 18: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Sequence of Calls

Constructors and destructors are called automatically

Constructors are called in the sequence in which object is declared

Destructors are called in reverse order

Page 19: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

Student::Student(char * aName){

cout << aName << “Cons\n”;

}

Student::~Student(){

cout << name << “Dest\n”;

}

Page 20: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

int main()

{

Student studentB(“Ali”);

Student studentA(“Ahmad”);

return 0;

}

Page 21: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

Output:Ali ConsAhmad ConsAhmad DestAli Dest

Page 22: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Accessor Functions

Usually the data member are defined in private part of a class – information hiding

Accessor functions are functions that are used to access these private data members

Accessor functions also useful in reducing error

Page 23: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example – Accessing Data Memberclass Student{

int rollNo;

public:

void setRollNo(int aRollNo){

rollNo = aRollNo;

}

};

Page 24: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example – Avoiding Error

void Student::setRollNo(int aRollNo){

if (aRollNo < 0){

rollNo = 0;

}

else

{

rollNo = aRollNo;

}

}

Page 25: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example - Getter

class Student{

int rollNo;

public:

int getRollNo(){

return rollNo;

}

};

Page 26: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

this Pointerclass Student{

int rollNo;

char *name;

float GPA;

public:

int getRollNo();

void setRollNo(int aRollNo);

};

Page 27: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

this PointerThe compiler reserves

space for the functions defined in the class

Space for data is not allocated (since no object is yet created)

Function SpacegetRollNo(), …

Page 28: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

this PointerStudent s1, s2, s3;

Function SpacegetRollNo(), …

s1(rollNo,…)

s2(rollNo,…)

s3(rollNo,…)

Page 29: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

this PointerFunction space is common

for every variableWhenever a new object is

created:Memory is reserved for

variables onlyPreviously defined functions

are used over and over again

Page 30: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

this PointerMemory layout for objects

created:s1rollNo, …

Function SpacegetRollNo(), …

s2rollNo, …

s3rollNo, …

s4rollNo, …

• How does the functions know on which object to act?

Page 31: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

this Pointer Address of each object is passed to the calling

function

This address is deferenced by the functions and hence they act on correct objects

address

s1rollNo, …

s2rollNo, …

s3rollNo, …

s4rollNo, …

address address address

• The variable containing the “self-address” is called this pointer

Page 32: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Passing this Pointer

Whenever a function is called the this pointer is passed as a parameter to that function

Function with n parameters is actually called with n+1 parameters

Page 33: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

void Student::setName(char *)

is internally represented as

void Student::setName(char *, const Student *)

Page 34: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Declaration of this

DataType * const this;

Page 35: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Compiler Generated CodeStudent::Student(){

rollNo = 0;

}

Student::Student(){

this->rollNo = 0;

}

Page 36: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Review

Copy constructorsDestructorAccessor Functionsthis Pointer

Page 37: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

this PointerThere are situations where designer wants to return reference to current object from a function

In such cases reference is taken from this pointer like (*this)

Page 38: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

Student Student::setRollNo(int aNo)

{

return *this;

}

Student Student::setName(char *aName)

{

return *this;

}

Page 39: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

int main()

{

Student aStudent;

Student bStudent;

bStudent = aStudent.setName(“Ahmad”);

bStudent = aStudent.setName(“Ali”).setRollNo(2);

return 0;

}

Page 40: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Separation of interface and implementation

Public member function exposed by a class is called interface

Separation of implementation from the interface is good software engineering

Page 41: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Complex Number

There are two representations of complex number Euler form

z = x + i y Phasor form

z = |z| (cos + i sin )z is known as the complex

modulus and is known as the complex argument or phase

Page 42: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

float getX()float getY()void setNumber (float i, float j)…

float x float y

Complex

Old implementation

float getX()float getY()void setNumber (float i, float j)…

float z float theta

Complex

New implementation

Page 43: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

class Complex{ //old

float x;

float y;

public:

void setNumber(float i, float j){

x = i;

y = j;

}

};

Page 44: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

class Complex{ //new

float z;

float theta;

public:

void setNumber(float i, float j){

z = sqrt((i ^ 2) + (j ^ 2));

theta = arctan(j / i);

}

};

Page 45: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Advantages

User is only concerned about ways of accessing data (interface)

User has no concern about the internal representation and implementation of the class

Page 46: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Separation of interface and implementation

Usually functions are defined in implementation files (.cpp) while the class definition is given in header file (.h)

Some authors also consider this as separation of interface and implementation

Page 47: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Student.h

class Student{

int rollNo;

public:

void setRollNo(int aRollNo);

int getRollNo();

};

Page 48: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Student.cpp

#include “student.h”

void Student::setRollNo(int aNo){

}

int Student::getRollNo(){

}

Page 49: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Driver.cpp

#include “student.h”

int main(){

Student aStudent;

}

Page 50: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

const Member Functions

There are functions that are meant to be read only

There must exist a mechanism to detect error if such functions accidentally change the data member

Page 51: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

const Member Functions

Keyword const is placed at the end of the parameter list

Page 52: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

const Member FunctionsDeclaration:

class ClassName{

ReturnVal Function() const;

};

Definition:

ReturnVal ClassName::Function() const{

}

Page 53: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

class Student{

public:

int getRollNo() const{

return rollNo;

}

};

Page 54: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

const Functions

Constant member functions cannot modify the state of any object

They are just “read-only”Errors due to typing are

also caught at compile time

Page 55: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

bool Student::isRollNo(int aNo){

if (rollNo == aNo){

return true;

}

return false;

}

Page 56: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

bool Student::isRollNo(int aNo){

/*undetected typing mistake*/

if (rollNo = aNo){

return true;

}

return false;

}

Page 57: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

bool Student::isRollNo(int aNo)const{

/*compiler error*/

if (rollNo = aNo){

return true;

}

return false;

}

Page 58: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

const Functions

Constructors and Destructors cannot be const

Constructor and destructor are used to modify the object to a well defined state

Page 59: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

class Time{

public:

Time() const {} //error…

~Time() const {} //error…

};

Page 60: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

const Function

Constant member function cannot change data member

Constant member function cannot access non-constant member functions

Page 61: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

Example

class Student{

char * name;

public:

char *getName();

void setName(char * aName);

int ConstFunc() const{

name = getName();//error

setName(“Ahmad”);//error

}

};

Page 62: CSC241 Object-Oriented Programming (OOP) Lecture No. 5

this Pointer and const Member Function

this pointer is passed as constant pointer to const data in case of constant member functions

const Student *const this;instead of

Student *const this;