How to know when a destructor is called? in c++ - c++

#include<iostream>
using namespace std;
class Point {
private:
int x;
int y;
static int numCreatedObjects;
public:
Point() :x(0), y(0) {
numCreatedObjects++;
}
Point(int _x, int _y) {
x = _x;
y = _y;
numCreatedObjects++;
}
~Point() {
cout << "Destructed..." << endl;
}
void setXY(int _x, int _y) {
this->x= _x;
this->y = _y;
}
int getX() const { return x; }
int getY() const { return y; }
Point operator+(Point& pt2) {
Point result(this->x + pt2.x, this->y + pt2.y);
return result;
}
Point& operator = (Point& pt) {
this->x = pt.x;
this->y = pt.y;
}
static int getNumCreatedObject() { return numCreatedObjects; }
friend void print(const Point& pt);
friend ostream& operator<<(ostream& cout, Point& pt);
friend class SpyPoint;
};
int Point::numCreatedObjects = 0;
void print(const Point& pt) {
cout << pt.x << "," << pt.y << endl;
}
ostream& operator<<(ostream& cout, Point& pt) {
cout << pt.x << "," << pt.y;
return cout;
}
class SpyPoint{
public:
void hack_point_info(const Point& pt) {
cout << "Hacked by SpyPoint" << endl;
cout <<"x: "<<pt.x <<endl<<"y: "<<pt.y << endl;
cout << "numCreatedObj:" << pt.getNumCreatedObject() << endl;
}
};
int main() {
Point pt1(1, 2);
cout << "pt1 : ";
print(pt1);
cout << endl;
Point* pPt1 = &pt1;
cout << "pt1 : ";
cout << pPt1->getX() << ", " << pPt1->getY() << endl;
cout << "pt1 : ";
cout << pPt1->getX() << ", " << pPt1->getY() << endl;
cout << endl;
Point* pPt2 = new Point(10, 20);
cout << "pt2 : ";
cout << pPt2->getX() << ", " << pPt2->getY() << endl;
cout << endl;
delete pPt2;
cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl;
Point pt2(10, 20);
Point pt3(30, 40);
Point pt4 = pt2 + pt3;
cout << "pt2 : ";
cout << pt2 << endl;
cout << "pt3 : ";
cout << pt3 << endl;
cout << "pt4 : ";
cout << pt4 << endl;
cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl << endl;
Point* ptAry = new Point[5];
cout << "pt2 NumCreatedObject : ";
cout << pt2.getNumCreatedObject() << endl;
cout << endl;
delete[] ptAry;
cout << endl;
SpyPoint spy;
cout << "pt1 info" << endl;
spy.hack_point_info(pt1);
cout << endl;
cout << "pt4 info" << endl;
spy.hack_point_info(pt4);
cout << endl;
return 0;
}
I don't know why the yellow underlined part prints out. I understand that the destructor is called when returning, unless it is dynamically allocated.
I guess pt1 is going to disappear, but I don't know why.
Also, I would like to know if there is a method that can be forcibly destroyed even if it is not dynamically allocated.

Related

Implementing a non-member IO operator

