c++ - constructor overloading. Getting the wrong solution -


  #include <iostream>    #include <conio.h>    using namespace std;    class crectangle {       int * height, * width;       public: crectangle();       crectangle(int, int);       crectangle();       int area() {           return (*height * *width);       }   };    crectangle::crectangle() {       *height = 3;       *width = 5;   }   crectangle::crectangle(int a, int b) {       height = new int;       width = new int;       *height = a;       *width = b;   }   crectangle::~crectangle() {       delete height;       delete width;   }    int main() {       crectangle rect(1, 2);       crectangle rectb;       cout << "rect = " << rect.area() << "\n";       cout << "rectb = " << rectb.area();        getch();   } 

i getting area rect "6", instead of "2". can please point out mistake.

only 1 of constructors allocates memory width , height. other 1 has undefined behaviour.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -