Creating objects
class Location {
public:
Location(double latitude, double longitude);
};
class Trip {
public:
Trip(Location
destination);
};
int main () {
// Q1: Create
a trip to: 15.5 deg. long, 67.3 deg lat.
------------------------------------------------------------------------------
class AnyClass {
public:
void
someFunction() {
cout << "Got Here!";
}
};
int main() {
// Q2: Finish
main so the message "Got Here!" is printed.
------------------------------------------------------------------------
class CA {
};
class CB {
};
class CC {
public:
CC(CA ca, CB
cb);
void printIt()
{
cout << "HELLO";
}
};
int main() {
// Q3: write
code that will cause "HELLO" to print
----------------------------------------------------------------------
Common mistakes:
class Patient {
public:
Patient(int
age);
};
int main() {
Patient(65); Problem!!!
Patient
pat; Problem!!!
pat(65);
-------------------------------------------------------------------------
Writing a member function ...
What can you
work with?
- any private data
- any public (or private) functions
class Line {
private:
int x1, y1,
x2, y2;
public:
double
length() {
//use x1,y1,x2,y2 as much as you like
}
};
--------------------------------------------------------------------------
class Line {
public
double
getX(int i); // i == 1 or 2
double
getY(int i); // i == 1 or 2
double
length() {
//use getX(1) getX(2) getY(1) getY(2) as much as you like
}
};
----------------------------------------------------------------------
Common confusion:
class Line {
private:
int x1, y1,
x2, y2;
public
double
length() {
Line someLine;
... someLine.x1 ... someLine.x2 ....
}
};
int main() {
Line aLine;
cout <<
aLine.length();
}
----------------------------------------------------------------
Type rules
class Person {
public:
double
getIncome();
};
class MarriedCouple {
private:
Person
husband, wife;
public:
double
getIncome() {
return husband+wife; // Problem
}
};
-----------------------------------------------
Who can see what?
class C1 {
private:
int privC1;
public:
int pubC1;
void C1Method() {
privC1, pubC1,
privC2, pubC2, mainVar ?? Legal
}
};
class C2 {
private:
int privC2;
public:
int pubC2;
void C2Method() {
privC1, pubC1,
privC2, pubC2, mainVar ?? Legal
}
};
int main() {
C1 c1;
C2 c2;
int mainVar;
c1.privC1,
c1.pubC1, c2.privC2, c2.pubC2, mainVar
}
----------------------------------------------------
When do you need to put a dotted object in front of
a member function call?
class TaxPayer {
public:
void showAddress();
}
class TaxForm {
private:
TaxPayer client;
void showDirections();
public:
void showForm() {
showDirections() or
tf.showDirections(); ??
client.showAddress()
or showAdress(); ??
}
};
int main() {
TaxForm tf;
tf.showForm() or showForm()
??
-------------------------------------------
Do all my member functions need to be public?
Class Collection {
private:
double getTotal();
public:
double getAve();
}
int main() {
Collection coll;
. . .
cout <<
coll.getAve(); ?? Legal
cout <<
coll.getTotal(); ?? Legal
--------------------------------------------------------------
member functions when other
objects show up....
class Applicant {
public:
double
getCreditRating();
};
class Bank {
public:
// Q4: write a member function that determines
// if an applicant is eligible for a mortgage
// (Credit Rating greater than 700)
-----------------------------------------------------------------
class Senator {
public:
int
votedFor(int billID);
};
class Senate {
private:
Senator
senators[100];
public:
// Q5: write a member function that tells whether
// some bill was passed by the senate unanimously
-----------------------------------------------------
class MaterialSample
public:
double
getVolume();
double
getWeight();
// Q6: write a member function that determines
// the material's density
--------------------------------------------------------------------
class Product {
public:
Product(char
name[MAX]);
};
class Store {
public:
double
priceFor(Product pro);
};
class ComparisonShopper {
private:
Store
suppliers[MAXSTORES];
public:
// Q7: write a member function that determines
// the best price available for a certain product
------------------------------------------------------------------
SomeObject objs[MAX];
int objsCount;
Finding the min .....
double min = objs[0].getValue();
for(int i = 1; i < objsCount;
i++) {
if
(objs[i].getValue() < min) {
min = objs[i].getValue();
}
}
Adding them all up ....
double total = 0;
for(int i = 0; i < objsCount;
i++) {
total +=
objs[i].getValue();
}
Finding one of a particular sort
.....
for(int i = 0; i < objsCount;
i++) {
if
(objs[i].getValue() == searchValue)
return objs[i];
}
// couldn't find a match
Seeing if they all work .....
for(int i = 0; i < objsCount;
i++) {
if
(!objs[i].isWorking())
return false;
}
return true;