Академический Документы
Профессиональный Документы
Культура Документы
1
class RailCar {
• Recall that the type of the object/pointer is class BoxCar : public RailCar {
void disp_name() { cout << “ BoxCar”;}
what determines which member you will };
access. class TankCar : public RailCar {
void disp_name() { cout << “ TankCar”; }
• Objects in array are TankCar & BoxCar };
main() {
• But array type is RailCar, we won’t access RailCar *train[100];
member functions. Let’s try it… train[0] = new BoxCar;
train[1] = new TankCar;
train[0]->disp_name(); // “RailCar” output!
…
}
class RailCar {
2
Shortcut for if/else for assignment Reading from Files
• A simple if/else for assigning a value • Open files as “input streams”
can be shortened: • Comes from fstream.h header
• Reading done same as cin
if (x < 0) { #include <fstream.h>
y = x * -1; main() {
} else { int x,y;
y = x; // open the stream
} ifstream happy_stream (“c:\happy.data”);
// equivalent to: // read two ints from it
y = (x < 0 ? x * -1 : x); happy_stream >> x >> y;
happy_stream.close(); // we’re done!