Docsity

Lecture 10: Copy Constructors, Destructors, Accessors, this Pointer, Interface, Slides of Object Oriented Programming

Partial preview of the text

Download Lecture 10: Copy Constructors, Destructors, Accessors, this Pointer, Interface and more Slides Object Oriented Programming in PDF only on Docsity!

Object Oriented Programming

(OOP)

Lecture No. 10

Review

• Copy constructors

• Destructor

• Accessor Functions

• this Pointer

Example

Student Student::setRollNo(int aNo) { … return *this; } Student Student::setName(char *aName) { … return *this; }

Example

int main() { Student aStudent; Student bStudent; bStudent = aStudent.setName(“Ahmad”); … bStudent = aStudent.setName(“Ali”).setRollNo(2); return 0; }

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

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

Example

class Complex{ //new float z; float theta; public: void setNumber(float i, float j){ theta = arctan(j/i); … } … };

Advantages

• User is only concerned about

ways of accessing data (interface)

• User has no concern about the

internal representation and

implementation of the class

Student.h

class Student{ int rollNo; public: void setRollNo(int aRollNo); int getRollNo(); … };

Student.cpp

#include “student.h” void Student::setRollNo(int aNo){ … } int Student::getRollNo(){ … }

  • 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

const Member Functions

  • Keyword const is placed at

the end of the parameter list

const Member Functions

Example

class Student{
public:
int getRollNo() const{
return rollNo;

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