By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

How to pass parameters to base class and call its function?

100+
P: 151
Hi,

I am working on some pblm.I need pass the parameters to base class and call the functions of derived classes.

Expand|Select|Wrap|Line Numbers
  1. Class A
  2.   public:
  3.   int x;
  4.   char y[3];
  5.   A(_x,_y)
  6.   {
  7.     x = _x;
  8.     strcpy(y,_y);
  9.   } 
  10.   int getX()
  11.   {
  12.      return X;
  13.   }  
  14.   char* getY()
  15.   {
  16.       return Y;
  17.   }
  18. };
  19. class B: public A  
  20. {
  21.    public:
  22.    int calculate()
  23.    {
  24.          return 4*getX();
  25.    }
  26. };
  27. class C: public A
  28. {
  29.    public:
  30.  
  31.    char* getMyname()
  32.    {
  33.        return Y; 
  34.    } 
  35. }
  36. int main()
  37. {
  38.    A(4,"Monika");  
  39.     // How to call class C and B functions without using abstract class
  40.   return 0;  
  41. }
Thanks in advance.
Nov 25 '10 #1
Share this Question
Share on Google+
8 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
There are no class C or B functions when you are using an A object.
Nov 25 '10 #2

100+
P: 151
How to use class B and C functions so that they can access the data of classs A?
Nov 26 '10 #3

Banfa
Expert Mod 5K+
P: 8,916
You would need to start by instantiating an object of type B or C
Nov 26 '10 #4

100+
P: 151
Can i all pass the value to constructor of base class by creating object B or C?
Nov 26 '10 #5

Banfa
Expert Mod 5K+
P: 8,916
Yes you can call specific parent object constructors the same way you call class initialisers

Expand|Select|Wrap|Line Numbers
  1. class MyClassA
  2. {
  3. private:
  4.   std::string name;
  5.   int value;
  6.  
  7. public:
  8.   MyClassA(int iv) :
  9.     name("ClassA"),
  10.     value(iv)
  11.   {
  12.   }
  13. };
  14.  
  15. class MyClassB : public MyClassA
  16. {
  17. private:
  18.   std::string name;
  19.  
  20. public:
  21.   MyClassB(int iv) :
  22.     MyClassA(iv),
  23.     name("ClassB")
  24.   {
  25.   }
  26. };
  27.  
Nov 26 '10 #6

100+
P: 151
Thanks for answer
My pblm is bit more complex.
I have one base class which has 30 parametrs and 10 funtions using and manipulating the data. I have 10 derived class which uses the base class data and functions. If i want to use any derived class then i need to set the all parameters of base class using derived class constructor for every class. Is there any way by which i can set the value of base class? All derived classes can use them whenever needed.
Nov 27 '10 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
You may have a design problem.

A base class contains data that is common to all derived classes. Therefore, every derived class must pass the 30 parameters.
Nov 28 '10 #8

Banfa
Expert Mod 5K+
P: 8,916
Well the base class can initialise its own parameters of course in the same way if the derived classes only need to pass a few parameters to it for the specific instantiation.

Like MyClassA::name in my previous example.
Nov 28 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.