GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Apr-2005, 18:19
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

inheritance problem


hello everyone. Sorry for being away for a while, the new job is a killer.
here i am now with a little bit of an inheritance problem. Actually it's more of a constructor problem.
i am trying to intialize a private const object in the constructor, but no matter what format i use to try to do so, ms visual studio still rejects it.
CPP / C++ / C Code:
Vehicle::Vehicle( const int doors, const int cylinders, 
   string color, double initialFuel, const int transmission ){

	Vehicle::numberOfDoors = doors;
	Vehicle::numberOfCylinders = cylinders;
	Vehicle::transmissionType = transmission;
	Vehicle::fuelLevel = initialFuel;
	Vehicle::vehicleColor = color;
	 


   
 
} // end class Vehicle constructor

in case someone needs to take a peek at the class itself, here it is.

CPP / C++ / C Code:
#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>

using std::ostream;

#include <string>

using std::string;

// class Vehicle definition
class Vehicle {
   friend ostream& operator<<( ostream &, const Vehicle & );

public:
   Vehicle( const int, const int, string, double, const int ); 
   
   void setColor( string );
   string getColor() const;

   void setFuelLevel( double );
   double getFuelLevel() const;

   void setClassName( string );
   string getClassName() const;

   int getTransmissionType() const;
   int getNumberOfDoors() const;
   int getNumberOfCylinders() const; 

private:
   const int numberOfDoors;
   const int numberOfCylinders;
   string vehicleColor;
   double fuelLevel;
   const int transmissionType;
   string className;

}; // end class Vehicle

#endif // VEHICLE_H

what do u recommend?
  #2  
Old 07-Apr-2005, 19:49
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
numberOfDoors, numberOfCylinders, and transmissionType are declared const. you can't make assignments to const variables, and this extends to class members. so either remove the const attribute from those variables, or use a member initialization list. they are the only way to initialize const members, but as a small tradeoff they make your constructor a little more cryptic. using an initialization list instead of assignments, your constructor would be rewritten like this:

CPP / C++ / C Code:
Vehicle::Vehicle( const int doors, const int cylinders, 
   string color, double initialFuel, const int transmission ) : numberOfDoors(doors), numberOfCylinders(cylinders), transmissionType(transmission), fuelLevel(initialFuel), vehicleColor(color)
{

/* nothing to do here because the initialization list takes care of it.
 * as you can see, it is a comma-delimited list of the class member variables,
 * with their initial values in parentheses. */

} // end class Vehicle constructor


your other problem is that you were trying to assign the member variables as if they were static. (declaring a class member variable static means that there is only one instance of the variable for all objects of the class. this can be useful, but I am almost positive it is not what you want.) anyways, non-static class member variables are accessed in the class's member functions in one of two ways:
(1) as if they were global variables. (i.e. just use them)
(2) through the this pointer (i.e. this->myVar). the this pointer is a parameter that the compiler adds to every class member function. it is transparent to the programmer, but it is what allows classes to be compiled back into regular C. (in other words, the class is separated into a structure containing the variables and a set of functions with their names mangled like __class___m_func_whatever(), that all receive a pointer to the structure they are being called on as the this pointer).
  #3  
Old 07-Apr-2005, 21:31
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

thanks a bunch


that really helped, thank you. no it was not my desire to make those objects static, but it is the book's desire...
i really appreciate it.
incidently, i created another class called "Truck" to inherit from class vehicle, but as i created the constructor for the class, as follows....
CPP / C++ / C Code:
Truck::Truck( bool c )
{
        c = cargo;
}
the compiler generates the following error
truck.cpp(6) : error C2512: 'Vehicle' : no appropriate default constructor available

if anyone can help and need to take a look at class vehicle it is posted before.
  #4  
Old 07-Apr-2005, 21:42
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
I would have probably have to see the Truck class source code, but my guess is you will have to add a
CPP / C++ / C Code:
Vehicle();
to the Vehicle class, that either does nothing or sets up default values for the member variables.
  #5  
Old 07-Apr-2005, 21:59
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

class Truck


Quote:
Originally Posted by ubergeek
I would have probably have to see the Truck class source code, but my guess is you will have to add a
CPP / C++ / C Code:
Vehicle();
to the Vehicle class, that either does nothing or sets up default values for the member variables.

CPP / C++ / C Code:
#ifndef TRUCK_H
#define TRUCK_H

#include <iostream>

#include "vehicle.h"

class Truck: public Vehicle {
	friend ostream& operator<<( ostream &, const Truck & );

public:
   Truck( bool );
   
