conversion from different classes

This is a discussion on conversion from different classes within the C++ Programming forums, part of the General Programming Boards category; before any one tells me that this its bad to do this or its dangerous..ive already heard it on IRC ...

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    conversion from different classes

    before any one tells me that this its bad to do this or its dangerous..ive already heard it on IRC and didnt get an answer that probably would've took them 10 seconds to answer than have a big b!tching fest for 20 minutes on how dangerous they are.

    the book im following converts time24 class to an object of time12. military time to civilian time.
    heres the 2 classes:
    Code:
    class time12
    {
    private:
    	bool pm;		//true = pm, false = am
    	int hrs;		//1 to 12
    	int mins;		//0-59
    	
    public:
    	time12():	pm(true),hrs(0),mins(0)		//contructor to init everything to 0
    	{	}
    
    	time12(bool ap, int h,int m):	pm(ap),hrs(h),mins(m)		//3-arg constructor
    	{	}
    
    	void display() const		//display time12
    	{
    		cout << hrs << ':';
    
    		if(mins < 10)			//extra zero if minutes is less than 10
    			cout << '0';
    
    		cout << mins << ' ';
    		string am_pm = pm ? "p.m." : "a.m."; //create obj of string- if pm=true then "p.m" else "a.m."
    		cout << am_pm;						 //display string
    	}
    
    };
    //////////////////////////////////////////////////////
    ///////////class time24///////////////////////////////
    class time24
    {
    private:
    	int hours;
    	int minutes;
    	int seconds;
    
    public:
    	time24():  hours(0),minutes(0),seconds(0)
    	{	}
    
    	time24(int h,int m,int s):	hours(h),minutes(m),seconds(s)
    	{	}
    
    	void display() const
    	{
    		if(hours < 10)
    			cout << '0';
    		cout << hours << ':';
    		if(minutes < 10)
    			cout << '0';
    		cout << minutes << ':';
    		if(seconds < 10)
    			cout << '0';
    		cout << seconds;
    	}
    	operator time12() const;	//conversion operator 
    };
    //--------------------------------------------------
    
    time24::operator time12() const		//conversion function
    {
    	int hrs24 = hours;				//assign time24.hours to hrs24
    	bool pm = hours < 12 ? false : true;	//if time24.hours < 12 then its a.m else its pm
    
    	int roundMins = seconds < 30 ? minutes : minutes+1;	//round minutes at 30 seconds
    
    	if(roundMins == 60)	//if minutes rounded off higher
    	{					//and 60 was the result
    		roundMins = 0;	//set minutes to zero
    		++hrs24;		//and add 1 hour to it
    
    		if(hrs24 == 12 || hrs24 == 24)	//set am/pm accordingly if ithits 12 or 24
    			pm = (pm == true) ?  false : true;	//toggle am/pm
    	}
    
    	int hrs12 = (hrs24 < 13) ? hrs24 : hrs24 - 12;
    	
    	if(hrs12 == 0)		//00 is 12 a.m.
    	{
    		hrs12 = 12;
    		pm = false;
    	}
    	return time12(pm,hrs12,roundMins);
    }
    the text in bold, could i name this anything i want? or does it have to be the class that im converting it to?

    for those that cant resist telling me that this is dangerous to use. please tell me why.

    thanks in advance

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    a) Yes, you have to name it this way. There is no part of the declaration that is free for naming. All three words are keywords or type identifiers.

    b) I've used those operators before and it's like everything in C++: You can use it for good or you can make horrible mistakes with it. It all depends on your coding, nothing is unsafe by default or good by default.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. implicit/explicit? conversion of classes?
    By what3v3r in forum C++ Programming
    Replies: 7
    Last Post: 01-31-2006, 06:08 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 08:44 PM

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