help with understanding code
May 20, 2015 at 4:52pm UTC
Write your question here.
can someone please explain to me how this works below?
bool compare(const CBox* pbox)const
1 2 3 4 5 6 7 8 9 10 11
bool compare(const CBox* pbox)const
{
if (!pbox)
return false ;
return this ->volume() > pbox->volume();
}
if (pB2->compare(pB1))
cout << "match is greater than cigar" << endl;
else
cout << "match is less than or equal to cigar" << endl;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
class CBox
{
public :
explicit CBox(double lv, double wv = 1.0, double hv = 1.0) :m_length{ lv }, m_width{ wv }, m_Height{ hv }
{
}
CBox()
{
cout << "Default constructor called" << endl;
m_length = m_width = m_Height = 0;
}
double volume()const
{
return m_length*m_width*m_Height;
}
bool compare(const CBox* pbox)const
{
if (!pbox)
return false ;
return this ->volume() > pbox->volume();
}
private :
double m_length;
double m_width;
double m_Height;
};
int _tmain(int argc, _TCHAR* argv[])
{
CBox boxes[5];
CBox match{ 2.2, 1.1, 0.5 };
CBox cigar{ 8.0, 5.0, 1.0 };
CBox* pB1{ &cigar };
CBox*pB2{};
pB2 = &match;
if (pB2->compare(pB1))
cout << "match is greater than cigar" << endl;
else
cout << "match is less than or equal to cigar" << endl;
pB1 = boxes;
boxes[2] = match;
cout << "Volume of boxes[2] is" << (pB1 + 2)->volume() << endl;
return 0;
}
thanks for the help
Last edited on May 20, 2015 at 4:52pm UTC
May 20, 2015 at 5:09pm UTC
It would be better if you explain the code first as well as you can. Then we know what you already know and can help with the rest. (If you state that you don't understand any of it, then you should study the basics.)
May 21, 2015 at 10:11am UTC
I understand most of it. I come from a java background.
I have put comments into my code.
Thanks for the help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include "stdafx.h"
#include<iostream>
using std::endl;
using std::cout;
class CBox
{
public :
// Constructor definition
explicit CBox(double lv, double wv = 1.0, double hv = 1.0) :m_length{ lv }, m_width{ wv }, m_Height{ hv }
{
}
//default constructor
CBox()
{
cout << "Default constructor called" << endl;
m_length = m_width = m_Height = 0;
}
// Function to calculate the volume of a box
double volume()const
{
return m_length*m_width*m_Height;
}
// Function to compare two boxes which returns true
// if the first is greater than the second, and false otherwise
// *****i dont understand this part properly********
bool compare(const CBox* pbox)const
{
if (!pbox)
return false ;
return this ->volume() > pbox->volume();
}
private :
double m_length;
double m_width;
double m_Height;
};
int _tmain(int argc, _TCHAR* argv[])
{
CBox boxes[5]; // Array of CBox objects defined
CBox match{ 2.2, 1.1, 0.5 }; //Declare match box
CBox cigar{ 8.0, 5.0, 1.0 }; // Declare cigarBox
CBox* pB1{ &cigar };// Initialize pointer to cigar object address
CBox*pB2{}; // Pointer to CBox initialized to nullptr
pB2 = &match;
if (pB2->compare(pB1)) // Compare via pointers
cout << "match is greater than cigar" << endl;
else
cout << "match is less than or equal to cigar" << endl;
pB1 = boxes;
boxes[2] = match;
cout << "Volume of boxes[2] is" << (pB1 + 2)->volume() << endl; // Now access through pointer
return 0;
}
May 21, 2015 at 10:36am UTC
1 2 3 4 5 6
bool compare(const CBox* pbox)const
{
if (!pbox)
return false ;
return this ->volume() > pbox->volume();
}
this accepts CBox pointer as right hand value to compare.
you can also overload
==
operator, so that you can write
if ( pB2 == pBl )
Last edited on May 21, 2015 at 10:37am UTC
May 21, 2015 at 10:45am UTC
The pbox is a pointer. Pointer can be null (
pB2->compare( 0 )
). You cannot call a member function via null pointer (because there is no valid object at address 0).
Therefore, there is a test. One could rewrite it in more than one way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
bool compare( const CBox* pbox ) const
{
if ( ! pbox ) // 0==false, so !0==true
{
return false ;
}
else
{
return this ->volume() > pbox->volume();
}
}
bool compare( const CBox* pbox ) const
{
if ( ! pbox ) return false ;
return this ->volume() > pbox->volume();
}
bool compare( const CBox* pbox ) const
{
if ( nullptr == pbox ) return false ;
return this ->volume() > pbox->volume();
}
bool compare( const CBox* pbox ) const
{
if ( pbox )
{
return this ->volume() > pbox->volume();
}
else
{
return false ;
}
}
A reference always refers to an object:
1 2 3 4
bool compare( const CBox & pbox ) const
{
return this ->volume() > pbox.volume();
}
May 21, 2015 at 12:34pm UTC
Thanks very much for the help.
Last edited on May 21, 2015 at 12:34pm UTC
Topic archived. No new replies allowed.