Thread: Issue creating objects using composition and inheritance

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    22

    Post Issue creating objects using composition and inheritance

    Hi, I'm trying to use both composition and inheritance as part of a learning project. I have a class Vehicle which uses composition to create objects of type date and insurance as part of its type. This works up to here, but when i try to create an object of type Car which inherits from vehicle I get a double free or corruption (fasttop) error. Any direction would be appreciated.

    My constructor for vehicle is as follows, works fine up to here.

    Code:
    Vehicle::Vehicle(const char *model, const char *brand, const char *color, const char *usageStatus,
                           int year, int totalPass, int plateNum, float dayRate, const Insurance &ins,
                           const Date &sDate):lastServiceDate(sDate), insurance(ins)
    

    My issue is using inheritance, my constructor is as follows:

    Code:
    Car::Car(const char * model, const char * brand, const char * color, const char *usageStatus,
     int year, int totalPass, int plateNum, float dayRate, const Insurance &ins, const Date &sDate,bool sports,bool convertible):Vehicle(model, brand, color ,usageStatus, year, totalPass, plateNum, dayRate, ins, sDate){
     setIsSports(sports);
    setIsConvertible(convertible);
    }
    
    Appreciate any guidance given.

    Thanks
    Last edited by ubmattpangolin; 02-23-2016 at 04:52 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should post the definitions of the Vehicle and Car classes.

    Why are you not using std::string? My guess is that you have your "double free or corruption (fasttop) error" because you are fiddling around with manual memory management when you should be using string classes, containers, and smart pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    22
    Hey laserlight,

    I found the issue... When i removed the delete section in my Insurance destructor in the insurance class that defined the insurance type like this...

    Code:
    Insurance::~Insurance(){ //I removed this   delete[]insuranceType;}
    The error stop, though I believe that this would be essential if my insurance type is of type char * insuranceType; The way I created this was through the following function.
    Code:
    voidInsurance::setInsuranceType(constchar*m){insuranceType=newchar[strlen(m)+1];strcpy(insuranceType,m);}
    


    Are you suggesting I use string insuranceType instead of char * insuranceType ??


  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ubmattpangolin
    Are you suggesting I use string insuranceType instead of char * insuranceType ??
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Linked List using inheritance or composition?
    By ubmattpangolin in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2015, 12:22 PM
  2. objects and classes in inheritance
    By student111 in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2012, 07:10 AM
  3. Inheritance of objects and linked list
    By rakeshkool27 in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2010, 12:39 PM
  4. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM
  5. inheritance vs composition
    By razrektah in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 11:13 AM