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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -