Link to home
Start Free TrialLog in
Avatar of yolis
yolis

asked on

Inhertiance and constructoers..

when i try to run this code:
#include <iostream.h>
#include <string.h>

class car{
char company_name[10];
int model,engine;
long price;
public:
car(char *Icompany_name,int Imodel,long Iprice,int Iengine)
{
  strcpy(company_name,Icompany_name);
  model=Imodel;
  price=Iprice;
  engine=Iengine;
}
void show()
{
  cout<<"Showing paramaters of class car\n"
  "Company name: "<<company_name<<
  "\nModel number: "<<model<<
  "\nPrice: "<<price<<
  "\nEngine capacity: "<<engine<<'\n';
}
int get_model() { return(model); }
int get_price() { return(price); }
int get_engine() { return(engine); }
char *get_company_name() { return(company_name); }
};

class car1:public car{
char color[10],land[20];
int maxspeed;
public:
car1(char *Icolor, char *Iland, int Imaxspeed)
: car (*Icompany_name,Imodel,Iprice,Iengine)
{
  strcpy(color,Icolor);
  strcpy(land,Iland);
  maxspeed=Imaxspeed;
}
void show()
{
  cout<<"Showing paramaters of class car\n"
  "Company name: "<<get_company_name()<<
  "\nModel number: "<<get_model()<<
  "\nPrice: "<<get_price()<<
  "\nEngine capacity: "<<get_engine()<<
  "\nColor: "<<color<<
  "\nLand: "<<land<<
  "\nMaxSpeed: "<<maxspeed<<'\n';
}
};


main()
{
  car bla("Yoli co.",554,10100,800);
  bla.show();
  return 0;
}

it gives me this error messages:
'Undefined symbol 'Icompany_name'
'Undefined symbol 'Imodel'
'Undefined symbol 'Iprice'
'Undefined symbol 'Iengine'
'Could not find a match for 'car::car(undefined,undefined,undefined,undefined)'
Avatar of yolis
yolis

ASKER

oh, all the error messages reffer to the line:
car1(char *Icolor, char *Iland, int Imaxspeed)
    : car (*Icompany_name,Imodel,Iprice,Iengine)


thanks..

ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
in the 2nd line of

car1(char *Icolor, char *Iland, int Imaxspeed)
   : car (*Icompany_name,Imodel,Iprice,Iengine)

you are trying to construct the "car" portion of a car1 object.  to do this you must pass the information that the car constructor need, like the company name, model, price, and engine.  However, you don't have that information to pass.  You pass lcompany_name for the company name, but you don't have lcompany_name defined.  Same with the others.  Does that make sense?  One way to make this work is to pass the company name and other information to the car1 constructor, like

car1(char *Icompany_name,int Imodel,long Iprice,int Iengine,char *Icolor, char *Iland, int Imaxspeed)
   : car (*Icompany_name,Imodel,Iprice,Iengine)
{
     strcpy(color,Icolor);
     strcpy(land,Iland);
     maxspeed=Imaxspeed;
   }

Now when you construct a "car1", you have to specify the 5 parameters that a "car" requires as well as the 4 extra parameters that a "car1" requires.  Does that make sense?

By the way, another solution would be to create a default constructor for the car class.  Then car1 could leave the car information default constructred.  This might or might not be a useful solution for you, I can't tell from what I've seen.
Is this working for you?
Avatar of yolis

ASKER

ALRIGHT!
thanks a lot!!!