Me too I have a problem with deep copy
help me..
this is my code i am using visual studio 2008

#include<iostream>
#include<string>
using namespace std;
class Street
{
private:

	int stretNumber;
	string name; 
	int numberHouses ;
	int *ListHouses ;
public :
	Street();
	Street(int, string, int);
	//Street(const  Street & otherStreet);
	Street(const  Street & Street1);
	void destroyHouses();
	void inputRoomsInHouses();
	void displayHouses();
	void print();
	~Street();
};

Street::Street()
{
	int stretNumber=0;
	string name= " "; 
	int numberHouses = 0 ;
	int *ListHouses= NULL;
}

Street::Street(int n,  string nam,int nberH)
{
	stretNumber=n;
	name= nam; 
	numberHouses = nberH ;
	ListHouses= new int [numberHouses];
	
}
/*Street::Street(const  Street & otherStreet)
{
	
	stretNumber=otherStreet.stretNumber;
	name= otherStreet.name ; 
	numberHouses = otherStreet.numberHouses ;
	ListHouses=otherStreet.ListHouses;
}*/
void Street::destroyHouses()
{
	delete[] ListHouses;
	//ListHouses=NULL;
}
Street::~Street()
{
	destroyHouses();
}
void Street::inputRoomsInHouses()
{
	cout<<"Enter number of rooms in each eleemnt of the ListHouses: ";
	for(int i=0;i<numberHouses;i++)
		cin>>ListHouses[i];
}
void Street::displayHouses()
{
		cout<<"Number of rooms in each eleemnt of Houses: ";
	for(int i=0;i<numberHouses;i++)
	cout<<ListHouses[i]<<" ";
		cout<<endl;
}
void Street::print()
{
	cout<<"The stret Number is: "<<stretNumber<<endl;
	cout<<"The stret name is: "<<name<<endl;
	cout<<"The stret number Houses is: "<<numberHouses<<endl;
	displayHouses();
	
}
Street::Street(const  Street & Street1)
{
	stretNumber=Street1.stretNumber;
	name= Street1.name ; 
	numberHouses = Street1.numberHouses ;
	ListHouses=new int[Street1.numberHouses];
	for(int i=0;i<Street1.numberHouses;i++)
		ListHouses[i]=Street1.ListHouses[i];
}
int main()
{
	int streetNnumber;
	int  nbeHouses;
	string aName;
	cout<<"Entr street Nnumber: ";
	cin>>streetNnumber;
	cout<<"Entr street Name: ";
	cin>>aName;
	cout<<"Enter number of Houses: ";
	cin>>nbeHouses;
	Street myStreet( streetNnumber,  aName , nbeHouses );
	myStreet.inputRoomsInHouses();
	myStreet.print();
	Street Street1(myStreet);
	cout<<"Street1 is: "<<endl;
	Street1.print();
	Street1.destroyHouses();
	cout<<"After destroy Street1: "<<endl;
	Street1.print();
		system("Pause");
	return 0;
}

Edited 5 Years Ago by Narue: Added code tags

You are freeing memory twice:

Street::~Street()
{
	destroyHouses(); //your memory is getting free here with destructor
}

// and you are manually deleting again in the main for Street1
Street1.destroyHouses();

So, when destructor of Street1 is trying to free the memory again, its crashing as that memory is already freed by you by calling Street1.destroyHouses();

You forgot to implement the assignment operator, which usually implies implementing a swap function as well. As so:

class Street
{
private:

	int stretNumber;
	string name; 
	int numberHouses ;
	int *ListHouses ;
public :
	Street();
	Street(int, string, int);
	//Street(const  Street & otherStreet);
	Street(const  Street & Street1);
	void destroyHouses();
	void inputRoomsInHouses();
	void displayHouses();
	void print();
	~Street();

        //friend swap function:
        friend void swap(Street& lhs, Street& rhs) throw() {
          using std::swap; //include std:: to ADL
          swap(lhs.stretNumber,rhs.stretNumber);
          swap(lhs.name,rhs.name); 
          swap(lhs.numberHouses,rhs.numberHouses);
          swap(lhs.ListHouses,rhs.ListHouses);
        };

        //A copy-and-swap assignment operator:
        Street& operator=(Street rhs) { //pass-by-value (copy)
          swap(*this,rhs);              //swap
          return *this;                 //return
        };
};
This article has been dead for over six months. Start a new discussion instead.