0
class A{
public:
    void getdata();
};
class B: public A{
public:
    int x,y,z;
};
class C: public A{
public:
    int x,y,z;
};

//here obj could be of class b or class c
void A::getdata(){//to input values of x,y,z
    std::cin>>obj.x;
    std::cin>>obj.y;
    std::cin>>obj.z;
}

How can I Pass object of different class(here class B and class C) to the same getdata() function to input values of x,y,z?

Humble
  • 39
  • 9
  • 1
    What is the real problem you're trying to solve? No, not one aobut using one method for different subclasses, but the one to which you believe this is the solution, and that's what you're asking about. This is not the right design for whatever is attemped to be done here. If you can explain the real problem, perhaps someone will suggest a better class design to implement it. – Sam Varshavchik Sep 08 '20 at 05:16
  • 1
    _@Humble_ In case you just missed that what you want is very easy and common using the `virtual` keyword see the 3rd duplicate link please. – πάντα ῥεῖ Sep 08 '20 at 06:01
  • 1
    `template void getdata(T &&obj);` – bipll Sep 08 '20 at 06:06

0 Answers0