Operator overloading,initization, Print() in header file

This is a discussion on Operator overloading,initization, Print() in header file within the C++ Programming forums, part of the General Programming Boards category; I'm currently writing a header file for a Car class. But The problem is I don't know how to initialized ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    13

    Operator overloading,initization, Print() in header file

    I'm currently writing a header file for a Car class. But The problem is I don't know how to initialized in the Car class and use the copy constructor in the header file. Also, this header file requires a operator function. When I compile it, I have a syntax errors for those two operator function. For the Print() method, I have to use it for display the mantfacturere, model, year, total manufacture, total kilometers travelled and the amount of fuel costs, total number of services and fuel economy with print method. Can anyone please take a look at my code and help me fixing some of the mistakes in the header file?

    Thank you

    PHP Code:
    #ifndef CAR_H
    #define CAR_H
    #ifndef LEN
    #define LEN 25

    class Car
    {    
           private:
               
    char name[LEN]; 
               
    char model[LEN]; 
               
    int year;
        
           public:
                
    Car();
                
    Car(char *mchar *nint y);
                
    Car(const Car &source);              //copy constructor
                
    const Car operator +(const Journeymeter) const;
                const 
    Car operator +(const FuelPurchaseamount) const;
                
    void print(void) const;        
                ~
    Car();
    };
    #endif 
    Last edited by Tozilla; 03-26-2003 at 08:02 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    With all that you've shown, the only thing wrong is that "endif" should be "#endif".

    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    thanks..but i still have sytnax errors with the operator overloading function

  4. #4
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    You need to show us the function bodys, we can't do anything with just the headers...
    If you ever need a hug, just ask.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well syntax errors i agree but logical errors and good coding standards errors abound.
    Code:
    #ifndef LEN
    #define LEN 25
    Whats that?
    You shouldnt ( read never) do that.
    do this...
    Code:
    //alt 1...
    
    const unsigned int LEN = 25;
    
    //alt2...
    
    namespace
    {
    const unsigned int LEN = 25;
    }
    // which is equivalent to this now deprecated use of static
    static const int LEN = 25;
    I would suggest doing this....
    Code:
    private:
               char name_[LEN]; 
               char model_[LEN]; 
               int year_;
    because it then allows you to do this...
    Code:
    Car(const char *model, const char *name, int year);
    Now you can easily see what the params for the constructor are. Single letter variable names are usually next to useless except for maybe loop variables.
    What are you doing here?
    Code:
    const Car operator +(const Journey& meter) const;
    const Car operator +(const FuelPurchase& amount) const;
    Firstly operator + should be a non-member. Secondly if you describe a car as a name,model and year what exactly does it mean to add either a fuelpurchase or a journey to that data and come up with a whole new car. Surely both are illogical.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    sorry..I forgot to write that there're two more header files and source files. Those files are for calculating how much fuel has been purchased and how far the car has travel.
    So, I have to use two operator functions that accepts an objects of type Journey and FuelPurchase as parameter. And then use print() to display the calculation.

    It should print out the orignal and final info of a car in the output:

    It should be like this:

    Original Info:
    Vehicle: BMW G200
    0km travelled, 0 liters of fuel at aa cost $0

    Final Info:
    Vehicle: BMW G200
    10km traveled, 5 litres of fuel at a cost of $2
    PHP Code:
    #ifndef CAR_H
    #define CAR_H
    static const int LEN 25;

        class 
    Car
        
    {    
            private:
                       
    char name_[LEN]; 
                       
    char model_[LEN]; 
                       
    int year_;

            public:
                
    Car();
                
    Car(const char *model, const char *nameint year);
                
    Car(const Car &source);              //copy constructor
                
    const operator +(const Journeym) const;
                const 
    operator +(const FuelPurchasea) const;
                
    void print(void) const;        
                ~
    Car();
                
        };
    #endif 
    Last edited by Tozilla; 03-27-2003 at 06:19 AM.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    operator + still makes no sense and you still have it as a member. Use these guidelines....

    • assignment = must be a member, as must () [] ->
    • unary operators are members
    • += -= *= /= %= etc. are members
    • All other binary operators are nonmembers.


    No matter how you write those functions if your car is only ever described as a make,model,year then adding fuelpurchase/journey to a car to get a new car makes no sense.
    observe...
    a car ..... Ford,Mondeo,2000. ( a 2000 Ford mondeo)
    Now add 10km travelled and you still get Ford,Mondeo,2000. This is because a car has no data members which can represent the things that fuelpurchase and journey represent.So describing a car as you ahave done it makes no sense to have those operators in this class.
    As i told you that use of static although still compiles is actually deprecated in c++. To get the effect of statics old meaning use an anonymous namespace. Read Kens namespace tut in the FAQ for an explanation of anonymous namespaces.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Another member is working on this same project and I think the interfaces were pre-determined for them (Lord help them).

    Tell your teacher that overloading an operator that doesn't have much to do with the operator is only good for code obfuscation contests!! Without writing a paper on the subject, I'll demostrate this principle in code:
    Code:
    Vehicle car(...);
    FuelPurchase fp(...);
    
    ....
    
    car = car + fp; //what the hell does this mean???
    
    //or 
    
    car.AddFuelPurchase(fp); //mmm, readable,self-documenting code
    Anyway, about your last post:
    - you renamed your class from "Car" to "Vehicle" but you didn't rename any of your constructors (or destructor).
    - you removed the return type from both of your "operator+" methods - they should now return Vehicle.

    gg

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Another member is working on this same project and I think the interfaces were pre-determined for them (Lord help them).
    Then how on earth will they ever learn good design principles given interfaces that poor.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    I don't know why my professor gives us this kind of design.
    I guess my professor wants us to know certain things in this design.

    ok..these are the classes I need to write

    Car class:
    --------------
    A constructor that allows each attribute of the Vehicle class to be initialised. By using default parametes, this constructor should also be able to act a default constructor

    A copy constructor

    addition operator fucntion that accept an object of type Jounrey as a parameter

    addition operator fucntion that accept an object of type FuelPurchase a parameter

    A method Print() to display

    Journey class:
    ------------------
    A constructor that allows each attribute of the Journey class to be initialised.


    FuelPurchase:
    ------------------
    A constructor that allows each attribute of the FuelPurchase class to be initialised.



    Output:
    ---------

    Original :
    -----------
    Vehicle: BMW G200
    0 km travelled requirng 0 litres of fuel at a cost of $0
    No travel have been recorded yet.
    No fuel has been purchased yet.

    Vehicle: Benz G100
    0 km travelled requirng 0 litres of fuel at a cost of $0
    No travel have been recorded yet.
    No fuel has been purchased yet.

    Vehicle: Ford R123
    0 km travelled requirng 0 litres of fuel at a cost of $0
    No travel have been recorded yet.
    No fuel has been purchased yet.


    Final:
    ------
    Vehicle: BMW G200
    150 km travelled requirng 75 litres of fuel at a cost of $60
    The car achieved a fuel economy of 50.0 litres/100km
    The vehicle should have undergone 1 services.
    The average cost of fuel was $0.83 per litres.

    Vehicle: Benz G100
    250km travelled requirng 82 litres of fuel at a cost of $72
    The car achieved a fuel economy of 32.8 litres/100km
    The vehicle should have undergone 2 services.
    The average cost of fuel was $0.83 per litres.

    Vehicle: Ford R123
    350 km travelled requirng 100 litres of fuel at a cost of $100
    The car achieved a fuel economy of 28.6litres/100km
    The vehicle should have undergone 3 services.
    The average cost of fuel was $1. per litres.
    Last edited by Tozilla; 03-27-2003 at 07:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21