0
#include <iostream>
#include <vector>
using namespace std;

class House {
public:
    //default constructor
    House(string = "no-style", double = 0.0);
    //destructor
    ~House();
    //set the style
    void setStyle(string style);
    //set the cost
    void setCost(double cost);
    //get the style
    string getStyle() const;
    //get the cost
    double getCost() const;
    //overloaded operator: = (assignment)
    House& operator = (const House &other);
    //overloaded operator: == (comparison)
    bool operator == (const House &other);
    //overloaded operator: = (assignment)
    friend ostream& operator<<(ostream& out, const House& other);
private:
string* pStyle;
double* pCost;
};

//default constructor
House::House(string style, double cost)
{
    pStyle = new string{style};
    pCost = new double{cost};
}

//destructor
House::~House()
{
    if(pStyle != nullptr)
        delete pStyle;
    if(pCost != nullptr)
        delete pCost;
}

//set the style
void House::setStyle(string style)
{
    *pStyle = style;
}

//set the cost
void House::setCost(double cost)
{
    *pCost = cost;
}

//get the style
string House::getStyle() const
{
    return *pStyle;
}

//get the cost
double House::getCost() const
{
    return *pCost;
}


//overloaded operator: = (assignment)
House& House::operator = (const House &other)
{
    if(pStyle != nullptr)
        delete pStyle;
    if(pCost != nullptr)
        delete pCost;
    pStyle = new string{*other.pStyle};
    pCost = new double{*other.pCost};
    return *this;
}

//overloaded operator: == (comparison)
bool House::operator == (const House &other)
{
    return (*pStyle == *other.pStyle && *pCost == *other.pCost);
}

ostream& operator<<(ostream& out, const House& other)
{
    out<<"House " << other.getStyle() <<" costs:" << other.getCost()<<" dollars."<<endl;
    return out;
}

void showHouses(vector<House>& houses, int houseCount) {
    cout << endl << "Here are your houses: " << endl;
    for(int i=0; i<houseCount; i++)
    {
        cout << houses.at(i);
    }
}

int main() {
    
    vector<House> houses;
    House currentHouse;
    int houseCount = 0; // number of houses
    string style;
    double cost;
    int num;
    cout << "Here is the currentHouse: " << currentHouse << endl;

    do{
        cout<<"How many houses do you want to build? (must be at least 3) ";
        cin >> houseCount;
    }while(houseCount<3);
    cout << endl;

    for(int i=0; i<houseCount; i++)
    {
        cout<<"What is the style for house "<<i+1<<"? ";
        cin >> style;
        cout<<"What is the cost for house "<<i+1<<"? ";
        cin >> cost;
        currentHouse.setStyle(style);
        currentHouse.setCost(cost);
        houses.push_back(currentHouse);
        cout<<houses.at(i);
    }

    showHouses(houses, houseCount);
    cout << endl << "Which house do you want to change? ";
    cin >>num;
    cout<<"What is the new style? ";
    cin >> style;
    cout<<"What is the new cost? ";
    cin >> cost;
    currentHouse = House(style, cost);
    if(houses.at(num) == currentHouse)
        cout<<"The new house is the same as the old house."<<endl;
    else
        cout<<"The houses are different."<<endl;
    cout << "Copying new house to the old house ..." << endl;
   
    houses.at(num) = currentHouse;

    showHouses(houses, houseCount);
    return 0;
}

The code is not working when put value of houseCount greater than 1. I have received the following error message while execute the code: Here is the currentHouse: House no-style costs:0 dollars.

How many houses do you want to build? (must be at least 3) 3

What is the style for house 1? e What is the cost for house 1? 2 What is the style for house 2? f What is the cost for house 2? 5 terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Aborted (core dumped)

  • `House` violates [the rule of three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). When an instance of `House` is copy-constructed from another, both instances end up holding the same pointers, and both believe they own them. When the first is destroyed, its destructor deletes the memory those pointers point to. The second ends up holding dangling pointers; any attempt to access them exhibits undefined behavior. Why did you feel the need to have pointers as data members? – Igor Tandetnik Apr 23 '22 at 19:29
  • In this problem, the data members are pointer types. How can I solve it? – Suman Halder Apr 25 '22 at 05:04
  • There is no reason for those members to be pointers. But if you insist, provide a copy constructor similar to your copy assignment operator. – Igor Tandetnik Apr 25 '22 at 13:43

0 Answers0