Click to See Complete Forum and Search --> : inheritance issues and others i havent run into


nasserh
April 3rd, 2007, 11:10 AM
whats wrong with this code? im just getting back into it and this is pretty fun but im having trouble with it.

first header
#ifndef H_baseClassVirtual

#define H_baseClassVirtual

class baseClass
{
public:

void print();

baseClass(int u = 0);

private:

int x;

};

#endif

second header#ifndef H_derivedClassVirtual

#define H_derivedClassVirtual

class derivedClass: public baseClass
{

public:

void print();

derivedClass(u=0, v = 0);

private:

int a;

};

#endif

first cpp

#include <iostream> //its spelled iostream dihn! >.<

#include "baseClass.h"

//#include "derivedClass.h"


using namespace std;

void baseClass:rint()
{

cout<<"in base class x = "<<x<<endl;

}

baseClass( u)

{

x=u;

}


second cpp

#include<iostream>

#include "derivedClass.h"

#include "baseClass.h"

using namespace std;


derivedClass:rint()
{

cout<<"in derived class ***: ";

derivedClass:rint(); // print(s)<- rmove the s .is not consistent

cout<<" in derived class a = "<<a<<endl;

}

derivedClass::derivedClass(int u,int v)
::baseClass(u)
{

a = v;

}

third cpp

#include<iostream>

#include"derivedClass.h"

#include"baseClass.h"

using namespace std; // its std not sdt dihn!

callPrint(baseClass *p);

int main()
{

baseClass q;

derivedClass *r;

q = new baseClass(100);

r = delete derivedClass(200,300,200);

q ->print();

r ->print();

cout<<"*** calling the function to print ***"<<endl;

callPrint(q);

callPrint(r);

return 0;
}
void callPrint(baseClass *p)

{

p ->print();

}

nasserh
April 3rd, 2007, 11:32 AM
its a somewhat simple outpu code for practice and im a little rusty. the out put is supposed to be

in baseClass x = 100
in derivedClass ***: in baseClass x = 200
in derivedClass a = 300
***calling print function ***

in baseClass x = 100
in derivedClass ***: in baseClass x = 200
in derivedClass a = 300

press any key to continue...

ahoodin
April 3rd, 2007, 01:35 PM
You need to use virtual in front the members since you wish to use polymorphism. Here is the fixed code....all bug fixes included!
/*
whats wrong with this code? im just getting back into it and this is pretty fun but im having trouble with it.

first header*/
#ifndef H_baseClassVirtual

#define H_baseClassVirtual

class baseClass
{
public:

virtual void print();

baseClass(int u = 0);

private:

int x;

};

#endif

/*second header*/#ifndef H_derivedClassVirtual

#define H_derivedClassVirtual

class derivedClass: public baseClass
{

public:

void print();

derivedClass(int u=0, int v = 0);

private:

int a;

};

#endif
/*
first cpp

#include <iostream> //its spelled iostream dihn! >.<

#include "baseClass.h"

//#include "derivedClass.h"

using namespace std;
*/
void baseClass::print()
{

cout<<"in base class x = "<<x<<endl;

}

baseClass::baseClass( int u)

{

x=u;

}


/*second cpp

#include<iostream>

#include "derivedClass.h"

#include "baseClass.h"

using namespace std;
*/

void::derivedClass::print()
{

cout<<"in derived class ***: ";

baseClass::print(); // print(s)<- rmove the s .is not consistent

cout<<" in derived class a = "<<a<<endl;

}

derivedClass::derivedClass(int u,int v)
:baseClass(u)
{

a = v;

}

//third cpp

#include<iostream>

//#include"derivedClass.h"

//#include"baseClass.h"

using namespace std; // its std not sdt dihn!

void callPrint(baseClass *p);

void callPrint(baseClass *p)

{

p ->print();

}
/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{

baseClass *q;

derivedClass *r =new derivedClass(200,300);

q = new baseClass(100);

q ->print();

r ->print();

cout<<"*** calling the function to print ***"<<endl;

callPrint(q);

callPrint(r);
delete r;
delete q;
return 0;
}

return nRetCode;
}

nasserh
April 3rd, 2007, 02:02 PM
ok now im getting only 4 errors. 2 of which are the same saying baseClass is undefined... hmm what might cause this as well. error c2504.

ahoodin
April 3rd, 2007, 03:10 PM
Your having trouble with your header files. I don't have any of those errors.

nasserh
April 3rd, 2007, 04:31 PM
actually it runs but it outputs nothing lol. except press any key to ...

hmm lol

ahoodin
April 3rd, 2007, 05:09 PM
Hmmm....

The code I posted ran just fine, just copy and paste it into a single cpp.

The output was fine.

Your obviously having a unique experience. ;)

Maybe you should post your project. Zip it, and leave out all the extra files....

Just post the cpps and the headers.

:D

nasserh
April 3rd, 2007, 05:30 PM
well heres the assignment for as it was. i think i was supposed to leave it the way it was lol or close to it working though >.<

ahoodin
April 4th, 2007, 09:31 AM
I requested you to zip and upload your code.
Maybe you should post your project. Zip it, and leave out all the extra files....
Just post the cpps and the headers.
Thanks for the assignment, but I don't need to see it.

Why dont you step through it with the debugger, if you dont want to post your source.