191k views
3 votes
A class named clock has two instance variables: hours (type int) and isticking (type boolean). write a constructor that takes a reference to an existing clock object as a parameter and copies that object's instance variables to the object being created.your task: write a clock constructor that makes this possible. just write this constructor -- don't write the whole class or any code outside of the class!

User Utiq
by
8.6k points

1 Answer

1 vote
You can use the initializer list for that; no code needed:

Clock(const Clock& rClock) : hours(rClock.hours), isTicking(rClock.isTicking)
{
// no code needed
}

In fact, the compiler will provide this copy constructor by default, you don't even have to write this constructor!
User Aleksa Majkic
by
7.3k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.