kukimukiku
kukimukiku

Reputation: 95

Deep copy unsuccessful

might be a stupid question and if it is, let me know, I will delete it as soon as possible. The thing is I have to make a deep copy in class "Kambarys" (ignore mixed languages, I know I shouldn't do that). Program terminates after trying to call function second time. Probably the problem is my syntax in constructor copy, but I can't find the correct one anywhere. One of the requirements is to create langas, durys and kambarys in dynamic memory using "new" and delete windows vector and door in Kambarys destructor. Appreciate the help! Requirements: In the main method, use the new operator to create room k1, add windows and doors to it. Write a constructor Room (const Room & k) that would create a correct copy. In the method main, write another room k2. Calculate the length of the baseboards / wall area. Perform the following steps: k2 = * k1; delete k1;

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

class Langas{
    private:
    float height;
    float widht;
    static int countL;
    public:
    Langas(float h, float w){
        this->height=h;
        this->widht=w;
        countL++;
    }
    ~Langas(){
        --countL;
    }
    float getHeight(){
      return height;
    }
    float getWidht(){
      return widht;
    }
    static int getWindowCount(){
        return countL;
    }
};

class Durys{
    private:
    float heightD;
    float widhtD;
    static int countD;
    public:
    Durys(float hD, float wD){
        this->heightD=hD;
        this->widhtD=wD;
        countD++;
    }
    ~Durys(){
        --countD;
    }
    float getHeightD(){
      return heightD;
    }
    float getWidhtD(){
      return widhtD;
    }
    static int getDoorCount(){
        return countD;
    }

};
class Kambarys{
    private:    
    float heightK;
    float widhtK;
    float lenghtK;
    public:
    vector<Langas*> windows;
    Durys* door;
    Kambarys(float hK, float wK, float lK){
        this->heightK=hK;
        this->widhtK=wK;
        this->lenghtK=lK;
    }
    Kambarys(const Kambarys &k){
        this->door=k.door;
        this->windows=k.windows;
        heightK=k.heightK;
        widhtK=k.widhtK;
        lenghtK=k.lenghtK;
    }
    ~Kambarys(){
        door=NULL;
        for(int i=0; i<windows.size(); i++){
            delete windows[i];
        }
        windows.clear();
        delete door;
    }
    float getHeightK(){
      return heightK;
    }
    float getWidhtK(){
      return widhtK;
    }
    float getLenghtK(){
      return lenghtK;
    }
    void addWindow(Langas* w){
        windows.push_back(w);
    }
    void addDoor(Durys *d){
        door=d;
    }
};
float countWallPlot(Kambarys* k){
    float cWPlot=(2*k->getLenghtK()*k->getHeightK())+(2*k->getWidhtK()*k->getHeightK());
    for(int i=0; i<k->windows.size(); i++){
        cWPlot-=((k->windows[i]->getHeight()))*(k->windows[i]->getWidht());
    }
    cWPlot-=((k->door->getHeightD()))*(k->door->getWidhtD());
    return cWPlot;
}
float countLenght(Kambarys* k){
    float floorL=(k->getLenghtK()*k->getWidhtK()*2);
    floorL-=(k->door->getWidhtD());
    return floorL;
}
int Langas::countL=0;
int Durys::countD=0;

int main(){
    Langas *langas1=new Langas(3.4, 1.2);
    Durys *durys=new Durys(3.1, 1.5);
    Langas *langas2=new Langas(6.4, 1.5);
    Kambarys *k=new Kambarys(30.4, 40.1, 50.1);
    Kambarys *k2=k;
    k->addWindow(langas1);
    k->addWindow(langas2);
    k->addDoor(durys);
    cout<<countWallPlot(k)<<" "<<countLenght(k)<<endl;
    cout<<"Window count "<<Langas::getWindowCount()<<", door count "<<Durys::getDoorCount()<<endl;
    k2=k;
    delete k;
    cout<<countWallPlot(k2)<<" "<<countLenght(k2)<<endl;
    cout<<"Window count "<<Langas::getWindowCount()<<", door count "<<Durys::getDoorCount()<<endl;
}

Upvotes: 1

Views: 113

Answers (1)

user15388024
user15388024

Reputation:

You have to allocate memory for k2 and copy the object, not the pointer.

You have to allocate memory in the copy constructor and copy assignment operator.

door=NULL; before delete door; would skip the delete and cause a memory leak.

windows.clear(); is not necessary in the destructor. Keep your code simple.

EDIT: After you added "Perform the following steps: k2 = * k1; delete k1;" I made k2 an object, not a pointer.

#include <iostream>
#include <vector>

class Langas {
private:
  float height;
  float width;
  static int count;

public:
  Langas(float h, float w): height(h), width(w) {
    ++count;
  }
  ~Langas() { --count; }
  float getHeight() const { return height; }
  float getWidht() const { return width; }
  static int getWindowCount() { return count; }
};

class Durys {
private:
  float height;
  float width;
  static int count;

public:
  Durys(float h, float w): height(h), width(w) {
    ++count;
  }
  ~Durys() { --count; }
  float getHeight() const { return height; }
  float getWidth() const { return width; }
  static int getDoorCount() { return count; }
};

class Kambarys {
private:
  float height;
  float width;
  float length;

public:
  std::vector<Langas *> windows;
  Durys *door = nullptr;
  Kambarys(float hK, float wK, float lK): height(hK), width(wK), length(lK) {}
  Kambarys(const Kambarys &k): height(k.height), width(k.width), length(k.length), windows(), door(k.door ? new Durys(k.door->getHeight(), k.door->getWidth()) : nullptr) {
    for (const auto window : k.windows) {
      windows.emplace_back(new Langas(window->getHeight(), window->getWidht()));
    }
  }
  Kambarys &operator=(const Kambarys &k) {
    door = k.door ? new Durys(k.door->getHeight(), k.door->getWidth()) : nullptr;
    for (const auto window : k.windows) {
      windows.emplace_back(new Langas(window->getHeight(), window->getWidht()));
    }
    height = k.height;
    width = k.width;
    length = k.length;
    return *this;
  }
  ~Kambarys() {
    for (auto window : windows) {
      delete window;
    }
    delete door;
  }
  float getHeight() const { return height; }
  float getWidth() const { return width; }
  float getLength() const { return length; }
  void addWindow(Langas *w) { windows.emplace_back(w); }
  void addDoor(Durys *d) { door = d; }
};

float countWallPlot(const Kambarys &k) {
  float cWPlot = 2 * k.getLength() * k.getHeight() + 2 * k.getWidth() * k.getHeight();
  for (const auto window : k.windows) {
    cWPlot -= window->getHeight() * window->getWidht();
  }
  cWPlot -= k.door->getHeight() * k.door->getWidth();
  return cWPlot;
}

float countLength(const Kambarys &k) {
  float floor = k.getLength() * k.getWidth() * 2;
  floor -= k.door->getWidth();
  return floor;
}

int Langas::count = 0;
int Durys::count = 0;

int main() {
  Langas *langas1 = new Langas(3.4, 1.2);
  Durys *durys = new Durys(3.1, 1.5);
  Langas *langas2 = new Langas(6.4, 1.5);
  Kambarys *k = new Kambarys(30.4, 40.1, 50.1);
  Kambarys k2(*k);
  k->addWindow(langas1);
  k->addWindow(langas2);
  k->addDoor(durys);
  std::cout << countWallPlot(*k) << " " << countLength(*k) << std::endl;
  k2 = *k;
  std::cout << "Window count " << Langas::getWindowCount() << ", door count " << Durys::getDoorCount() << std::endl;
  delete k;
  std::cout << countWallPlot(k2) << " " << countLength(k2) << std::endl;
  std::cout << "Window count " << Langas::getWindowCount() << ", door count " << Durys::getDoorCount() << std::endl;
}

Upvotes: 3

Related Questions