1
votes

How do I create a copy constructor that copies an object to the newly instantiated object and also how do I use the (==) operator to compare the two Tricycle object’s speeds? Here is the sample code!

#include <iostream>

class Tricycle
{
private:
    int* speed;
  //static int speedlim;

public:
    Tricycle();
    // copy constructor and destructor use default
    int getSpeed() const { return *speed; }
  //static int getspeedlim() {return speedlim;}
    void setSpeed(int newSpeed) { *speed = newSpeed; }
    Tricycle operator=(const Tricycle&);
  void operator++();
  void operator++(int);
};

  //Tricycle operator++();
  //Tricycle operator++(Tricycle,int);

Tricycle::Tricycle()
{
    speed = new int;
    *speed = 5;
  //speedlim=40;
  }

Tricycle Tricycle::operator=(const Tricycle& rhs){ 
  
    if (this == &rhs)
        return *this;
    delete speed;
    //speed = new int;
    //*speed = rhs.getSpeed();
    return *this;
}

void Tricycle::operator++(){
  (*speed)++;}
void Tricycle::operator++(int){
  (*speed)++;}  

int main()
{
    Tricycle wichita;
  //std::cout << "the speed limit is : " << Tricycle::getspeedlim();; 
    std::cout << "Wichita's speed : " << wichita.getSpeed() << "\n";
    std::cout << "Setting Wichita's speed to 6 ...\n";
    wichita.setSpeed(6);
    Tricycle dallas;
    std::cout << "Dallas' speed : " << dallas.getSpeed() << "\n";
    std::cout << "Copying Wichita to Dallas ...\n";
    wichita = dallas;
    std::cout << "Dallas' speed : " << dallas.getSpeed() << "\n";

  wichita.setSpeed(5);
  wichita++;
  std::cout << "Wichita's speed : " << wichita.getSpeed() << "\n";

  wichita.setSpeed(5);
  ++wichita;
  std::cout << "Wichita's speed : " << wichita.getSpeed() << "\n";
  
    return 0;
}

Here is a test I tried using pre-increment and post-increment to inc speed. This is the answer.

Wichita's speed : 5
Setting Wichita's speed to 6 ...
Dallas' speed : 5
Copying Wichita to Dallas ...
Dallas' speed : 5
Wichita's speed : 6
Wichita's speed : 6

But for these, I'm genuinely lost. ????

Unrelated, but your assignment operator should return a reference: Tricycle &. - jkb
What's the point of using dynamically allocated memory for speed? If you use int instead you don't need to define the copy and move constructors/assignment operators, since the default ones are sufficient. The return type of the assignment operators should be a reference to the containing class though... - fabian
I recommend that you go through this canonical implementation reference for overloading operators. All your overloaded operators are wrong. - Some programmer dude
As for the copying part, you already seem to know how to do that (well, almost) with your copy-assignment operator. Now think a little on how to do this in your copy-constructor. - Some programmer dude
Here's how to "create a copy constructor that copies an object to the newly instantiated object", which is what you asked: declare the constructor, it's a constructor that takes a const reference to another instance of this object. In that constructor you then proceed and initialize all members of the new object to be the same as the other object. There's nothing magical about a copy constructor, it's a constructor that's no different than any other constructor, except that its only parameter is a const reference to another instance of the same class. That's all. - Sam Varshavchik