Click here to Skip to main content
Rate this: bad
good
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 3: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 at 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 at 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 355
1 Abhinav S 333
2 sanket saxena 305
3 CPallini 205
4 thatraja 195
0 Sergey Alexandrovich Kryukov 8,497
1 OriginalGriff 4,850
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


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