Thread: Please check my C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    By this you referring to getting the current time from system maybe? Or just set to any hour, minute... I removed the seconds, no relevancy to this program really
    I mean you set hour and minute (and second?) to zero if they are not specified.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    overloading

    Code:
    // Overload for reading input streams
    istream& operator >> (istream& in, Car& car)
    {
    	string make, model;
    	int ymodel, engCap;
    	TankStatus ts;
    
    	in >> make >> model;
    	in >> ymodel;
    	in >> engCap;
    	in >> ts;
    	
    	car.setCarDetails(make, model, ymodel, engCap, ts);
    }
    My thoughts are the overloaded function should only have the four lines, not the setCarDetails()...

    here's the setCarDetails
    Code:
    void Car::setCarDetails(string make, string model, unsigned yrModel, 
    						unsigned engCap, TankStatus tnkStatus)
    {
    	make = carMake;
    	model = carModel;
    	yearModel = yrModel;
    	engineCapacity = engCap;
    	tankStatus = tnkStatus;
    }
    OR, can overloaded operators call other functions within?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code is correct.

    If you want to operate directly on car, you either need to make the operator>> function a friend ("friends" are trusted to not do silly things with an object, and thus can see private member variables and functions), or make it a member function of the object itself.

    Whether you prefer to call a member setter function or access the member variables directly is pretty much a design decision that can be argued either way, and without any particular knowledge of the overall design/requirements, it would be impossible to say which is better. At this point, you decide what you want to do. The call of the setter has the benefit of being safer - but potentially slower because of the call overhead. I don't think performance (here at least) will be a factor, so I wouldn't worry about that bit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    flood of errors

    Thnx mats & Elysia for your help... There's a progress...

    Now i've compiled after the changes which includes the use of the '<<' & '>>' overloaded operators.. I'm gonner post them by step to avoid confusions...

    read member function

    Code:
    void Car::read(istream& indata, Car& car, bool isKeyboard)
    {
    	indata >> car;
    	if(isKeyboard) {
    		readDates(indata);
    	}
    }
    it's declaration in class Car

    Code:
    void read(istream& i, const Car&, bool t);
    error..
    Code:
    error C2511: 'void Car::read(std::istream &,Car &,bool)' : overloaded member function not found in 'Car'
    ?????
    Last edited by csonx_p; 07-14-2008 at 04:20 AM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Car Class

    Code:
    // Declare class to hold car details
    
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    #include "RentalDate.h"
    
    using namespace std;
    
    class Car						
    {
    public: 
    	
    	Car();						
    	
    	enum TankStatus {unknown, empty, half, full};	 
    
    	void setCarDetails(string m,string ml,unsigned yrm,unsigned engCap,TankStatus ts);
    	static string getTankStatusName(TankStatus);
    	bool sameModel(const string&) const;
    	bool operator<(const Car&) const;	
    	void read(istream&, const Car& c, bool);
    	void write(ostream&, bool);
    	
    	string getModel();
    	string getMake();
    	unsigned getEngineCap();
    	unsigned getYearModel();
    	TankStatus getTankStatus();
    
    private:
    
    	string		make;				// Make as in..."toyota"
    	string		model;				// Model as in ... "cressida"
    	unsigned	engineCapacity;		// Engine capacity may be stored as "1800" not 1.8
    	unsigned	yearModel;			
    	TankStatus	tankStatus;			// Maybe filled, half-filled, or empty
    };
    
    // Overload fstream operators for enum variable
    ostream& operator << (ostream& out, Car::TankStatus eStatus);
    istream& operator >> (istream& in, Car::TankStatus& eStatus);
    istream& operator >> (istream& in, Car& car);
    void readDates(Date& dates);

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Thnx mats & Elysia for your help... There's a progress...

    Now i've compiled after the changes which includes the use of the '<<' & '>>' overloaded operators.. I'm gonner post them by step to avoid confusions...

    read member function

    Code:
    void Car::read(istream& indata, Car& car, bool isKeyboard)
    {
    	indata >> car;
    	if(isKeyboard) {
    		readDates(indata);
    	}
    }
    it's declaration in class Car

    Code:
    void read(istream& i, const Car&, bool t);
    error..
    Code:
    error C2511: 'void Car::read(std::istream &,Car &,bool)' : overloaded member function not found in 'Car'
    ?????
    Hmm. Why would you have a function that reads into a car object, that in itself is declared (and not static) in the Car class. I would either make it a free function (not part of the class), or use the current object (this) as the car object, and not pass it.

    I don't know why it's going wrong - perhaps you need std::istream in your class member function?

    Edit: And your problem in itself is that in your declaration in the class, you use const Car &, then loose the const in the actual function definition - you should not have const in either place in this instance, as you INTEND to modify car.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM