hi! i have a class from which other classes are inhertited. I then create a function GetInput() & I want it to funtion for evry object inherited by MAIN. I don't want to create separate functions for each. Is there any way for it?

Thank you.

Recommended Answers

All 7 Replies

The way you describe your situation is a little confusing. You'll have to show us what you have so that we can understand your description.

It sounds like you want to define the function once and have it valid for every child class without re-defining it. To do that, you would define it in the base/parent class instead of the inherited/child classes. The only issue though, is that if you do that, the function will only have access to the members of the base/parent class and will not have access to the members of the inherited/child class. You will wind up with an identical function within every object. The function will not adapt to the type of the object, it will only produce results based on the data contained within it. For example, if you have a base class "shape" and child classes "circle" and "square" and define "getArea()" within the base class the area will be calculated the same every time instead of modifying the equation to fit the shape.

class parent {
  public:
    virtual someFunction() { cout << "in someFunction()" << endl; }
};

class child1 : public parent {
}

class child2 : public parent {
}

int main() {
  parent *pParent1 = new child1;
  parent *pParent2 = new child2;

  /* ... etc ... */

  pParent1->someFunction(); //call child1::someFunction() (is actually parent::someFunction())
  pParent2->someFunction(); //call child2::someFunction() (is actually parent::someFunction())

  /* ... etc ... */

  return 0;
}

Polymorphism to the rescue!

#include <iostream>

class Base {
public:
    // Edward believes in abstract base classes
    virtual void method() = 0;
};

class Child1: public Base {
public:
    virtual void method() { std::cout << "Child1!\n"; }
};

class Child2: public Base {
    virtual void method() { std::cout << "Child2!\n"; }
};

void function(Base& obj)
{
    // Use the virtual interface to engage polymorphism
    obj.method();
}

int main()
{
    Child1 c1;
    Child2 c2;

    function(c1);
    function(c2);
}

Polymorphism to the rescue!

This does appear to be a question about poly, but your example isn't what the OP seems to be looking for. It appears that they don't want to define the function more than once. Your example defines it once for each derived class.

I'd be using fbody's class for this.

class parent {
  public:
    virtual void myFunc() = 0 ;
};

class child1 : public parent {
  public:
   void myFunc(){cout<<"Do stuff";}
};

class child2 : public parent {
  public:
   void myFunc(){cout<<"Do more stuff";}
};

void GetInput(parent *ptr)
{
  ptr->myFunc();
}

So you can just call it from main().

int main()
{
  chidl1 Obj1;
  child2 Obj2;
  GetInput(&Obj1);
  return 1;
}
commented: thx +1

> They don't want to define the function more than once. Your example defines it once for each derived class.
If you were confused by it, then Edward's example failed completely. Apologies. It was meant *only* to show how to use polymorphism, not an attempt at a realistic solution to the OP's specific requirements. In the real GetInput(), Ed would expect a shared algorithm to use the virtual interface for customizing parsing.

void GetInput(Base& obj)
{
    std::string token;

    obj.open();

    while (obj.incomplete() && std::cin >> token)
        obj.add(token);

    if (obj.incomplete())
        throw std::runtime_error("Incomplete on close");

    obj.close();
}

Without details on what the objects do and what GetInput() is supposed to do, a realistic example is impossible. So Ed did not even try.

Thank you very much nbaztec! this is exactly what I wanted. Also thank you Fbody & Edward. sorry if i wasn't clear.

> They don't want to define the function more than once. Your example defines it once for each derived class.
If you were confused by it, then Edward's example failed completely. Apologies. It was meant *only* to show how to use polymorphism, not an attempt at a realistic solution to the OP's specific requirements. In the real GetInput(), Ed would expect a shared algorithm to use the virtual interface for customizing parsing.

void GetInput(Base& obj)
{
    std::string token;

    obj.open();

    while (obj.incomplete() && std::cin >> token)
        obj.add(token);

    if (obj.incomplete())
        throw std::runtime_error("Incomplete on close");

    obj.close();
}

Without details on what the objects do and what GetInput() is supposed to do, a realistic example is impossible. So Ed did not even try.

Oh... I see what you did now... I completely missed it... :$

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.