In my assignment I was asked to create the Product class, and I have finished all the implementations except the "non-member IO operator". The question I found it very vague, it asks me to overload the << and >> operators to work with ostream and istream to read a Product from and print a Product to the console in order to make this main function work.
Here I see the main function has cout or cin to Product's derived class SItem, I wonder how I should implement the << >> operators to make the main work.
My main:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "Product.h"
#include <fstream>
#ifdef TAB
# undef TAB
#endif
#define TAB '\t'
using namespace std;
namespace sict {
class SItem :public Product {
public:
SItem(const char* theSku, const char * theName) :Product(theSku, theName) {}
SItem() {}
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const {
if (!isEmpty()) {
file.open("ms4.txt", ios::out | ios::app);
file << sku() << TAB << name() << TAB << quantity() << TAB << qtyNeeded() << TAB
<< int(taxed()) << TAB << price() << endl;
file.clear();
file.close();
}
return file;
}
virtual std::fstream& load(std::fstream& file) {
file.open("ms4.txt", ios::in);
char buf[2000];
double dbuf;
int ibuf;
file >> buf;
sku(buf);
file >> buf;
name(buf);
file >> ibuf;
quantity(ibuf);
file >> ibuf;
qtyNeeded(ibuf);
file >> ibuf;
taxed(ibuf != 0);
file >> dbuf;
price(dbuf);
file.clear();
file.close();
return file;
}
virtual std::ostream& write(std::ostream& os, bool linear)const {
return isEmpty() ? os : (os << sku() << ": " << name() << ", qty: "
<< quantity() << ", qtyNeeded:" << qtyNeeded()
<< ", Cost: " << fixed << setprecision(2) << cost());
}
virtual std::istream& read(std::istream& is) {
char buf[2000];
double dbuf;
int ibuf;
cout << "Sku: ";
is >> buf;
sku(buf);
cout << "Name (no spaces): ";
is >> buf;
name(buf);
cout << "Qty: ";
is >> ibuf;
quantity(ibuf);
cout << "Qty Needed: ";
is >> ibuf;
qtyNeeded(ibuf);
cout << "Is taxed? (1/0): ";
is >> ibuf;
taxed(ibuf != 0);
cout << "Price: ";
is >> dbuf;
price(dbuf);
return is;
}
};
}
void dumpFile(fstream& f) {
f.open("ms4.txt", ios::in);
char ch;
while (!f.get(ch).fail()) {
cout.put(ch);
}
f.clear();
f.close();
}
using namespace sict;
void test() {
double res, val = 0.0;
fstream F("ms4.txt", ios::out);
F.close();
SItem S;
SItem T;
SItem U;
cout << "Enter Product info: " << endl;
cin >> S;
SItem V = S;
S.store(F);
T.load(F);
cout << "T: (store, load)" << endl;
cout << T << endl;
cout << "S: " << endl;
cout << S << endl;
cout << "V(S): " << endl;
cout << V << endl;
cout << "U=T & op= :" << endl;
U = T;
cout << U << endl;
cout << "Operator == :" << endl;
cout << "op== is " << (T == "1234" ? "OK" : "NOT OK") << endl;
cout << "op+=: " << endl;
U += 10;
cout << U << endl;
cout << "op+=double : " << endl;
res = val += U;
cout << res << "=" << val << endl;
}
int main() {
fstream F("ms4.txt", ios::out);
F.close();
SItem S;
SItem U("4321", "Rice");
cout << "Empty Prouduct:" << endl << S << endl;
cout << "U(\"4321\", \"Rice\"):" << endl << U << endl;
cout << "Please enter the following information:" << endl;
cout << "Sku: 1234" << endl;
cout << "Name(no spaces) : Blanket" << endl;
cout << "Qty : 12" << endl;
cout << "Qty Needed : 23" << endl;
cout << "Is taxed ? (1 / 0) : 1" << endl;
cout << "Price : 12.34" << endl;
test();
cout << "Please enter the following information:" << endl;
cout << "Sku: 1234" << endl;
cout << "Name(no spaces) : Jacket" << endl;
cout << "Qty : 12" << endl;
cout << "Qty Needed : 23" << endl;
cout << "Is taxed ? (1 / 0) : 0" << endl;
cout << "Price : 12.34" << endl;
test();
dumpFile(F);
cout << "----The End" << endl;
return 0;
}
This is my Product.h:
namespace sict {
class Product : public Streamable {
char sku_[MAX_SKU_LEN + 1];
char * name_;
double price_;
bool taxed_;
int quantity_;
int qtyNeeded_;
public:
Product();
Product(const char*, const char*, bool = true, double = 0, int = 0);
Product(const Product&);
virtual ~Product();
Product& operator=(const Product&);
//setters
void sku(const char*);
void price(double);
void name(const char*);
void taxed(bool);
void quantity(int);
void qtyNeeded(int);
//getters
const char* sku()const;
double price()const;
const char* name()const ;
bool taxed()const;
int quantity()const;
int qtyNeeded()const;
double cost()const;
bool isEmpty()const;
//member operators
bool operator==(const char*);
int operator+=(int);
int operator-=(int);
};
double operator+=(double, const Product&);
std::ostream& operator<<(std::ostream& ostr, const Product& p);
std::istream& operator >> (std::istream& istr, Product& p);
}
All the functions have been implemented except the last two, which are the IO operators.
Streamable class is an abstract class that has no implementations.
You did this wrong in many ways.
Best approach in your case is do it like that.
First define interfaces for stream operations, for your products:
class IStreamPrintable
{
public:
virtual std::ostream& PrintToStream(std::ostream& outStream) const = 0;
};
class IStreamReadable
{
public:
virtual std::istream& ReadFromStream(std::istream& inputStream) = 0;
};
Secondly define stream operators which will use this interfaces.
std::ostream& operator<<(std::ostream& out, const IStreamPrintable& printObject)
{
return printObject.PrintToStream(out);
}
std::istream& operator>>(std::istream& input, IStreamReadable& readObject)
{
return printObject.ReadFromStream(input);
}
Now you Product can inherit this interfaces:
class Product
: public IStreamPrintable
, public IStreamReadable
{
…
};
You do not have to implement it immediately. You can implement those methods in specific product classes SItem and it will work out of the box.
Your method virtual std::fstream& store(std::fstream& file, bool addNewLine = true) is total mess. You are passing fstream object and opening some specific file on it. This is wrong since you are unable to write multiple objects to single file. Keep there ostream object and do not change is state (do only writing), so you could cascade calls and so you could avoid hard-coding a file name.

