Thread: Constructor
View Single Post
  #1 (permalink)  
Old March 17th, 2006, 11:45 AM
ramess ramess is offline
Registered User
 
Join Date: Mar 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Constructor

I was trying to run a C++ program from Ivor's book. I defined a constructor to my program with default values and for values that user provide. But when i created an instance of that class using pointer object and i initialized this object using other object of same class using address of operator. The program compiled fine and i have the answer i need. But i was just wondering how did this work though i didnt have any constructor initialized for that particular case. I have the program typed below. I appreciate any help.

"box.h"
class box
{
public:
    box(double lvalue = 1, double bvalue = 1, double hvalue = 1);
    double volume();
private:
    double length;
    double breadth;
    double height;
};


#include <iostream>
#include "box.h"
using namespace std;

box::box(double lvalue, double bvalue, double hvalue): length(lvalue), breadth(bvalue), height(hvalue)
{
    if(length < 0)
        length = 1;
    if(breadth < 0)
        breadth = 1;
    if(height < 0)
        height = 1;
}

double box::volume()
{
    return length * breadth * height;
}

int main ()
{
  cout << endl;
  box firstbox(2.2,1.1,0.5);
  box secondbox;
  box* pthirdbox(&firstbox);
  cout << "volume of first box = " << firstbox.volume() << endl;
  cout << "volume of second box = " << secondbox.volume() << endl;
  cout << "volume of third box = " << pthirdbox->volume() << endl;
  return 0;
}
Reply With Quote