View Single Post
Old 08-04-2005, 01:34 PM   #3 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Code:
class 2dCoord
{
public:
    2dCoord(double x, double y) { x_ = x; y_ = y; }

protected:
    double x_;
    double y_;
};



class 3dCoord : public 2dCoord
{
public:
    3dCoord(double x, double y, double z) {
        // Call base-class constructor with x and y values
        2dCoord(x, y);
        z_ = z;
    }
    void print() const {
        cout << "x = " << x_ << "\ny = " << y_
             << "\nz = " << z_ << endl;
    }

protected:
    double z_;
};



int main()
{
    3dCoord t(489.2210, 5939.6667, 115.293);
    t.print();
}
That should output:

x = 489....
y = 5939.....
z = 115....

Notice the call to the base class constructor. Theres more efficient ways to do it but you should practice with simple inheritance first (like this) before getting into more advanced inheritance.

As for books, I have too many, but I like "C++ How to Program" by Deitel & Deitel, and "C++ Primer Plus" by Stephen Prata.
fp_unit is offline   Reply With Quote