Академический Документы
Профессиональный Документы
Культура Документы
Paradigm
Lecture # 04
More on Classes, Class
Members, and Objects
Outline
Data Hiding
Access Specifiers
Data Hiding
Access Specifiers
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;
Data is private
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
We believe that
partno=pn;
cost=c;
}
void showPart()
{
cout<<"Model :
"<<modelno<<endl;
cout<<"Part :
"<<partno<<endl;
cout<<"Cost :
"<<cost<<endl;
}
};
//program continues
14
15
partno=pn;
cost=c;
}
void showPart()
{
cout<<"Model :
"<<modelno<<endl;
cout<<"Part :
"<<partno<<endl;
cout<<"Cost :
"<<cost<<endl;
}
};
//program continues
16
17
Example: cars
In fact
On the other hand, all the objects in a given class use the same
member functions
object
1
data1
function1
object
3
data1
data2
function2
data2
19