OOPLec04

19
Object Oriented Paradigm Lecture # 04 More on Classes, Class Members, and Objects

description

oop

Transcript of OOPLec04

Page 1: OOPLec04

Object Oriented Paradigm

Lecture # 04

More on Classes, Class Members, and Objects

Page 2: OOPLec04

Outline

Data Hiding Access Specifiers Objects as Function Arguments Returning Objects from Functions Classes versus Structures Classes, Objects, & Memory

2

Page 3: OOPLec04

Data Hiding

Data should be concealed within a class, so that

it could not be accessed mistakenly by

functions outside the class

That is, hiding data from parts of the program

that don’t need to access it

The primary mechanism for hiding data is to put

it in a class and make it private

One class’s data is hidden from other classes3

Page 4: OOPLec04

Access Specifiers

private members (data or functions) Can only be accessed from within the

class

public members (data or functions) Are accessible from any where (both

inside and outside the class)

4

Page 5: OOPLec04

Access Specifiers

private://Data or functions

Class

public://Data or functions

Not accessible from outside class

Accessible from outside class

5

Page 6: OOPLec04

Private//within class

public:

….

private:double GPA;string address1, address2;int numOfGrades;string fullName;

//Outside class

cout << habib.GPA;cout << ismail. numOfGrades;

6

Page 7: OOPLec04

Public

class Student{

public:l l l

int ID;private:

l l l

};

//outside classcout << habib.ID;

cout << mohsin.ID;

cout << se101[37].ID;

ü

7

Page 8: OOPLec04

Private and Public

Functions are public Usually functions are public

This is a result of how classes are used

Functions that operate on data are public so they can

be accessed from outside the class Data is private

Data is hidden so it will be safe from accidental

manipulation There is no rule that data must be private and

functions public8

Page 9: OOPLec04

Objects as Function Arguments//distance.hclass Distance{private:

int feet;float inches;

public:void setFeet(int);void setInches(float);int getFeet();float getInches();void addDistance(Distance,Distance);

};

9

Page 10: OOPLec04

Objects as Function Arguments//distance.cpp

#include"distance.h"

void Distance::setFeet(int f)

{feet=f;}

void Distance::setInches( float i)

{inches=i;}

int Distance::getFeet()

{return feet;}

float Distance::getInches()

{return inches;}

void Distance::addDistance (Distance d1, Distance d2)

{inches=d1.inches+d2.inches;

feet=0;

if(inches>=12.0)

{inches-=12.0;

feet++;}

feet+=d1.feet+d2.feet;

}

10

Page 11: OOPLec04

Objects as Function Arguments//driver.cpp

#include"distance.h"

#include<iostream>

using namespace std;

void main()

{

Distance dist1, dist2, dist3;

dist1.setFeet(11);

dist1.setInches(6.25);

dist2.setFeet(17);

dist2.setInches(5.75);

dist3.addDistance (dist1,dist2);

cout<<dist3.getFeet()

<<endl;

cout<<dist3.getInches()

<<endl;

}

Output290Press any key to continue

11

Page 12: OOPLec04

Objects as Function Arguments

feet11

inches6.25

dist1feet17

inches5.75

dist2

feet29

inches0

dist3

dist3.addDistance(dist1,dist2);

d1.feet

d1.inches

d2.feet

d2.inches

feet

inches

12

Page 13: OOPLec04

Structures versus Classes

We believe that Structures provide a way to group data Classes provide a way to group both data and

functions

In fact, structures can also be used to group data and functions

However, an important difference remains that In a class, members are private by default In a structure, members are public by default

13

Page 14: OOPLec04

Part Example (with class)#include<iostream>using namespace std;class Part{ int modelno; int partno; float cost;public:

void setPart(int mn, int pn, float c){modelno=mn;//continues on right side…

partno=pn;cost=c;}void showPart(){cout<<"Model : "<<modelno<<endl;cout<<"Part : "<<partno<<endl;cout<<"Cost : "<<cost<<endl;}

}; //program continues …

14

Page 15: OOPLec04

Part Example (with class) Cont…void main()

{

Part part;

part.setPart(555,100,100);

part.showPart();

}

15

Page 16: OOPLec04

Part Example (with struct)#include<iostream>using namespace std;struct Part{ int modelno; int partno; float cost;public:

void setPart(int mn, int pn, float c){modelno=mn;//continues on right side…

partno=pn;cost=c;}void showPart(){cout<<"Model : "<<modelno<<endl;cout<<"Part : "<<partno<<endl;cout<<"Cost : "<<cost<<endl;}

};//program continues …

16

Page 17: OOPLec04

Part Example (with struct) Cont…void main()

{

Part part;

part.setPart(555,100,100);

part.showPart();

}

17

Page 18: OOPLec04

Classes, Objects, and Memory We believe

Each object created from a class contains separate copies of that class’s data and member functions

It emphasizes that objects are complete, self-contained entities, designed using the class declarations

Example: cars

In fact Each object has its own separate data items On the other hand, all the objects in a given class use the same

member functions The member functions are created and placed in memory only

once The rational being: functions for each object are identical

18

Page 19: OOPLec04

Classes, Objects, and Memory

data1

data2

object1

data1

data2

object2

data1

data2

object3function1

function2

19