Вы находитесь на странице: 1из 19

Object Oriented

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

Outline

Data Hiding

Access Specifiers

Objects as Function Arguments

Returning Objects from Functions

Classes versus Structures

Classes, Objects, & Memory

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 dont need to access it

The primary mechanism for hiding data is to put


it in a class and make it private

One classs data is hidden from other classes


3

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)

Access Specifiers
Class
Not accessible
from outside
class

private:
//Data or functions

Accessible from
outside class
public:
//Data or functions

Private
//within class
public:
.
private:
double GPA;
string address1, address2;
int numOfGrades;
string fullName;

//Outside class
cout << habib.GPA;
cout << ismail. numOfGrades;
6

Public
class Student
{
public:
lll

int ID;
private:
lll

};

//outside class
cout << habib.ID;
cout << mohsin.ID;
cout << se101[37].ID;

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 public
8

Objects as Function
//distance.h
Arguments

class Distance
{
private:
int feet;
float inches;
public:
void setFeet(int);
void setInches(float);
int getFeet();
float getInches();
void addDistance(Distance,Distance);
};
9

Objects as Function
void
Arguments
//distance.cpp
Distance::addDistanc
#include"distance.h"

e (Distance d1,
Distance d2)

void Distance::setFeet(int f)
{feet=f;}
void
Distance::setInches( float
i)

{inches=d1.inches+d2.
inches;
feet=0;
if(inches>=12.0)
{inches-=12.0;
feet++;}

{inches=i;}
int Distance::getFeet()
{return feet;}
float Distance::getInches()
{return inches;}

feet+=d1.feet+d2.feet;
}
10

Objects as Function
Arguments
//driver.cpp
dist2.setInches(5.75);
#include"distance.h"
#include<iostream>
using namespace std;
void main()
{
Distance dist1, dist2, dist3;
dist1.setFeet(11);
dist1.setInches(6.25);
dist2.setFeet(17);

dist3.addDistance
(dist1,dist2);
cout<<dist3.getFeet()
<<endl;
cout<<dist3.getInches()
<<endl;
}
Output
29
0
Press any key to continue
11

Objects as Function
Arguments dist3
feet
29
inches
0

feet
inches

dist3.addDistance(dist1
,dist2);
d1.feet
d1.inch
es

dist1
feet
11
inches
6.25

dist2
feet
17

d2.feet

inches
5.75
12

d2.inch
es

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

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

Part Example (with class)


Cont
void main()
{
Part part;
part.setPart(555,100,100);
part.showPart();
}

15

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

Part Example (with struct)


Cont
void main()
{
Part part;
part.setPart(555,100,100);
part.showPart();
}

17

Classes, Objects, and


Memory
We believe

Each object created from a class contains separate copies of that


classs 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

Classes, Objects, and


Memory
object
2
data1
data2

object
1
data1

function1

object
3
data1

data2

function2

data2

19

Вам также может понравиться