1

I was wondering if the following was possible:

class foo
{
  virtual void bar();
}

class izy : public foo
{
 int a;
 virtual  void bar()
 {
  a = 2;
 }
}

foo *anyfoo = new izy;
anyfoo.bar();

essentially what I want to know is, can I add the variable a or will a be nonexistant since its not part of the base class foo?

phimuemue
  • 34,669
  • 9
  • 84
  • 115
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 4
    In you example you cannot call `bar` because a) it's private & b) your using the wrong syntax for calling methods through pointers to objects... – Eugen Constantin Dinca Jul 24 '10 at 18:52
  • [Get a book and find out](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). :) – GManNickG Jul 24 '10 at 18:53
  • The title of the question is weak, generic, and poorly formulated. Titles like this get the most attention of the community. Nicely done! More than 60 views in less then 20 minutes. – karlphillip Jul 24 '10 at 19:06

6 Answers6

3

yes, you can add new Variables and Methods in subclasses.

mhshams
  • 16,384
  • 17
  • 55
  • 65
1

Inheritance allows new variables in derived classes. What you are trying to achieve is polymorphism, which is prevented in this case by using a pointer to the base class type.

JustJeff
  • 12,640
  • 5
  • 49
  • 63
1

In your code you will call foo::bar(). To call a function which will modify a (izy::bar) you should make bar virtual.


After you've made bar virtual your code will change a if you'll move bar to the public section. anyfoo is points to the instance of izy class, which contains a.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
1

With the code you have, it will not work as you want because foo's bar will be called, not izy's bar. If you change bar to be virtual, it will work as intended. a only existing in the izy class is not a problem.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • but the instance is izy instance. it will override the orginial method. – mhshams Jul 24 '10 at 18:50
  • 2
    @mohammad: But the pointer has the type `foo*`, which removes anything about `izy` from the equation. If the function were marked `virtual`, you'd get polymorphic behavior as intended. EDIT: And of course the question is now edited to include virtual. – GManNickG Jul 24 '10 at 18:51
  • 1
    @mohammadshamsi: Only virtual functions are overridden. If you define two non-virtual functions with the same name, you don't get polymorphism, you just get two functions with the same name. If you access a non-virtual function through a base-class pointer, you'll call the base-class's function. – sepp2k Jul 24 '10 at 18:55
0

Yes you can add new property like functions and variables.Inheritance allows you to use public and protected properties of parent class. As well can over read those properties as you did for the bar() function.

Thanks

Muhit
  • 789
  • 1
  • 7
  • 17
0

can I add the variable a or will a be nonexistant since its not part of the base class foo?

What the other answers haven't quite said yet: your variable isn't a foo, you have a foo*. It's a pointer to some other area of memory of indeterminate size. So you're not restricted to storage size of a foo so yes you can have any amount of extra storage.

If you allocated your variable as a foo with foo storage you can't shoehorn in more, i.e.

foo anyFoo;
((bar*)(&anyFoo))->bar();

won't work.

Rup
  • 33,765
  • 9
  • 83
  • 112