Click here to Skip to main content
12,076,206 members (49,525 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
Hi.

I have these :

#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 31-Dec-12 4:12am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Have a look at this piece of code:
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:
#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 aObj;
aObj = C();
aObj.foo();
instead of:
A aObj;
aObj = C();
aObj.foo();
Hope this helps.
  Permalink  
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
ProgramFOX 31-Dec-12 9:35am
   
You're welcome!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

gghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjghghjfg hg
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160208.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100