   bool hasCargo() const;
   void Truck::setCargo( bool ); 
private:
	bool cargo;

};
#endif

that is the truck class, and i also tried adding Vehicle():\; to class Vehicle wich stopped the compiler from generating a syntax error, but when i try to build the program it shows this
truck.obj : error LNK2001: unresolved external symbol "public: __thiscall Vehicle::Vehicle(void)" (??0Vehicle@@QAE@XZ)
  #6  
Old 08-Apr-2005, 04:41
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Hi.

You'll have to implement your default constructor in the .cpp file.

CPP / C++ / C Code:
Vehicle::Vehicle()
{
//...
}

When you build an object of class Vehicle, the default constructor will be called (unless you specify otherwise), and the compiler can't find it's definition.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #7  
Old 08-Apr-2005, 14:46
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
LuciWiz is right, although if you don't want to take up that space in the .cpp file you can just define the def. constructor in the class definition (in the .h file):
CPP / C++ / C Code:
//vehicle.h
class Vehicle
{
Vehicle() { } //default constructor, does nothing
...
};
in fact you can define any class function like that, inside the class. the brackets don't have to be empty; it is just like a regular function definition. however, long functions are generally defined outside the class for a couple reasons:
(1) defining them inside the class like that makes the class definition harder to read
(2) they are implicitly inline functions when defined inside the class. that means that whenever they are called, the code is simply inserted into the calling function. so if the function is long and involved, this will slow down the program. but for short functions, it can actually add speed because branching to a function causes a small amount of overhead.
Last edited by LuciWiz : 08-Apr-2005 at 15:45. Reason: Fixed broken [c++] tag
  #8  
Old 09-Apr-2005, 13:59
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

thank you so much guys


you guys were really helpful, thank you so much
  #9  
Old 11-Apr-2005, 12:24
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

now main problem


i have created a main function to test the program and Vehicle and Taxi objects are running fine, but for some reason the constructor for class Truck is not being accessed.

this how my main looks like
CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::endl;

#include "vehicle.h"
#include "taxi.h"
#include "truck.h"

int main()
{
   Vehicle car( 2, 6, "blue", 14.6, 3 );
   Taxi cab( 3.3 );
   Truck mack( 7.54 );

   /* Write code to indicate that mack is carrying cargo */
   
   cout <<"the car object has the following features\n"<<car<<endl;
   cout <<"the taxi object has the following features\n"<<cab<<endl;
   cout <<"the truck object has the following features\n"<<mack<<endl;
   
   return 0;

} // end main

class truck is as follows
CPP / C++ / C Code:
#ifndef TRUCK_H
#define TRUCK_H

#include <iostream>

#include "vehicle.h"

class Truck: public Vehicle {
	friend ostream& operator<<( ostream &, const Truck & );

public:
   Truck( double );
   
   bool hasCargo() const;
   void Truck::setCargo( double ); 
private:
	double cargo;

};
#endif

with the follwoing defenition

CPP / C++ / C Code:
#include "truck.h"
using namespace std;
Truck::Truck( double c ) : cargo(c)
{
       cout <<"la la"<<c; 
}

// function hasCargo definition
bool Truck::hasCargo() const
{
   
	   return false;

} // end function hasCargo

// function setCargo definition
void Truck::setCargo( double c ) 
{
   cargo = c;

} // end function setCargo

// function operator<< definition
ostream &operator<<( ostream &output, const Truck &t )
{
   output << t.getClassName() << "\n"
          << "\tNumber of doors: " 
          << t.getNumberOfDoors()
          << "\n\tNumber of cylinders: " 
          << t.getNumberOfCylinders()
          << "\n\tTransmission type: " 
          << t.getTransmissionType()
          << "\n\tColor: " << t.getColor()
          << "\n\tFuel level: " 
          << t.getFuelLevel() << "\n";

   if ( t.cargo )
      output << "\tThe truck is carrying cargo.\n";

   else
      output << "\tThe truck is not carrying cargo.\n";

   return output;

} // end function operator<<

i am getting weird numbers for an output and looks clearly that the constructor has not ben initiated.

can anyone advice me?
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache / PHP problem, maybe output length? HaganeNoKokoro Apache Web Server Forum 3 07-Aug-2008 05:42
Please help! Insert into database problem robsmith MySQL / PHP Forum 1 24-Apr-2005 04:44
Inheritance problem CaptnB C++ Forum 2 27-Jan-2005 09:09
File Serialize's problem stanely MS Visual C++ / MFC Forum 2 08-Dec-2004 10:54
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:39.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.