Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I have these :

C#
#include <iostream>
using namespace std;

class A;

class A {
public:
    virtual void foo() { }
};

class C : public A {
public:
    virtual void foo() { cout << "abc?" << endl; }
};

int main()
{

    A aObj;
    aObj = C();
    aObj.foo();
}


but I want after compile I see " abc? " . but I dont see any thing. What should I do ? And where I have a mistake ?
Thanks
Posted

gghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjghghjfg hg
 
Share this answer
 
Hi,

Have a look at this piece of code:
C++
A aObj;
aObj = C();
aObj.foo();

aObj is, even because you call C(); and not A();, an instance of A. And in A, the method foo is empty. I did this test:
C++
#include <iostream>
using namespace std;
 
class A;
 
class A {
public:
    virtual void foo() { }
};
 
class C : public A {
public:
    virtual void foo() { cout << "abc?" << endl; }
    int i;
};
 
int main()
{
 
    A aObj;
    aObj = C();
    aObj.i = 0;
    aObj.foo();
}

I got a compiler error 'class A' has no member named 'i' So, I'm sure aObj is an instance of A You can try
C++
C aObj;
aObj = C();
aObj.foo();

instead of:
C++
A aObj;
aObj = C();
aObj.foo();

Hope this helps.
 
Share this answer
 
Comments
mohammadali1375 31-Dec-12 9:34am    
Oh yea. I dont pay attention to this. Thanks for help. Now I know correct form of this , is like this :

A* aObj;
aObj = new C();
aObj->foo();

now i see " abc? "

Thanks alot for your help
Thomas Daniels 31-Dec-12 9:35am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900