Wrox Programmer Forums
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 17th, 2006, 11:45 AM
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.

class box
    box(double lvalue = 1, double bvalue = 1, double hvalue = 1);
    double volume();
    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
  #2 (permalink)  
Old March 18th, 2006, 07:23 PM
Authorized User
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark

pthirdbox is a pointer variable of type, pointer to box, the '*' denotes this variable a pointer. a pointer is a 4 byte field that contains an address which points to an instance of the specified type attached to it, in this case an object of type box. The way that you have chosen to write your declaration/definition "box* pthirdbox(&firstbox);" is function initialization form. what is happening is you are declaring a pointer of type, pointer to box, and initializing it to point to the object firstbox by assigning the address of the first box, with the '&' operator which is the address of operator, to the pointer pthirdbox. It is not a new instance of a third box. In your cout statements: cout << "volume of first box = " << firsttbox.volume() << endl; and cout << "volume of third box = " << pthirdbox->volume() << endl; notice how you dereference the class member volume() in the first one you dereference it with the '.'(dot) Operator which is the direct member access operator and in the second you dereference it with '->' which is the indirect member access operator. So you are indirectly accessing the class member volume() of the firstbox object thru the pointer psthirdbox via the -> operator.
Reply With Quote

Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor nayeem69 .NET Framework 2.0 1 July 17th, 2007 12:33 AM
private constructor in C# n.nsivakumar C# 1 August 16th, 2006 07:08 AM
Problem with a constructor Mishkina General .NET 0 September 23rd, 2005 08:26 AM
Constructor problem yengzhai C++ Programming 6 May 18th, 2005 11:05 AM
Constructor With a Default? n6532l Visual C++ 2 September 18th, 2003 02:16 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.