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

Pointers to Class Objects

• Can instantiate class objects with pointers


• Similar to structures, but use “new”:
Intro to C++ part 3
main() {
BoxCar x; // creates new box car
Daniel Wigdor BoxCar *p = NULL, *b = NULL; // boxcar pointers
p = &x; // p points to x
Some material from:
b = new BoxCar; // create new boxcar
Winston, P.H., “On to C++” ...
ISBN: 0-201-58043-8

Using Pointers to Classes Arrays of Class Objects


• Just like structures, pointers to classes • 2 ways to store arrays of class objects
must be dereferenced main() {
BoxCar train1[50]; // creates 50 boxcars
BoxCar *train2[50]; // creates 50 boxcar pointers
#include <iostream.h >
main() { // create 50 boxcars
for (int i=0; i<50; i++) {
BoxCar *b = new BoxCar;
train2[50] = new BoxCar;
cout << “Volume of b is ” b->volume();
}
// equivalent to:
cout << “Volume of b is ” (*b).volume(); // pointer requires dereference
train1[0].setHeight(15);
...
train2[0]->setHeight(15);

“Super” Pointers Arrays of “Different” Objects


• A pointer to a superclass can be set to • We can use one array to hold pointers to
point to any of its sub-classes different classes that have a common
• Recall that “RailCar” is the parent of both ancestor
“BoxCar” and “TankCar”: …
main() {
RailCar *train[100];// 100 pointers to railcars
class RailCar { … };
train[0] = new BoxCar;
class BoxCar : public RailCar { … };
train[1] = new TankCar;
class TankCar : public RailCar { … }; …

RailCar *b = new BoxCar,*t = new TankCar; • Is this useful?

1
class RailCar {

Pointer Type Matters! public:


void disp_name() { cout << “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 {

“Virtual Functions” public:


virtual void disp_name() { cout << “ RailCar”;}
};
• Functions marked “Virtual” run based on class BoxCar : public RailCar {
// virtual property is inherited
object type, not pointer type. virtual void disp_name() { cout << “BoxCar”;}
};
class RailCar { class TankCar : public RailCar {
public: virtual void disp_name() { cout << “TankCar”; }
virtual void disp_name() { cout << “ RailCar”;} };
}; main() {
class BoxCar : public RailCar { RailCar *train[100];
// virtual property is inherited train[0] = new BoxCar;
virtual void disp_name() { cout << “BoxCar”;} train[1] = new TankCar;
}; train[0]->disp_name(); // “BoxCar” output!
class TankCar : public RailCar { train[1]->disp_name(); // “TankCar” output!
virtual void disp_name() { cout << “TankCar”; } }
};

“Pure” Virtual Functions Now some FUN STUFF!


• In this example, we never want someone
to run the disp_name function from
RailCar
• We make it a “pure” virtual function:
class RailCar {
public:
// this function can never be called.
virtual void disp_name() = 0;
};

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!

Writing to Files Close all streams!


• File output through “output streams” • Don’t count on OS to close streams
• Very similar to input streams. • Can’t re-open an open stream
#include <fstream.h>
main() {
• Blocks yours and all other programs
int x = 5, y = 17;
// open the stream
ofstream happy_stream (“c:\happy.data”);
// write two ints to it
happy_stream << x << y;
...

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