S
soni29
hi,
i have written the following code, still in the learning stage:
#include<iostream.h>
class CBox {
public:
// Constructor definition
CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
m_Breadth(bv), m_Height(hv)
{
cout << endl << "Constructor called";
}
// Default Constructor
CBox() {
cout << endl
<< "Default constructor called.";
m_Length = m_Breadth = m_Height = 1.0;
}
// Function to calculate the volume of a box
double Volume() const {
return m_Length * m_Breadth * m_Height;
}
private:
double m_Length;
double m_Breadth;
double m_Height;
};
int main() {
CBox boxes[5];
CBox cigar(8.0, 5.0, 1.0);
cout << endl
<< "Volume of boxes[3] = " << boxes[3].Volume()
<< endl
<< "Volume of cigar = " <<cigar.Volume();
cout << endl;
return 0;
}
when i try to break it into a header and source file, 2 separate
files, i get errors:
source file:
#include <iostream.h>
class CBox;
int main() {
CBox boxes[5];
CBox cigar(8.0, 5.0, 1.0);
cout << endl
<< "Volume of boxes[3] = " << boxes[3].Volume()
<< endl
<< "Volume of cigar = " <<cigar.Volume();
cout << endl;
return 0;
}
CBox.h:
#include<iostream.h>
class CBox {
public:
// Constructor definition
CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
m_Breadth(bv), m_Height(hv)
{
cout << endl << "Constructor called";
}
// Default Constructor
CBox() {
cout << endl
<< "Default constructor called.";
m_Length = m_Breadth = m_Height = 1.0;
}
// Function to calculate the volume of a box
double Volume() const {
return m_Length * m_Breadth * m_Height;
}
private:
double m_Length;
double m_Breadth;
double m_Height;
};
\Microsoft Visual Studio .NET\Vc7\include\useoldio.h(29): warning
C4995: '_OLD_IOSTREAMS_ARE_DEPRECATED': name was marked as #pragma
deprecated
Test.cpp(5): error C2133: 'boxes' : unknown size
Test.cpp(5): error C2512: 'CBox' : no appropriate default constructor
available
Test.cpp(5): error C2262: 'boxes' : cannot be destroyed
Test.cpp(6): error C2079: 'cigar' uses undefined class 'CBox'
Test.cpp(6): error C2078: too many initializers
Test.cpp(6): warning C4244: 'initializing' : conversion from 'double'
to 'int', possible loss of data
Test.cpp(9): error C2036: 'CBox *' : unknown size
Test.cpp(9): error C2027: use of undefined type 'CBox'
Test.cpp(9): error C2228: left of '.Volume' must have
class/struct/union type
Test.cpp(11): error C2228: left of '.Volume' must have
class/struct/union type
is there a way to make it so that i can keep the files apart, if i
wanted all my classes to be in a header and the source to be separate,
please note that i wanted to make the constructors and the Volume
method inline that's why i put the code in the header.
Thank you.
i have written the following code, still in the learning stage:
#include<iostream.h>
class CBox {
public:
// Constructor definition
CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
m_Breadth(bv), m_Height(hv)
{
cout << endl << "Constructor called";
}
// Default Constructor
CBox() {
cout << endl
<< "Default constructor called.";
m_Length = m_Breadth = m_Height = 1.0;
}
// Function to calculate the volume of a box
double Volume() const {
return m_Length * m_Breadth * m_Height;
}
private:
double m_Length;
double m_Breadth;
double m_Height;
};
int main() {
CBox boxes[5];
CBox cigar(8.0, 5.0, 1.0);
cout << endl
<< "Volume of boxes[3] = " << boxes[3].Volume()
<< endl
<< "Volume of cigar = " <<cigar.Volume();
cout << endl;
return 0;
}
when i try to break it into a header and source file, 2 separate
files, i get errors:
source file:
#include <iostream.h>
class CBox;
int main() {
CBox boxes[5];
CBox cigar(8.0, 5.0, 1.0);
cout << endl
<< "Volume of boxes[3] = " << boxes[3].Volume()
<< endl
<< "Volume of cigar = " <<cigar.Volume();
cout << endl;
return 0;
}
CBox.h:
#include<iostream.h>
class CBox {
public:
// Constructor definition
CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
m_Breadth(bv), m_Height(hv)
{
cout << endl << "Constructor called";
}
// Default Constructor
CBox() {
cout << endl
<< "Default constructor called.";
m_Length = m_Breadth = m_Height = 1.0;
}
// Function to calculate the volume of a box
double Volume() const {
return m_Length * m_Breadth * m_Height;
}
private:
double m_Length;
double m_Breadth;
double m_Height;
};
\Microsoft Visual Studio .NET\Vc7\include\useoldio.h(29): warning
C4995: '_OLD_IOSTREAMS_ARE_DEPRECATED': name was marked as #pragma
deprecated
Test.cpp(5): error C2133: 'boxes' : unknown size
Test.cpp(5): error C2512: 'CBox' : no appropriate default constructor
available
Test.cpp(5): error C2262: 'boxes' : cannot be destroyed
Test.cpp(6): error C2079: 'cigar' uses undefined class 'CBox'
Test.cpp(6): error C2078: too many initializers
Test.cpp(6): warning C4244: 'initializing' : conversion from 'double'
to 'int', possible loss of data
Test.cpp(9): error C2036: 'CBox *' : unknown size
Test.cpp(9): error C2027: use of undefined type 'CBox'
Test.cpp(9): error C2228: left of '.Volume' must have
class/struct/union type
Test.cpp(11): error C2228: left of '.Volume' must have
class/struct/union type
is there a way to make it so that i can keep the files apart, if i
wanted all my classes to be in a header and the source to be separate,
please note that i wanted to make the constructors and the Volume
method inline that's why i put the code in the header.
Thank you.