0
#include <iostream>
using namespace std;

class bike {
  public:
		bike ()
		{
			cout << "Bike: no parameters\n";
		}
		bike (int a)
		{
			cout << "Bike: int parameter\n";
		}
		void color(int a,int b)
		{
			cout<<"Inside Bike Color"<<endl;
		}
};

class ducati : public bike {
  public:
		ducati (int a)
		{
			cout << "Ducati: int parameter\n\n";
		}
		void color(int a,int b,int c)
		{
					cout<<"Inside Ducati Color"<<endl;
		}
	};

class honda : public bike {
  public:
	  honda (int a) : bike (a)
      {
    	cout << "honda: int parameter\n\n";
      }

};

int main ()
{
  ducati monster(0);
  ducati *dObj=new ducati(10);
  bike *bObj=&monster;
  bObj->color(10,20,30); //it's showing error at this line. 

  return 0;
}

Please help me out

Edited by Narue: Added code tags

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Narue
0
void color(int a,int b)

Two parameters.

bObj->color(10,20,30);

Three arguments.

I see what you wanted to happen, but it won't work because ducati's version of color() is an overload rather than an override. When looking at the object through a pointer to bike, you can only see the public member functions declared in bike. Since the three parameter version of color() is added in ducati, it's not visible from a pointer to bike even if that pointer points to a ducati object.

You can achieve something similar with judicious constructor parameters, virtual member functions, and polymorphism:

#include <iostream>

using namespace std;

class bike
{
public:
    bike() {
        cout << "bike()\n";
    }
    bike(int a) {
        cout << "bike(int a)\n";
    }
    virtual void color(int a, int b) {
        cout<<"Inside Bike Color"<<endl;
    }
};

class ducati : public bike
{
public:
    ducati(int a): bike(a) {
        cout << "ducati(int a)\n";
    }
    ducati(int a, int c): bike(a) {
        cout << "ducati(int a, int c)\n";
    }
    virtual void color(int a, int b) {
        cout<<"Inside Ducati Color"<<endl;
    }
};

int main()
{
    ducati monster(0, 30);
    bike *bObj = &monster;
    
    bObj->color(10, 20);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.