Copy constructor issue [duplicate]

This question already has answers here:
C++: Argument Passing "passed by reference"
(4 answers)
Why is the copy constructor called when we pass an object as an argument by value to a method?
(3 answers)
Closed 6 years ago.
The program should resolve grade 1 equations in this specific manner. I had to use output messages for the constructors and destructors for better understanding of the code.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Ec {
public: float a, b; //equation's parameters
public:
Ec() {float x, y; cout <<"a= "; cin >> x; cout << "b= ";
cin >> y; a = x; b = y; cout << "void constr\n";};
Ec(float x, float y) { a = x; b = y; cout << "param constr\n"; }
~Ec() { cout << "destr\n"; }
Ec(Ec &z) { a = z.a; b = z.b; cout << "cpy constr\n"; }
friend float half1(Ec); //function to return a/2
friend float half2(Ec); //function to return b/2
friend float sol1(Ec); //function to return the solution for the standard eq
friend float sol2(Ec); //function to return the sol for the /2 param eq
};
float half1(Ec ec1) { return (ec1.a / 2);}
float half2(Ec ec1) { return (ec1.b / 2); }
float sol1(Ec ec1) { float x; return x = -ec1.b / ec1.a; }
float sol2(Ec ec1) { float x2; return x2 = -half2(ec1) / half1(ec1); }
int main()
{
int x, y;
cout << "a= ";
cin >> x;
cout << "b= ";
cin >> y;
Ec ec1;
Ec ec2(x, y);
Ec ec3 = ec1;
//the couts display for ex:" ec 2x+1=0 has sol -0.5"
cout << "ec " << ec1.a << "x+ " << ec1.b << "=0 has sol " << sol1(ec1) << endl;
cout << "ec " << ec2.a << "x+ " << ec2.b << "=0 has sol " << sol1(ec2) << endl;
cout << "ec " << ec3.a << "x+ " << ec3.b << "=0 has sol " << sol1(ec3) << endl;
cout << "ec halved " << half1(ec1) << "x+ " << half2(ec1) << "=0 has sol " << sol2(ec1) << endl;
cout << "ec halved " << half1(ec2) << "x+ " << half2(ec2) << "=0 has sol " << sol2(ec2) << endl;
cout << "ec halved " << half1(ec3) << "x+ " << half2(ec3) << "=0 has sol " << sol2(ec3) << endl;
}
return 0;
}
Now, I have troubles understanding why after the first cpy constructor(for ec3) another cpy constructor is called then a destructor. What is it doing?
Your functions take Ec objects by value
float half1(Ec ec1) { return (ec1.a / 2);}
^
These will make function local copies that are then destroyed at the end of each function.
If you want to avoid making these copies, pass the arguments by const reference
float half1(Ec const& ec1) { return (ec1.a / 2);}
^

