Quantcast
Channel: Visual Studio General Questions forum
Viewing all articles
Browse latest Browse all 21115

Creating A Class Constructor With The Object As Argument

$
0
0

Hello!

My professor gave me an assignment containing classes. This is what he asked:

****************

Implement class Time [...] It should provide the following operations:

  1. Five constructors:
    1. Default
    2. Takes hour, minute, second
    3. Takes hour, minute
    4. Takes hour
    5. Takes a Time object

*****************

My question is about the last constructor. Is it even possible for a constructor in a class to take an object of this same class as argument?

Here's what I tried to do:

class Time
{
private:
	int hour, minute, second;

public:
	Time(){setTime(0,0,0);}
	Time(int hour, int minute, int second){setTime(hour, minute, second);}
	Time(int hour, int minute){setTime(hour,minute,0);}
	Time(int hour){setTime(hour,0,0);}
	Time(Time timeObject){this->hour=timeObject.getHour(); this->minute=timeObject.getMinute(); this->second=timeObject.getSecond();}

	void setTime(int hour, int minute, int second){
		this->hour = hour;
		this->minute = minute;
		this->second = second;}

	int getHour(){return hour;}
	int getMinute(){return minute;}
	int getSecond(){return second;}

};

I'm not sure what it is I'm doing wrong if it is possible, but this is the error I got: "error C2333: 'Time::Time' : error in function declaration; skipping function body".

Any suggestions/ideas?

Thanks! :)

Rita.


Viewing all articles
Browse latest Browse all 21115

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>