View Single Post
  #3 (permalink)  
Old May 5th, 2005, 04:02 PM
yengzhai yengzhai is offline
Authorized User
 
Join Date: Nov 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. Because I have never seen such syntax, normally what I get from book is:

myDerivedClass :: myDerivedClass(int a, int b) : myBaseClass(x, y)
{
public:
...
}

So the above statement is equivalent to the statement that I posted the other day?

If the statement is correct, but somehow I still get the meaningless output from the screen. Below is my coding:

Code:
// base class
class myBaseClass
{
public:
    myBaseClass(int, int);
    void print();

private:
    int x;
    int y;
};

// derived class
class myDerivedClass:public myBaseClass
{
public:
    myDerivedClass(int, int):myBaseClass(x, y){}
    void print();

private:
    int a;
    int b;
};

// base class implementation
myBaseClass :: myBaseClass(int param1, int param2)
{
    x = param1;
    y = param2;
}

void myBaseClass :: print()
{
    cout<<x<<" "<<y<<endl;
}

// in main program
myDerivedClass derivedClass(5, 6);
derivedClass.print();

If the above coding is executed, the output will have these kinda numbers:

-858993460 -858993460
where the expected output should be 5 and 6.

Is there any mistakes in my sample program?






yengzhai
Reply With Quote