Error with pointer when using dynamic_cast to detect derived class

I'd like to have some help on an issue I face.
I made an inheritance with polymorph program with class Shape and Circle (derived from shape). So I have some code like this
main.cpp
Shape* shape = new (nothrow) Shape[size]();
input_circle(shape);
show_circle_area(shape);
and a procedure in main.cpp too
void show_circle_area(Shape *mshape){
int i;
sort(mshape,mshape+totshape,sortByArea);
cout << "CIRCLE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Circle*> (mshape[i]))
cout << mshape[i].getWidth() << " " << mshape[i].getArea() << " " << mshape[i].getPerimeter() << endl;
}
when I run this program, I always got this error :
main2.cpp: In function 'void output_circle(Shape*)':
main2.cpp:66:39: error: cannot dynamic_cast '*(mshape + ((sizetype)(((unsigned int)i) * 40u)))' (of type 'class Shape') to type 'class Circle*' (source is not a pointer)
if (dynamic_cast<Circle*> (mshape[i]))
^
Anyone can help what should I do to fix this?
main.cpp (updated)
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include <fstream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <limits>
#include <typeinfo>
using namespace std;
const int size = 200;
int totshape = 0;
// INPUT FROM FILE
void input_circle(Shape* mshape[]){
ifstream file;
int i;
double r;
file.open("circle.txt");
while (file >> r){
Circle* crl = new (nothrow) Circle(r);
mshape[totshape]=crl;
totshape++;
}
file.close();
}
void input_rectangle(Shape* mshape[]){
ifstream file;
int i;
double w,h;
file.open("rectangle.txt");
while (file >> w >> h){
Rectangle* rec = new (nothrow) Rectangle(w,h);
mshape[totshape]=rec;
totshape++;
}
file.close();
}
void input_square(Shape* mshape[]){
ifstream file;
int i;
double s;
file.open("square.txt");
while (file >> s){
Square* sqr = new (nothrow) Square(s);
mshape[totshape]=sqr;
totshape++;
}
file.close();
}
//OUTPUT TO FILE
void output_circle(Shape *mshape[]){
int i;
ofstream file;
file.open("outcircle.txt");
file << "radius\tarea\tperimeter" << endl;
for (i=0;i<totshape;i++){
if (dynamic_cast<Circle*> (mshape[i]))
file << mshape[i]->getWidth() << "\t" << mshape[i]->getArea() << "\t" << mshape[i]->getPerimeter() << endl;
}
file.close();
}
void output_rectangle(Shape *mshape[]){
int i;
ofstream file;
file.open("outrectangle.txt");
file << "width\theight\tarea\tperimeter" << endl;
for (i=0;i<totshape;i++){
if (dynamic_cast<Rectangle*> (mshape[i]))
file << mshape[i]->getWidth() << "\t" << mshape[i]->getHeight() << "\t" << mshape[i]->getArea() << "\t" << mshape[i]->getPerimeter() << endl;
}
file.close();
}
void output_square(Shape *mshape[]){
int i;
ofstream file;
file.open("outsquare.txt");
file << "sisi\tarea\tperimeter" << endl;
for (i=0;i<totshape;i++){
if (dynamic_cast<Square*> (mshape[i]))
file << mshape[i]->getWidth() << "\t" << mshape[i]->getArea() << "\t" << mshape[i]->getPerimeter() << endl;
}
file.close();
}
//SORTING STL FOR AREA AND PERIMETER
bool sortByArea(Shape lhs[], Shape rhs[]) {
return lhs->getArea() < rhs->getArea();
}
bool sortByPerimeter(Shape lhs[], Shape rhs[]){
return lhs->getArea() < rhs->getArea();
}
//SHOW DATA SORT BY AREA
void show_shape_area(Shape *shape[]){
int i;
sort(shape,shape+totshape,sortByArea);
cout << "ALL SHAPE" << endl;
for (i=0;i<totshape;i++)
cout << shape[i]->getWidth() << " " << shape[i]->getWidth() << " " << shape[i]->getArea() << " " << shape[i]->getPerimeter() << endl;
}
void show_circle_area(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByArea);
cout << "CIRCLE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Circle*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
void show_rectangle_area(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByArea);
cout << "RECTANGLE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Rectangle*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getHeight() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
void show_square_area(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByArea);
cout << "SQUARE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Square*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
//SHOW DATA SORT BY PERIMETER
void show_shape_perimeter(Shape *shape[]){
int i;
sort(shape,shape+totshape,sortByPerimeter);
cout << "ALL SHAPE" << endl;
for (i=0;i<totshape;i++)
cout << shape[i]->getWidth() << " " << shape[i]->getWidth() << " " << shape[i]->getArea() << " " << shape[i]->getPerimeter() << endl;
}
void show_circle_perimeter(Shape *mshape[]){
int i;
//Shape * tempshape;
sort(mshape,mshape+totshape,sortByPerimeter);
cout << "CIRCLE" << endl;
for (i=0;i<totshape;i++)
//cout << "masuk for" << endl;
//tempshape=&mshape[i];
if (dynamic_cast<Circle*> (mshape[i])){
cout << mshape[i]->getWidth() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
//cout << "masuk" << endl;
}
}
void show_rectangle_perimeter(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByPerimeter);
cout << "RECTANGLE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Rectangle*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getHeight() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
void show_square_perimeter(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByPerimeter);
cout << "SQUARE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Square*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
//ADD DATA
void add_circle(Shape *mshape[]){
int input;
cout << endl << "Masukkan jari-jari : ";
while (!(cin >> input) || input < 0) // <<< note use of "short circuit" logical operation here
{
cout << "Input tidak valid" << endl;
cout << "Masukkan jari-jari : ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // NB: preferred method for flushing cin
}
Circle* crl = new (nothrow) Circle(input);
mshape[totshape]=crl;
totshape++;
}
void add_rectangle(Shape *mshape[]){
int inwidth, inheight;
cout << endl << "Masukkan panjang : ";
while (!(cin >> inwidth) || inwidth < 0) // <<< note use of "short circuit" logical operation here
{
cout << "Input tidak valid" << endl;
cout << "Masukkan panjang : ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // NB: preferred method for flushing cin
}
cout << endl << "Masukkan lebar : ";
while (!(cin >> inheight) || inheight < 0 || !(inheight < inwidth)) // <<< note use of "short circuit" logical operation here
{
cout << "Input tidak valid" << endl;
cout << "Masukkan lebar : ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // NB: preferred method for flushing cin
}
Rectangle* rec = new (nothrow) Rectangle(inwidth,inheight);
mshape[totshape]=rec;
totshape++;
}
void add_square(Shape *mshape[]){
int input;
cout << endl << "Masukkan sisi : ";
while (!(cin >> input) || input < 0) // <<< note use of "short circuit" logical operation here
{
cout << "Input tidak valid" << endl;
cout << "Masukkan sisi : ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // NB: preferred method for flushing cin
}
Square* sqr = new (nothrow) Square(input);
mshape[totshape]=sqr;
totshape++;
}
//DELETE DATA
//MAIN PROGRAM
int main(){
Shape* shape[size];
input_circle(shape);
input_rectangle(shape);
input_square(shape);
show_shape_area(shape);
show_shape_perimeter(shape);
show_circle_area(shape);
show_circle_perimeter(shape);
show_rectangle_area(shape);
show_rectangle_perimeter(shape);
show_square_area(shape);
show_square_perimeter(shape);
//add_circle(shape);
//show_circle_area(shape);
//add_rectangle(shape);
//show_rectangle_area(shape);
//add_square(shape);
//show_square_area(shape);
output_circle(shape);
output_rectangle(shape);
output_square(shape);
return 0;
}
Your problem is here:
mshape[totshape]=crl;
This assignment just copies the "Shape part" inside crl to mshape[totshape] thus mshape[totshape] is still a shape, not a circle.
In order to fix your problem, please use an array of Shape* pointers instead of Shape values:
Shape* shape[size]; // we should write this as size is a const
And, your input_***() functions:
void input_circle(Shape* mshape[]){
ifstream file;
int i;
double r;
file.open("circle.txt");
while (file >> r){
Circle* crl = new Circle(r);
mshape[totshape]=crl;
totshape++;
}
file.close();
}
Note that the function prototype is changed, please do it for other functions, and use mshape[i]->foo instead of mshape[i].foo
The casting now is: if (dynamic_cast<Circle*> (mshape[i])){
Don't forget to free memory before ending since we are using pointers:
for (int i = 0; i < totshape; i++) delete mshape[i];
This deleting would force you to make Shape's destructor be virtual:
class Shape {
public:
virtual ~Shape() {...}
}
Otherwise, Circle, Rectangle ... 's destructors will not be called.

Cannot call class function (parameterized class)

#include<iostream>
using namespace std;
class a
{
private:
int x;
int y;
public:
int getx()
{
return x;
}
int gety()
{
return y;
}
a()
{
x = 100;
y = 100;
}
void xmin()
{
x--;
}
void ab(a x)
{
x.xmin(); x.xmin(); x.xmin(); x.xmin();
}
};
void main()
{
a xx;
a yy;
cout << "xx" << endl;
cout << "x : " << xx.getx() << "y : " << xx.gety()<<endl;
cout << "yy" << endl;
cout << "x : " << yy.getx() << "y : " << yy.gety()<<endl;
xx.ab(yy);
cout << "xx" << endl;
cout << "x : " << xx.getx() << "y : " << xx.gety() << endl;
cout << "yy" << endl;
cout << "x : " << yy.getx() << "y : " << yy.gety() << endl;
}
Why the function x.xmin() in void ab(a x) cannot be executed properly? (The value of x didn't change as the function of xmin() decrease the value of x by 1.
This is the simple version of my code so that will be easier to understand :)
void ab(a x)
That takes its argument by value. The function modifies a local copy of the argument, so the caller won't see any changes. If you want the function to modify the caller's object, then pass by reference:
void ab(a & x)
^

c++ destructor didn't get called?

I have a class:
class Rectangle {
int width;
int height;
public:
Rectangle(int w, int h) {
width = w;
height = h;
cout << "Constructing " << width << " by " << height << " rectangle.\n";
}
~Rectangle() {
cout << "Destructing " << width << " by " << height << " rectangle.\n";
}
int area() {
return width * height;
}
};
int main()
{
Rectangle *p;
try {
p = new Rectangle(10, 8);
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}
cout << "Area is " << p->area();
delete p;
return 0;
}
This is a quite simple C++ sample. I compiled in Linux g++ and run it.
Suddenly I found the delete p did not call ~Rectangle() ...
I should see string like "Destructing " << width << " by " << height << " rectangle."
but I did not ....
but why?
Deleting an object should call that object's destructor, shouldn't it?
You haven't ended the line, so the line was not output. Add << endl to your printing.