Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 4th, 2005, 03:11 PM
Authorized User
 
Join Date: Nov 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Constructor problem

Suppose I have derived class of:

Code:
class myDerivedClass : public myBaseClass
{
public:
    myDerivedClass(int a, int b) : myBaseClass(x, y) {} // line 4

... // other members from here
...
}
Can anyone tell me whether the line 4 syntax is correct, and if it does, what does it means? Thanks.



yengzhai
__________________
yengzhai
Reply With Quote
  #2 (permalink)  
Old May 5th, 2005, 01:04 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

The syntax is correct.

When you create an object of myDerivedClass type, the constructor used will be the myBaseClass() constructor. In other words the constructor for myDerivedClass uses the constructor inherited from myBaseClass.

Alan


Alan



Reply With Quote
  #3 (permalink)  
Old May 5th, 2005, 04:02 PM
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
  #4 (permalink)  
Old May 16th, 2005, 10:50 PM
Authorized User
 
Join Date: May 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The first (but not last) of the errors is here

                    it is like this:
.................................................. ...........
myDerivedClass(int a, int b) : myBaseClass(x, y) {} // line 4
.................................................. ............

                    it should be:
.................................................. ............
myDerivedClass(int a, int b) : myBaseClass(a,b) {} // line 4
.................................................. ...........

Good luck with the rest of the errors!

Reply With Quote
  #5 (permalink)  
Old May 17th, 2005, 04:36 AM
Registered User
 
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to vigorous365 Send a message via MSN to vigorous365
Default

Hello
   Your program should change to this style

#include <iostream.h>

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

private:
    int x;
    int y;
};

// derived class
class myDerivedClass:public myBaseClass
{
public:
    myDerivedClass(int x, int y):myBaseClass(x, y){ //IZRAILEVICH1 revised here
    a=x;
    b=y;
    }
    void print();

private:
    int a;
    int b;
};

// base class implementation
myBaseClass :: myBaseClass(int param1, int param2)
{
    x = param1;
    y = param2;
}
void myDerivedClass :: print() // Added by me
{
    cout<<a<<" "<<b<<endl;
}

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

// in main program


int main(int argc, char* argv[])
{
    printf("Hello World!\n");

    myDerivedClass derivedClass(5, 6);
    derivedClass.print();

    return 0;
}

// the output is :
    Hello World!
    5 6

zhangyanbo
Reply With Quote
  #6 (permalink)  
Old May 18th, 2005, 06:05 AM
Authorized User
 
Join Date: May 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I THINK ITS RIGHT.

Reply With Quote
  #7 (permalink)  
Old May 18th, 2005, 11:05 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to minhu
Default

may be you should define the print function as virtual

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor nayeem69 .NET Framework 2.0 1 July 17th, 2007 12:33 AM
Constructor ramess C++ Programming 1 March 18th, 2006 07:23 PM
constructor in servlets bondalapati98 Servlets 5 January 16th, 2006 12:27 AM
Problem with a constructor Mishkina General .NET 0 September 23rd, 2005 08:26 AM
VB constructor snowbelly VB How-To 2 August 18th, 2004 04:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.