Thread: My code is wrong? It doesn't work! Plz Help.

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    5

    Question My code is wrong? It doesn't work! Plz Help.

    Professor gave me a homework and I coded.
    But it does not work!! :q......
    I can't tell what to do... There are no errors.
    (Please understand my bad English..)

    -It is my homework
    ◆Homework
    UML, main(), refer to execution result, make out a code for Car class and CarList class.

    ◆Demand
    ● Car class' member variables are private
    -maker : name of manufacturing company (char*)
    -passengers : maximum passengers' number (int)
    -fuelcap : fuel tank's size (int)
    -efficiency : fuel efficiency (float)
    ● default constructor/constructor with variable/copy constructor/destructor of Car class.
    -default constructor : allocate 'null' to 'make' and '0' to the rest
    -constructor with variable : allocate input parameter to member variable. Also, 'maker' must be allocated after dynamic allocation.(deep copy)
    -copy constructor : allocate all member variables of input parameter to own member variables. Also, 'make' must be allocated after dynamic allocation(deep copy)
    -destructor : check 'make' is 'null'. If it is not 'null', deallocate dynamic memory.
    ● printStatus() : Print all member variables.
    ● Access to member function with accessor function(set, get)
    ● getRange() : return the car's maximum travel range(fuelcap * efficiency)
    ● CarList class' member variables are private
    ● insert() : add parameter to the end of member variable list (member variable list is vector form)
    ● print() : print all component of member variable list (member variable list is vector form)

    My code is wrong? It doesn't work! Plz Help.-11-png
    My code is wrong? It doesn't work! Plz Help.-22-png
    My code is wrong? It doesn't work! Plz Help.-33-png

    -This is my code
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    
    using namespace std;
    
    
    class Car
    {
    
    
    
    
    public:
    
    
    Car(char* p_make, int p_passengers, int p_fuelcap, float p_efficiency)
    {
    char* maker= NULL;
    int passengers=0;
    int fuelcap=0;
    float efficiency=0;
    }
    
    
    Car();
    Car(const Car&c)
    {
    passengers = c.passengers;
    fuelcap  = c.fuelcap;
    }
    ~Car()
    {
    if(maker!=NULL)
    delete maker;
    }
    
    
    
    void setMake(char* p_make)
    {
    maker=p_make;
    }
    void setPassengers(int p_passengers)
    {
    passengers=p_passengers;
    }
    void setFuelcap(int p_fuelcap)
    {
    fuelcap=p_fuelcap;
    }
    void setEfficiency(float p_efficiency)
    {
    efficiency=p_efficiency;
    }
    
    
    char* getMake()
    {
    return maker;
    }
    int getPassengers()
    {
    return passengers;
    }
    int getFuelcap()
    {
    return fuelcap;
    }
    float getEfficiency()
    {
    return efficiency;
    }
    
    void printStatus()
    {
    cout<<"Make : "<<getMake()<<"\nPassengers : "<<getPassengers()<<"\nEfficiency : "
    <<getEfficiency()<<"\nRange : "<<getRange()<<endl<<endl;
    }
    
    
    
    
    private:
    
    
    char* maker;
    int passengers;
    int fuelcap;
    float efficiency;
    
    float getRange()
    {
    return fuelcap * efficiency;
    }
    
    };
    
    
    
    
    class CarList
    {
    public:
    void insert(Car&c)
    {
    list.push_back(c);
    }
       void print()
    {
    cout<<"Make"<<endl;
    }
    
    
    private:
    vector <Car> list;
    
    
    };
    
    
    
    
    int main()
    {
    
    
    Car chairman("ssangyoung", 5, 70, 8.4f);
    
    
    cout<<"Chairman Specification" << endl;
    chairman.printStatus();
    
    
    Car k9;
    k9.setMake("kia");
    k9.setPassengers(5);
    k9.setEfficiency(70);
    k9.setEfficiency(9.4f);
    
    
    cout << endl << "K9 Specification" << endl;
    k9.printStatus();
    
    
    Car k3(k9);
    k3.setEfficiency(14.5);
    
    
    cout << endl << "K3 Specification" << endl;
    k3.printStatus();
    
    
    cout << endl<< "Efficiency Comparision" << endl;
    cout <<"Chariman : "<<chairman.getEfficiency() << endl;
    cout <<"K9 : " <<k9.getEfficiency() <<endl;
    cout <<"K3 : " <<k3.getEfficiency() <<endl;
    
    
    CarList list;
    
    
    list.insert(chairman);
    list.insert(k9);
    list.insert(k3);
    
    
    cout<< endl << "list"<<endl;
    list.print();
    
    
    
    
    return 0;
    }

    Plz help... I can't fix it.... :q..:q...:q

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to indent your copy properly.

    Also, besides the copy constructor for Car, you should define or disable to copy assignment operator. Alternatively, change the maker member variable to be a std::string, but I am not sure if that is allowed in your requirements. If it is, you can then avoid explicitly defining the copy constructor and destructor too.

    Speaking of destructors, you should delete[] what you new[]. However, I note that you pass a string literal to setMake, but setMake only copies the pointer, which you then later using delete on.
    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 hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. What is purpose of defining a constructor that accepts all those parameters but does nothing with them?
    2. What is purpose of defining a copy constructor that only copies some of the data elements?
    3. Your "get" member functions should be declared const since they do not modify the object that they are called upon.
    4. Your getMake member function currently returns a non-const pointer which is dangerous if the user decides they will use the returned pointer to change what is being pointed to.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My code doesn't work, help please
    By BenBusby in forum C Programming
    Replies: 3
    Last Post: 01-24-2013, 06:14 AM
  2. C+P code use to work but now it doesn't!
    By CodeMonkey03 in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2012, 07:33 PM
  3. Example Code doesn't work!
    By Kayl669 in forum C Programming
    Replies: 8
    Last Post: 02-18-2010, 10:19 AM
  4. Simple C++ code doesn't work
    By alex_dude_122 in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2006, 12:53 PM
  5. this code doesn't work please help me debug
    By newcstudent in forum C Programming
    Replies: 2
    Last Post: 10-09-2006, 05:01 AM