OOP Lecture 10: Interface-Implementation Separation, Const Member Functions, this Pointer, Slides of Object Oriented Programming

Advanced object-oriented programming concepts, including copy constructors, destructors, accessor functions, and the this pointer. It also covers the importance of separating interface and implementation, and the use of const member functions. Examples are provided in c++ for complex number representation.

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object Oriented Programming
(OOP)
Lecture No. 10
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download OOP Lecture 10: Interface-Implementation Separation, Const Member Functions, this Pointer 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 ofcomplex number^ 

Euler form^ ►

z = x +

i

y

Phasor form^ ►

z = |z| (cos

i

sin

z is known as the complex modulusand

is known as the complex

Example

float getX()float getY()void setNumber

(float i, float j) float xfloat y Old implementationComplex

float getX()float getY()void setNumber

(float i, float j) float zfloat theta NewimplementationComplex

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 waysof accessing data (interface)

User has no concern about theinternal representation andimplementation 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 aremeant to be read only

There must exist a mechanismto detect error if such functionsaccidentally change the datamember

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 cannotmodify the state of any object

They are just

“read-only”

Errors due to typing are alsocaught at compile time