-1

Suppose I have the following code:

class A 
{
     public:
         void Funct1();
};

class B : public A
{
      public:
          void Func1();
          void Func2();
}

In main()

Declaring Object of type A

  A b = new B;
  b.Func1() // can access it;
  b.Func2() // cannot access

How can I make or access Func2 using object b especially in main

blackbird
  • 957
  • 12
  • 18
Plengo
  • 97
  • 1
  • 10
  • 3
    Start with code that compiles. `b` is of type `A`; not `A*` or `B*`. `Public` isn't valid either, and after you sort all of that out, read about `dynamic_cast<>` [here](http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast), and [here](http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used). – WhozCraig Apr 06 '14 at 07:28
  • you cannot assign a pointer (`new B`) to a non-pointer variable (`A`). – blackbird Apr 06 '14 at 07:30
  • Is it on purpose that you have `A::Funct1()`, instead of `A::Func1()`. – blackbird Apr 06 '14 at 07:33
  • Don't forget to declare `A::Func1()` as `virtual` or overriding it won't work properly. Specifically, if you correct the declaration of `b` to `A *b = new B();`, then `b->Func1()` will actually call `A::Func1()`, not `B::Func1()`. – Nicu Stiurca Apr 06 '14 at 07:37
  • Thanks i was suppose to declare as pointers i just forgot to type that in as making it A* b = new B. Suppose i did not want to put a virtual function func2 in A. I can easily solve the issue with that method. I meant for a case maybe you have 10 subclasses, are you going to virtualize every member functions even if some dont apply to other derived classes? – Plengo Apr 06 '14 at 07:57

5 Answers5

1

There are a few problems in your code.

  1. You are using Void instead of void. Notice the uppercase vs lowercase difference.
  2. You are using Funct1 in A but Func1 in B. You can change Funct1 to Func1
  3. There is a missing ; at the end of B.
  4. Using B instead of new B. You have:

    A b = new B;
    

    That is a syntactically incorrect line. You can make it either

    A b = B();
    

    or

    A* b = new B();
    

Even after that change, you still have the problems you described.

There are two ways you can solve this:

  1. Use B b instead A b.

    B b;
    b.Func1();
    b.Func2();
    
  2. Use a virtual member function.

     class A 
     {
          Public:
              void Func1();
              virtual void Func2();
     };
    
     class B : public A
     {
           Public:
               void Func1();
               virtual void Func2();
     };
    

    Then, you can use:

    B b;
    A* aPtr = &b;
    aPtr->Func1();
    aPtr->Func2();
    
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You may use static_cast

A *b = new B;
B *bb = static_cast<B*> (b);

through bb you can access Func2().

Tahlil
  • 2,680
  • 6
  • 43
  • 84
  • 1
    This is usually a sign of design issues in the inheritance hierarchy. It would be better to use dynamic_cast because b can hold any objects which inherit from A, not only B objects. dynamic_cast can check if this conversion works. – Jens Apr 06 '14 at 07:42
  • Thanks that works. After lot googling i came across this B* bb = (B*) A; – Plengo Apr 06 '14 at 08:24
0

Write a function which gets argument of Type A. Then you can send an argument of type A or B to this function,polymorphism works and the function inside class A or B works wrt what you want.

oiyio
  • 5,219
  • 4
  • 42
  • 54
0

In a statically typed language like C++, you can only call methods corresponding to the static type of an object in the current scope. In main, you declared b with static type A, restricting it to the interface defined in class A. If you want polymorphism, you need to declare the polymorphic functions in A and also add the virtual keyword.

Jens
  • 9,058
  • 2
  • 26
  • 43
0

If object-oriented programming doesn't make sense for the problem you are trying to solve, don't use it. In C++, you don't need to put everything in classes, let alone in class hierarchies. Start with simple free-standing functions and use classes if you need them.

That being said, if you really insist, use dynamic_cast. In some situations, this may even be a legitimate choice. But if you feel that you need it all the time, then you must review your understanding of object-oriented programming.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62