As part of my Software Engineering degree we are studying Advance Object Orientated Programming. This is being done using C++ as the language. The course is part time so all the material is compressed into 2 hours of lectures a week. Being completely new to C++ the learning curve is steep. We are currently covering RAII and the concepts of memory allocation and clean up are new to me. We have been asked to create a class which is constructed with an int and a char*.

We were asked to create the constructors for the following.

#ifndef __CAT__
#define __CAT__
#include <iostream>

class Cat{

private:
    int lives;
    char* name;

public:
    Cat(int _lives, char* _name);
    Cat(const Cat &rhs);
    Cat& operator=(const Cat &rhs);
    Cat(Cat &&rhs);
    Cat& operator=(Cat &&rhs);
    ~Cat();

    friend std::ostream& operator<<(std::ostream& os, const Cat &rhs);
};
#endif

I have managed to build the constructor, deconstructor, and the Copy constructor. They compile and seem to do what they are supposed to.

Cat::Cat(int _lives, char* _name){
    printf("Constructor called!\n");

    lives = _lives;
    int length = strlen(_name)+1;
    name = new char[length];
    memcpy(name, _name, length*sizeof(char));
}

Cat::~Cat(){
    printf("Deconstructor called!\n");

    if(name != nullptr){
        delete [] name;
        name = nullptr;
    }
    lives = 0;
}

Cat::Cat(const Cat &rhs){
    printf("Copy Constructor called.\n");
    lives = rhs.lives;
    int length = strlen(rhs.name)+1;
    name = new char[length];
    memcpy(name, rhs.name, length*sizeof(char));
}

I need to build copy assignment, move, and move assignment constructors.

I understand that copy assignment is taking an already assigned object and copying another instance of this object to this already assigned object. I understand this means deallocating resources of the existing object and allocating resources to accept the object being copied.

Move is moving an objectA to a new objectB and deallocating the resources of object A. Move allocation is the same but the objectB is not new but already exists.

I need to write the following constructors.

Cat& Cat::operator=(const Cat &rhs) {
    printf("Copy Assignment Constructor called.\n");

}

Cat::Cat(Cat &&rhs){
    printf("Move Constructor called.\n");

}

Cat& Cat::operator=(Cat &&rhs){
    printf("Move Assignment Constructor called.");

}

so they allow the following to be run

#include <iostream >
#include "Cat.h"

    // std :: move forces the move constructor to be called ...
    // Otherwise the compiler can optimize ...
    // Cat d = make_cat (); ... into ...
    // Cat d(42 , " Douglas ");
    // without ever invoking the move constructor .
Cat make_cat() {
    Cat tmp(42 , "Douglas");
    return std :: move (tmp);
}


int main (int argc , char* argv[]){
    Cat a(9, "Garfield");
    std::cout << a << std::endl;

    { // Anonymous scope - copy constructor
        Cat b = a;
        std::cout << b << std::endl;
    }

    { // Anonymous scope - copy assignment
        Cat c(666 , "Catbert")
        std::cout << c << std::endl;
        c = a;
        std::cout << c << std::endl;
    }

    { // Anonymous scope - move constructor
        Cat d = make_cat();
        std::cout << d << std::endl;
    }

    { // Anonymous scope - move assignment
        Cat e(9, "Maru");
        std::cout << e << std::endl;
        e = make_cat();
        std::cout << e << std::endl;
    }
    return 0;
}

I just don't really understand C++ enough to achieve this. Any help, resources, or advice would be greatly appreciated.

UPDATE I have read all the links to info in the post comments and discussed the problem further with my lecturer. I have now managed to complete the task by writing all the constructors. I will post the solution after the submission deadline on 22/05/2019.

Thanks Paul.

0 Answers