base class calling derived class' functions
-
okay.. in my program i would like to have a structure like that
class base { public: void function1() { .... function2(); } virtual void function2 = 0; }; class derived1 : public base { public: void function2 { cout << "Function2 from derived1"; } }; class derived2 : public base { public: void function2 { cout << "Function2 from derived2"; } }; int main() { base* pBase = new derived1; base* pBase2 = new derived2; pBase->function1(); pBase->function2(); }
Any Ideas, thanks in advance, bernhard
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
U.S. Secretary of State Colin Powell at George Bush's ranch in Texas -
okay.. in my program i would like to have a structure like that
class base { public: void function1() { .... function2(); } virtual void function2 = 0; }; class derived1 : public base { public: void function2 { cout << "Function2 from derived1"; } }; class derived2 : public base { public: void function2 { cout << "Function2 from derived2"; } }; int main() { base* pBase = new derived1; base* pBase2 = new derived2; pBase->function1(); pBase->function2(); }
Any Ideas, thanks in advance, bernhard
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
U.S. Secretary of State Colin Powell at George Bush's ranch in TexasI cant see anyt problem in your code It's ok and should work... output:
Function2 from derived1 Function2 from derived1
what is your problem ???? any ideas for what ???? ------ The Server -
I cant see anyt problem in your code It's ok and should work... output:
Function2 from derived1 Function2 from derived1
what is your problem ???? any ideas for what ???? ------ The Serverit doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes..
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
U.S. Secretary of State Colin Powell at George Bush's ranch in Texas -
it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes..
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
U.S. Secretary of State Colin Powell at George Bush's ranch in TexasYou need to declare thye base class functions as virtual aswell as the derived class Asim Hussain e: asim@jawache.net w: www.jawache.net
-
it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes..
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
U.S. Secretary of State Colin Powell at George Bush's ranch in TexasBernhard wrote: it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes.. You just said it youself :-), this class is abstract, it has a pure virtual method, it can't be instantiated.
-
Bernhard wrote: it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes.. You just said it youself :-), this class is abstract, it has a pure virtual method, it can't be instantiated.
but is there something, which will do the job.. i want a function in the base class which calls a overloaded function from derived classes?
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
U.S. Secretary of State Colin Powell at George Bush's ranch in Texas -
it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes..
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
U.S. Secretary of State Colin Powell at George Bush's ranch in TexasFOR CRYING OUT LOUD:mad: IT F#$%ING WORKS !!!!:mad: file.h:
class cBase
{
public:
virtual void func1(){ func2(); }
virtual void func2()==0;
};class cDer1
{
public:
void func2() { printf("(2)It works!!!\n");}
};class cDer2
{
public:
void func2() { printf("(2)It works!!!\n");}
};file.cpp
#include "file.h"
#include
#include
void main()
{cBase * a= new cDer1();
cBase * b= new cDer2();a->func1();
b->func1();
a->func2();
b->func2();printf("IT F@#!@ WORKS");
getch();
}==================== === THE SERVER === ====================
-
FOR CRYING OUT LOUD:mad: IT F#$%ING WORKS !!!!:mad: file.h:
class cBase
{
public:
virtual void func1(){ func2(); }
virtual void func2()==0;
};class cDer1
{
public:
void func2() { printf("(2)It works!!!\n");}
};class cDer2
{
public:
void func2() { printf("(2)It works!!!\n");}
};file.cpp
#include "file.h"
#include
#include
void main()
{cBase * a= new cDer1();
cBase * b= new cDer2();a->func1();
b->func1();
a->func2();
b->func2();printf("IT F@#!@ WORKS");
getch();
}==================== === THE SERVER === ====================
it really works.. my problem was that i didn't declare both base class members as virtual, because in my real project the calling function was the constructor.. and the constructor can't be virtual (at least i think the constructor can't be virtual) thanks for your help (i found another workaround) bernhard (hope you didn't get too angry)
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
U.S. Secretary of State Colin Powell at George Bush's ranch in Texas