基本上我的代码崩溃,我不能找出原因,我已经使用的代码:: Blocks的调试器,但对我来说没有任何帮助。它会在打印出属性的正确数据后崩溃[4]。我知道这不是实现这一目标的最佳方式,但是对于需要展示可以完成的多种方式的课程而言,我的问题是它显示“河桂餐厅”后崩溃,无法找出原因。COUT打印后崩溃对象的getter
const int MAX_PROPERTIES = 5;
int main(void) {
Property properties[MAX_PROPERTIES];
Rentals tempRent;
tempRent = Rentals();
tempRent.setBond(10000);
tempRent.setMonthlyRent(700);
tempRent.setOwner("River Kwai Restaurant");
tempRent.setAddress("3 Bishopton Road");
tempRent.setSuburb("Footscray");
tempRent.setPostcode(5000);
properties[4] = tempRent;
tempRent.~Rentals();
cout << properties[4].getOwner() << endl;
return 0;
}
Property::Property(){
owner = "NULL";
address = "NULL";
suburb = "NULL";
postcode = 0;
}
Property::Property(string theOwner, string theAddress,
string theSuburb, int thepostCode):
owner(theOwner), address(theAddress),
suburb(theSuburb), postcode(thepostCode){}
Property::~Property() {}
Rentals::Rentals(string theOwner, string theAddress, string theSuburb,
int thePostCode, double theBond, double theMonthlyRent):
Property(theOwner, theAddress, theSuburb, thePostCode),
bond(theBond), monthlyRent(theMonthlyRent){}
Rentals::Rentals() : Property() {
owner = "NULL";
address = "NULL";
suburb = "NULL";
postcode = 0;
bond = 0;
monthlyRent = 0;
}
Rentals::~Rentals() {}
注:删除析构函数确实解决了它,但我想知道为什么?应该析构函数不会影响属性[4],因为它已经被初始化了?
也就是说相关的代码,但我会后,如果需要下面的整个代码/头文件。
#include "property_a.h"
#include "rentals.h"
#include "commercial.h"
#include "sales.h"
const int MAX_PROPERTIES = 5;
int main(void) {
Property properties[MAX_PROPERTIES];
Rentals tempRent;
properties[0] = Commercial("Notting Hill McDonalds",
"4 Gardiner Road", "Notting Hill", 5000,
"Li3000");
properties[1] = Rentals("Janet Dalgleish", "30 Firhill Court",
"Mary Hill", 4000, 500.00, 300.00);
properties[2] = Sales("Robert Burns", "3 Ayr Court", "Irvine", 4000,
"10/10/2015", 700000);
properties[3] = Property();
properties[3] = properties[0];
tempRent = Rentals();
tempRent.setBond(10000);
tempRent.setMonthlyRent(700);
tempRent.setOwner("River Kwai Restaurant");
tempRent.setAddress("3 Bishopton Road");
tempRent.setSuburb("Footscray");
tempRent.setPostcode(5000);
properties[4] = tempRent;
cout << properties[4].getOwner() << endl;
return 0;
}
Property::Property(){
owner = "NULL";
address = "NULL";
suburb = "NULL";
postcode = 0;
}
Property::Property(string theOwner, string theAddress,
string theSuburb, int thepostCode):
owner(theOwner), address(theAddress),
suburb(theSuburb), postcode(thepostCode){}
Property::~Property() {}
Commercial::Commercial() : Property() {
owner = "NULL";
address = "NULL";
suburb = "NULL";
postcode = 0;
license = "NULL";
}
Commercial::Commercial(string theOwner, string theAddress,
string theSuburb, int thepostCode,
string theLicense): Property(theOwner, theAddress,
theSuburb, thepostCode), license(theLicense) {}
Commercial::~Commercial() {}
Rentals::Rentals(string theOwner, string theAddress, string theSuburb,
int thePostCode, double theBond, double theMonthlyRent):
Property(theOwner, theAddress, theSuburb, thePostCode),
bond(theBond), monthlyRent(theMonthlyRent){}
Rentals::Rentals() : Property() {
owner = "NULL";
address = "NULL";
suburb = "NULL";
postcode = 0;
bond = 0;
monthlyRent = 0;
}
Rentals::~Rentals() {}
Sales::Sales(string theOwner, string theAddress, string theSuburb,
int thepostCode, string theAuctionDate, double thePrice):
Property(theOwner, theAddress, theSuburb, thepostCode),
auctionDate(theAuctionDate), price(thePrice) {}
Sales::Sales() : Property() {
owner = "NULL";
address = "NULL";
suburb = "NULL";
postcode = 0;
auctionDate = "NULL";
price = 0;
}
Sales::~Sales() {}
物业头
#ifndef __PROPERTY_A_H__
#define __PROPERTY_A_H__
/*TODO REQUIRED HEADER FILES AND NAMESPACES*/
#include <string>
#include "utility1.h"
class Property
{
protected:
string owner;
string address;
string suburb;
int postcode;
public:
Property();
Property(string theOwner, string theAddress, string theSuburb, int thepostCode);
virtual ~Property();
string getOwner() const {return owner;}; //Note the use of const
string getAddress() const {return address;};
string getSuburb() const {return suburb;};
int getPostcode() const {return postcode;};
void setOwner(string newOwner) {owner = newOwner;};
void setAddress(string newAddress) {address = newAddress;};
void setSuburb(string newSuburb) {suburb = newSuburb;};
void setPostcode(int newPostcode) {postcode = newPostcode;};
};
#endif
出租头
#ifndef __RENTALS_H__
#define __RENTALS_H__
#include "property_a.h"
class Rentals : public Property
{
protected:
double bond;
double monthlyRent;
public:
Rentals();
Rentals(string theOwner, string theAddress, string theSuburb,
int thepostCode, double theBond, double theMonthlyRent);
~Rentals() ;
double getBond() const {return bond;}; //Note the use of const
void setBond(double theBond) {bond = theBond;};
double getMonthlyRent() const {return monthlyRent;}; //Note the use of const
void setMonthlyRent(double theMonthlyRent) {monthlyRent = theMonthlyRent;};
};
#endif
你应该谷歌'对象slice' – billz 2015-04-05 10:07:09