Sign in to follow this  
powerskill

Inheritance Help

Recommended Posts

I'm trying to do this with different .cpp files in the same project. Not sure if it matters, I'm using devC++. My problem is, I can't compile it and I can't inherit anything at all. Please help Thank you. // in main

#include <cstdlib>
#include <iostream>
#include <string>

#include "baseClass.h"
#include "derivedClass.h"

using namespace std;

int main(int argc, char *argv[])
{
    derivedClass one;
    one.set( 2, 3 );
    one.show ();
    one.setk();
    one.showk();

    system("PAUSE");
    return EXIT_SUCCESS;
}



// base class .h

#ifndef H_baseClass
#define H_baseClass

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class baseClass
{
   protected:
             int i;
             int j;
 
   public:
          void set ( int a, int b );
          
          void show ();
   
          baseClass();
};
#endif



// base class .cpp

#include <cstdlib>
#include <iostream>
#include <string>

#include "baseClass.h"

using namespace std;
          
void baseClass::set ( int a, int b )
{
     i = a;
     j = b;
}
          
void baseClass::show ()
{
     cout << i << " " << j << endl;
}
  
baseClass::baseClass()
{

}



// derived class .h

#ifndef H_derivedCLass
#define H_derivedCLass

#include <cstdlib>
#include <iostream>
#include <string>

#include "baseClass.h"

using namespace std;

class derivedClass: public baseClass
{
   private:
           int k;     
   
   public:
          derivedClass(); // constructor
          void setk ( );
          void showk ( );
    
};
#endif



// derived class .cpp

#include <cstdlib>
#include <iostream>
#include <string>

#include "derivedClass.h"

using namespace std;

void derivedClass::setk ( )
{
   k = i * j; // compiler is telling me to declare k, but why?
              // didn't I give k and passed it to the derived class
              // when i made the base class?

}

void derivedClass::showk ( )
{
   cout << k << endl;
}

// constructor
derivedClass::derivedClass()
{
} 



[Edited by - powerskill on November 26, 2006 3:12:36 PM]

Share this post


Link to post
Share on other sites
Step 1) Post all compiler/linker errors, in full [smile]

A first guess would be that the constructor for base class in baseclass.cpp should have the class name first, like so:

baseClass::baseClass()
{

}


And in derived class.cpp, you would want to #inlcude derived class.h, and add derivedClass:: before the functions listed inside.


Finally, why does showk() take an integer argument?

Good Luck!

Share this post


Link to post
Share on other sites
I made a few corrections but still not working, I'll upgrade my first post.
This time it will have the compiler errors.
And some new stuff thats bothering me, which might help you guys help me.

Share this post


Link to post
Share on other sites
To elaborate only very slightly on one of rip-off's points:

void setk ( int k )
{
/* When you have two variables of the same name in scope, the one which is
used is the one with the most local scope. In this case, the k you're
assigning a value to is the one local to this function (which is passed
in as an argument for no discernable reason, as rip-off pointed out)
rather than the member variable of your class. */

k = i * j;
}



EDIT: Posted 12 seconds after your reply.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Step 1) Post all compiler/linker errors, in full [smile]


This bears re-iteration.

We cannot guess whats going wrong! [smile]

Again, at a glance setk() and showk() in derived class.cpp need to have derivedClass:: in front of the function name.

[Edit:]
okay, I see you've inlined the problem in the source file. The "derivedClass::" should fix that error. The reason is that the compiler does not know to associate the function setk() with the class derivedClass. The compiler wont guess, it needs to know. So all it sees is a new function, not attached to any class, that is trying to use a variable k without declaring it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this