1

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;
}
kukimukiku
  • 85
  • 8
  • 1
    You need at least to either correctly implement [The Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), or use smart pointers. – Ken Y-N Mar 17 '21 at 08:37
  • 2
    Why do you use pointers for `langas1`, `durys` and `langas2`? Where do you `delete` them? –  Mar 17 '21 at 08:40
  • @jabaa in Kambarys destructor, or didn't I? – kukimukiku Mar 17 '21 at 08:42
  • 2
    Why do you `Kambarys *k2=k;` and `k2=k;`? This is undefined behavior: `cout< –  Mar 17 '21 at 08:46
  • @jabaa oh there I just forgot to correct the code. Eitherway I'm not sure what I'm doing honestly – kukimukiku Mar 17 '21 at 08:47
  • 1
    `k` and `k2` are two pointers pointing to the same memory. After `delete k;` you can't access `k2`. –  Mar 17 '21 at 08:49
  • @jabaa understood, thank you. How should I correctly implement that if it's not too much to ask? – kukimukiku Mar 17 '21 at 08:53
  • 3
    Why do you use pointers? Avoid pointers. E.g. `Kambarys *k=new Kambarys(30.4, 40.1, 50.1);` could be `Kambarys k(30.4, 40.1, 50.1);`, `k->addWindow(langas1);` could be `k->addWindow(3.4, 1.2);`. Read about [Rule of zero](https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero). –  Mar 17 '21 at 08:54
  • @jabaa it was the requirement to do so – kukimukiku Mar 17 '21 at 08:56
  • 1
    There is no requirement in your question. Please add all requirements there and describe them in detail. It's important to answer the question. –  Mar 17 '21 at 08:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230015/discussion-between-elena-ingrida-and-jabaa). – kukimukiku Mar 17 '21 at 08:58

1 Answers1

3

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;
}