This program contains three parts: the header, the functions for the class and the main part that is interactive. However, it won't compile.
I keep getting the response that there is an expected constructor, destructor or type conversion.
#ifndef BOX_H
#define BOX_H
class Box
{
private:
double height;
double width;
double length;
public:
Box();
double setHeight();
double setWidth();
double setLength();
double getVolume();
double getSurfaceArea();
};
#endif
function.cpp:
#include "Box.hpp"
/**********************************************************************
Box:: Box
This is the default constructor that uses the set methods to initialize each field to 1.
* **********************************************************************/
Box::Box()
{
height = 1;
width = 1;
length = 1;
}
/*
Does anyone know what this section does? Is it another default constructor or is is it even needed?
Box::Box(double height, double width, double length)
{
setHeight(height);
setWidth(width);
setLength(length);
}
*/
double Box::setHeight()
{
return height;
}
double Box::setWidth()
{
return width;
}
double Box::setLength()
{
return length;
}
double Box::getVolume()
{
return height * width * length;
}
double Box::getSurfaceArea()
{
double SurAre = 0;
SurAre = (2 * (length * width)) + (2 * (length * height)) + (2 * (height * width));
return SurAre;
}
main.cpp:
#include <iostream>
#include "Box.hpp" //contains Box class declaration
using namespace std;
int main()
{
Box object;
double Alength;
double Awidth;
double Aheight;
cout << "This program will calculate the area of a box.\n";
cout << "What is the length?";
cin >> Alength;
cout << "What is the width?";
cin >> Awidth;
cout << "What is the height?";
cin >> Aheight;
object.setLength(Alength);
if (!object.setWidth(Awidth))
cout << "Invalid box width entered. \n" << endl;
if (!object.setHeight(Aheight))
cout << "Invalid box height entered. \n" << endl;
cout << "Volume: " << object.getVolume() << endl;
cout << "Surface Area: " << object.getSurfaceArea() << endl;
return 0;
}
Does anyone have an idea about why?
If you uncomment the three-parameter constructor, you will get an error message, because a constructor is a class member, and class members have to be declared inside the class before they can be used or defined outside.
Add the line
Box(double height, double width, double length);
inside your class definition, and then the additional constructor can be compiled also.
C++ has some strange behaviors: if you fail to declare and define a default constructor, it will do it for you. Think autogenerated. It will also define a compiler-generated copy constructor and destructor. It's helpful to understand this, because these things exist whether you define them or not.
You both declared the constructor in the header:
public:
Box();
and defined it in the cpp file:
Box::Box()
You correctly both declared and defined this constructor.
If you want any other constructor, you have to declare it before you can define it as well
public:
Box();
Box(double height, double width, double length); // this is new
and then you can uncomment your definition in the cpp file, and all should be well.
One other style point: the way you've defined your 3 parameter constructor is not great style. What happens is that you construct a Box, and when you do so, you use the default constructor for it's member variables of height, width, and depth. You then call 3 member functions to assign these variables. In C++ you can avoid all this by using an initializer list. The body of your 3 parameter constructor becomes
Box::Box(double height, double width, double length) :
height(height), width(width), length(length)
{}
What this says is "build me a Box, and as you do so, use the value of passed in height for member height, width for member width, length for member length". You save yourself an assignment of 0 as a default value for the member variables and 3 function calls by building your Box "out of the box" as it were with those values. This uses the copy constructor for your member variables rather than their default constructors followed by an assignment.
(technical note: as builtins, doubles technically don't have these constructors, but the semantics behave as if they do, so you can think of them as having them on first order thought.)
This means, additionally, that if you define a copy constructor:
Box(const Box& other);
then you can use that in other classes to initialize a Box in their initializer lists, e.g.
BunchOfBoxes(const Box& firstBox) :
m_firstBox(firstBox)
{}
and it will use your copy constructor from the Box class to do the same sort of initialize-on-build for BunchOfBoxes
You have a problem with your class declaration/definition:
Your setters should have parameters so you can use them as setters.
In your Box.hpp change
double setHeight();
double setWidth();
double setLength();
to
double setHeight(double _height);
double setWidth(double _width);
double setLength(double _length);
Then in your Box.cpp change
double Box::setHeight()
{
return height;
}
double Box::setWidth()
{
return width;
}
double Box::setLength()
{
return length;
}
to
double Box::setHeight(double _height)
{
height = _height;
return height;
}
double Box::setWidth(double _width)
{
width = _width;
return width;
}
double Box::setLength(double _length)
{
length = _length;
return length;
}
Related
The constructor of class "Circle" allows the radius to be specified via a parameter, while it is not possible to create objects of the Circle type without specifying the parameter. Also, automatic conversion of real numbers into Circle objects must not be allowed. The Set method, which does the same thing as a constructor, should also be supported, except that it allows the radius of an already created object to be changed later.
The Cylinder class constructor requires two parameters that represent the base radius and the height of the roller, respectively. Instances of this class also cannot be created without specifying the mentioned information. It should also support the "Set" function, which does the same thing as a constructor, except that it allows you to modify an already created object.
Both classes must have other methods (listed in code).
I need to use class Circle inside class Cylinder to enable calculating volume, area, and other functions.
#include <cmath>
#include <iostream>
class Circle {
double radius;
public:
Circle(double r);
void Set(double r);
double GetRadius() const;
double GetPerimeter() const;
double GetArea() const;
void Scale(double s);
void Print() const;
};
class Cylinder {
Circle baze;
double height;
public:
Cylinder(double r_baze, double h);
void Set(double r_baze, double h);
Circle GetBaze() const;
double GetRadiusOfBaze() const;
double GetHeight() const;
double GetArea() const;
double GetVolume() const;
void Scale(double s);
void Print() const;
};
int main() {
return 0;
}
Circle::Circle(double r) {
radius = r;
}
void Circle::Set(double r) {
radius = r;
}
double Circle::GetRadius() const { return radius; }
double Circle::GetPerimeter() const { return 2 * 4 * atan(1) * radius; }
double Circle::GetArea() const { return radius * radius * 4 * atan(1); }
void Circle::Scale(double s) {
radius *= s;
}
void Circle::Print() const {
std::cout << "R= " << GetRadius() << " O= " << GetPerimeter()
<< " P= " << GetRadius();
}
Cylinder::Cylinder(double r_baze, double h) {
baze.GetRadius() = r_baze;
height = h;
}
void Cylinder::Set(double r_baze, double h) {
baze.GetRadius() = r_baze;
height = h;
}
Circle Cylinder::GetBaze() const { return baze; }
double Cylinder::GetRadiusOfBaze() const { return baze.GetRadius(); }
double Cylinder::GetHeight() const { return height; }
double Cylinder::GetArea() const {
return baze.GetArea() * 2 + baze.GetPerimeter() * height;
}
double Cylinder::GetVolume() const { return baze.GetArea() * height; }
void Cylinder::Scale(double s) {
baze.GetRadius() *= s;
height *= s;
}
void Cylinder::Print() const {
std::cout << "R= " << baze.GetRadiusOfBaze() << " H= " << height
<< " P= " << GetArea() << " V= " << GetVolume();
}
I'm new to objected-oriented programming concept. Could you help me to understand where I'm making mistakes?
I cannot compile this, because I get errors:
57 : no matching function for call to ‘Circle::Circle()’
14: note: candidate: ‘Circle::Circle(double)’
14: note: candidate expects 1 argument, 0 provided
3: note: candidate: ‘constexpr Circle::Circle(const Circle&)’
3: note: candidate expects 1 argument, 0 provided
62, 70, 91 : lvalue required as left operand of assignment
Cylinder::Cylinder(double r_baze, double h) {
baze.GetRadius() = r_baze;
height = h;
}
In your Cylinder class, when your constructor is called, baze is implicitly initialized with a default constructor that does not exist.
You want to use an initializer list to handle that initialization, at which point the code inside your Cylinder constructor becomes unnecessary.
Cylinder::Cylinder(double r_baze, double h)
: baze(r_baze), height(h) {
}
Alternatively, you could provide functionally a default constructor for your Circle class, and then Set the radius in Cylinder's constructor, but that's more work.
Circle::Circle(double r=0.0) {
radius = r;
}
Cylinder::Cylinder(double r_baze, double h) {
baze.Set(r_baze);
height = h;
}
Also...
Please note that GetRadius returns a double and cannot be assigned to, so you will get an error on that line of code.
I have a following class and I am creating 1d array of that class. Class has two constructors. So when I create the array of class like 'Box b[5]',so the first constructor is getting called. What I like if there is a way I can initialize length,breadth,height at the time of declaration or definition or is there a way I can invoke the parameter-ed constructor of a class for same array object elements while/or after creating array b[x] (elements)?
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Box {
public:
// Constructor definition
Box ()
{
cout<<"without constructot"<<endl;
}
Box(double l, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main()
{
Box b[5];
}
Is there a way to assign a variable within a class with a value of a class method?
I was trying the example below, but it is obviously not working. Is there a correct way of doing this?
#include <iostream>
using namespace :: std;
class rectangle
{
public:
rectangle(double h, double w) {
height = h;
width = w;
};
double area(void) {
return height*width;
};
double myarea = (*this).area();
private:
double height, width;
};
int main(void) {
rectangle r(2.5, 3);
cout << "Area is: " << r.area() << endl;
cout << "Area is: " << r.myarea << endl;
return 0;
}
result
Area is: 7.5
Area is: 0
The problem is that member variables are initialized in the order they are declared within the class. Also, your constructor creates the object before you have actually initialized width and height, as you do your initial assignment in the body of the constructor, rather than in an initializer list. Thus, when myarea is initialized (in your code), width and height have not yet been set.
To fix this, you can re-order the members and move the initialization of width and height into a list:
class rectangle {
private:
double height, width; // These will NOW be set before "myarea" is calculated
public:
rectangle(double h, double w) : height{ h }, width{ w } {
// Any assignments done here will be AFTER initialization of "myarea"
}
double area(void) {
return height * width;
}
double myarea = area();
};
Also, you don't need the complex (*this).area() syntax - just area() will do.
EDIT: Another thing to remember is that the setting of myarea by calling the area() function will only be done once (at object creation); if you change either width or height later, you won't (automatically) change myarea.
My program is supposed to calculate the surface area and volume of a box. I am to have the following functions: setHeight, setWidth, setLength, getVolume, getSurfaceArea.
When I compile my program I get the following error message:
boxMain.cpp: In function ‘int main()’:
boxMain.cpp:21: error: no matching function for call to 'Box::getVolume(double&, double&, double&)’
Box.hpp:20: note: candidates are: double Box::getVolume()
boxMain.cpp:23: error: no matching function for call to ‘Box::getSurfaceArea(double&, double&, double&)’
Box.hpp:21: note: candidates are: double Box::getSurfaceArea()
Based on my searches on this site, most answers seem to suggest that the default constructor doesn't exit or isn't working. I don't know if that is my issue as well, but I wrote the code for my default constructor according to textbook, so I have trouble figuring out what I did wrong.
The missing lines in my code below are just the descriptions and comments that I removed.
Any help would be appreciated! Thank you all so much in advance.
Here is my .hpp file:
#include <iostream>
#ifndef Box_HPP
#define BOX_HPP
class Box {
private:
double h, w, l;
double height, width, length;
public:
void setHeight(double h);
void setWidth(double w);
void setLength(double l);
double getVolume();
double getSurfaceArea();
Box();
};
Here is my function .cpp file:
#include <iostream>
#include <iomanip>
#include "Box.hpp"
double height;
double width;
double length;
Box::Box() {
height = 1.0;
width = 1.0;
length = 1.0;
}
void setHeight(double h) {
if(h < 0) {
std::cout << "Error. Height must be positive."
<< std::endl;
}
else {
height = h;
}
}
void setWidth(double w) {
if(w < 0) {
std:: cout << "Error. Width must be positive."
<< std::endl;
}
else {
width = w;
}
}
void setLength(double l) {
if(l < 0) {
std::cout << "Error. Length must be positive."
<< std::endl;
}
else {
length = l;
}
}
double getVolume() {
return (height * width * length);
}
double getSurfaceArea() {
return (2.0 * length * height + 2.0 * width * height);
}
Here is my main .cpp file:
#include <iostream>
#include <fstream>
#include "Box.hpp"
int main() {
Box box1;
double h, w, l;
std::cout << "Enter height" << std::endl;
std::cin >> h;
std::cout << "Enter width" << std::endl;
std::cin >> w;
std::cout << "Enter length" << std::endl;
std::cin >> l;
void setHeight(double h);
void setWidth (double w);
void setLength (double l);
std::cout << "volume: "
<< box1.getVolume(h, w, l) << std::endl;
std::cout << "surface: "
<< box1.getSurfaceArea(h, w, l) << std::endl;
return 0;
}
Box::getVolume is declared as taking no parameters, but in line 21 you call it with 3 arguments:
box1.getVolume(h, w, l)
Use simply box1.getVolume() instead. Similarly for Box::getSurface().
Also, use Box:: in front of all member function definitions, like void Box::setHeight(double h) {...}, otherwise you end up defining free standing functions and you'll get linker errors since the member functions end up not defined.
vsoftco's answer is mostly correct so I have up voted him, although I feel it would be good to add some more information to the answer to help explain it a bit better as well as help clean up several other issues you have here.
The error you had clearly explains the problem if you look at it. It mentions boxMain.cpp in the function int main() as having the problem. It further gives you the line number and mentions that there is "no matching function" for the "call" and in both instances notes the call as well as the possible candidates, which in this case is the methods without parameters.
with that aside here are some other tips that may help you avoid further frustration:
Prefer forward declarations instead of includes in your header files. In your case you don't need #include <iostream> at all in your box.hpp. The reason is that anything in your header is effectively leaked into any files that include your header. This is mainly to help reduce circular dependancies as well as shorten compile times when making changes. The Pimpl Idiom is an example pattern used with this mantra. I would not recommend using naked pointers, however, instead stick with RAII types such as smart pointers or plain references.
You either didn't paste all of your code or your include guard is wrong. You need a closing #endif in your header.
You should use a convention to name your member variables to avoid conflicts and keep your large .cpp files easier to read. This is a preference more then a rule but it helps. Something like _height, mHeight, _mHeight rather then height. Lower case camel style variable names are common for locals and parameters. It also looks like you really don't need the h,w,l members at all.
You should probably use assert for your error handling so as to optimize it out during release builds. That is of coarse unless you are intending to show the end user the error message. You may also consider using std::cerr instead.
The height, width, length variables in your .cpp are also extraneous and do not do what you think.
You should use an initialization list in your constructor instead of doing the simple work of assigning to variables in the body. There are several special situations as well but this can get you started.
ex: Box::Box() : height(1.0), width(1.0), length(1.0){}
The only method in the cpp you fully qualified with Box:: is the constructor. You need to do that with all the methods as well. ex: double Box::getVolume()
You are also not calling the correct methods in your main function (in fact you are not calling methods at all. You are basically forward declaring them.) you should be using an object to invoke the methods. This is why you didn't see a complaint in the error log about missing Box::
There is one major error in your header file that has nothing to do with your class's declaration, but has to do with the header during the pre-compilation stage. Your header guard is not correct as you have first have your #include <iostream> before the guard; this should be after the guard. Second your #ifndef CLASS_NAME does not match your #define CLASS_NAME and you are missing the matching #endif at the bottom of your header file. I will remove the #include <iostream> since it is not needed. The reason I am doing this is because if a user enters a value that is less than or equal to zero it will be set to a default value of 1. So there is no need to print out any error messages for this class.
Your class is close to what you need. There are a few things that would help to improve your class and maybe this will help.
Box.h
#ifndef BOX_H
#define BOX_H
class Box {
private:
double m_width;
double m_length;
double m_height;
double m_volume;
double m_surfaceArea;
public:
Box(); // Default Constructor
Box( double width, double length, double height );
// virtual ~Box(); // Default Okay
void setWidth( double width );
void setLength( double length );
void setHeight( double height );
double getWidth() const;
double getLegnth() const;
double getHeight() const;
double getVolume() const;
double getSurfaceArea() const;
private:
// If You Need Either The Copy Constructor Or The Assignment Operator
// Remove Them From The Private Section Into The Public Section
// Where You May Need To Implement Them Where The Default May Not Be Appropriate
Box( const Box& c ); // Not Implemented
Box& operator=( const Box& c ); // Not Implemented
void calculateVolume();
void calculateSurfaceArea();
}; // Box
#endif // BOX_H
Box.cpp
#include "Box.h"
// -------------------------------------------------------------------------
// Box() - Default Box Will Have a 1 x 1 x 1 Dimension
Box::Box() :
m_width( 1.0 ),
m_length( 1.0 ),
m_height( 1.0 ),
m_volume( 0.0 ),
m_surfaceArea( 0.0 ) {
calculateVolume();
calculateSuraceArea();
} // Box
// -------------------------------------------------------------------------
// Box() - User Defined Constructor
Box::Box( double width, double length, double height ) :
m_volume( 0.0 ),
m_surfaceArea( 0.0 ) {
if ( width <= 0 ) {
m_width = 1.0;
} else {
m_width = width;
}
if ( length <= 0 ) {
m_length = 1.0;
} else {
m_length = length;
}
if ( height <= 0 ) {
m_height = 1.0;
} else {
m_height = height;
}
calculateVolume();
calculateSurfaceArea();
} // Box
// -------------------------------------------------------------------------
// setWidth()
void Box::setWidth( double width ) {
// First Check To See If Value Passed In Is Same Member Value
if ( width == m_width ) {
// Nothing To Do
return;
} else if ( width <= 0 ) {
m_width = 1.0
} else {
m_width = width;
}
calculateVolume();
calculateSurfaceArea();
} // setWidth
// -------------------------------------------------------------------------
// setLength()
void Box::setLength( double length ) {
// First Check To See If Value Passed In Is Same Member Value
if ( length == m_length ) {
// Nothing To Do
return;
} else if ( length <= 0 ) {
m_length = 1.0
} else {
m_length = length;
}
calculateVolume();
calculateSurfaceArea();
} // setLength
// -------------------------------------------------------------------------
// setHeight()
void Box::setHeight( double height ) {
// First Check To See If Value Passed In Is Same Member Value
if ( height == m_height ) {
// Nothing To Do
return;
} else if ( height <= 0 ) {
m_height = 1.0
} else {
m_height = height;
}
calculateVolume();
calculateSurfaceArea();
} // setHeight
// -------------------------------------------------------------------------
// getWidth()
double Box::getWidth() const {
return m_width;
} // getWidth
// -------------------------------------------------------------------------
// getLength()
double Box::getLength() const {
return m_length;
} // getLength
// -------------------------------------------------------------------------
// getHeight()
double Box::getHeight() const {
return m_height;
} // getHeight;
// -------------------------------------------------------------------------
// getVolume()
double Box::getVolume() const {
return m_volume;
} // getVolume
// -------------------------------------------------------------------------
// getSurfaceArea()
double Box::getSurfaceArea() const {
return m_surfaceArea;
} // getSurfaceArea
// -------------------------------------------------------------------------
// calculateVolume()
void Box::calculateVolume() {
m_volume = m_width * m_length * m_height;
} // calculateVolume
// -------------------------------------------------------------------------
// calculateSurfaceArea()
void Box::calculateSurfaceArea {
m_dSurfaceArea = (m_width * m_length * 2) +
(m_width * m_height * 2) +
(m_length * m_height * 2);
} // calculateSurfaceArea
With the design of this class the calculations for both the volume and surface area are private to this class only for all they do is an internal calculation that get saves into the classes member variable where there are public access methods available to retrieve them. Also each of the 3 dimensional setters does the same check as both constructors for values that are less than and equal to 0 and if so will default them to 1.
If the value that is passed into any of the setter functions is the same that is already stored the function will do nothing and just return. If the value is greater than 0 and is not equal to what is already saved, it will overwrite the member variable and call both of the private calculate methods to update the volume and surface area.
Now if you want to improve this for better performance then you can declare both calculate methods as inline, move them out of the cpp file and either add them into the header file after your class declaration and before the #endif or you can add an `#include "Box.inl" and just create another header file type with the same name as the class except with the inl extension and cut and paste these two functions in there.
I have some question for coding C++ program with header file.
This is my header.h file :
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double width, double length);
double get_perimeter();
double get_area();
void resize(double factor);
private:
double width;
double length;
double area;
double factor;
};
And this is my Question1.cpp file which store all the methods:
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double width, double length)
{
width = width; //I have no idea how to use this.something as its in Java
length = length; //Problem probably occurs at here
}
double Rectangle::get_perimeter()
{
return ((width * 2) + (length * 2)) ;
}
double Rectangle::get_area()
{
return (width * length);
}
void Rectangle::resize(double factor)
{
width *= factor;
length *= factor;
}
private:
double width;
double length;
double area;
double factor;
};
And lastly, here is my main method.cpp :
#include <iostream>
#include "header.h";
using namespace std;
int main()
{
Rectangle rectangle1(2.5,7.0);
cout << rectangle1.get_perimeter();
cout << rectangle1.get_area();
system("PAUSE");
return 0;
}
However, when I try to run the program, the system told me that there was build errors and unresolved externals which I have no idea why is it so. Could somebody please help me fix it?
Thanks in advance.
Your implementations should not look like
class Rectangle
{
public:
Rectangle(double width, double length) { .... }
but like
Rectangle::Rectangle(double width, double length) : width(width), length(length) {}
You need to include header.h in the implementation .cpp file and in any file that needs the definition of the Rectangle class. You also need include guards in your headers. And do not put using namespace std in a header. In fact, don't put it anywhere.
Change .h to ->
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double width, double length);
double get_perimeter();
double get_area();
void resize(double factor);
private:
double width;
double length;
double area;
double factor;
};
Then .cpp to->
#include <iostream>
#include "header.h"
using namespace std;
Rectangle::Rectangle(double width, double length)
{
this->width = width; //I have no idea how to use this.something as its in Java
this->length = length; //Problem probably occurs at here
}
double Rectangle::get_perimeter()
{
return ((this->width * 2) + (this->length * 2)) ;
}
double Rectangle::get_area()
{
return (this->width * this->length);
}
void Rectangle::resize(double factor)
{
this->width *= factor;
this->length *= factor;
}
This should work then.
Regards,
Luka
Few things to unpick here.
1) use this->width which is equivalent to java's this.width (In C++, this is a pointer). Actually some C++ programmers (including me) prefix member variables with m_. Then you can just write m_width = width.
2) include "header.h" at the top of Question1.cpp
3) avoid putting "using namespace std" in a header file, or you could get unintended namespace
clashes as your code expands. OK to put it in the separate source files though although some folk even discourage this.
4) depending on your compiler and linker, you'll need to link to various lib's that the iostream library uses. Perhaps if you tell us the compiler you're using, we can help you here.
5) you need to surround your header with
#ifndef ARBITRARY_TEXT_BUT_DISTINCT_FROM_ANY_OTHER_IN_YOUR_PROGRAM
#define ARBITRARY_TEXT_BUT_DISTINCT_FROM_ANY_OTHER_IN_YOUR_PROGRAM
...your code here
#endif
This is an include guard - helps prevent multiple inclusion of a header file contents.
In Question1.cpp you have to include header.h
And don't forget include guards in header.h
Also in Question1.cpp you must change
Rectangle(double width, double length)
to
Rectangle::Rectangle(double width, double length)
You need to tell your build system to compile "question1.cpp". Usually there is an "add existing file to project" menu item somewhere under "File".
And typically, your class and constructor would use a different name than the input parameter. Many people put a prefix at the begginning (mLength, iLength) or postfix at the end (length_ is common).
The other solution is to prefix the member variable with this->length, but that can get pretty messy after a while.
the big mistake that in the .cpp file you are supposed to implement only the methods not rewrite full class implementation try the following in .cpp file
Rectangle::Rectangle(double width, double length)
{
width = width; //I have no idea how to use this.something as its in Java
length = length; //Problem probably occurs at here
}
and don't include the methods in class{}; block and don't redefine your member variables
also don't forget to include the header file in the .cpp file
thanks
I wonder if you are getting linker errors as your cpp file is little weird
In the cpp file include the .h file and only implement the functions like
Rectangle::Rectangle(double width, double length){
//implement
}
double Rectangle::get_perimeter(){
//implement
}
double Rectangle::get_area(){
//implement
}
void Rectangle::resize(double factor){
//implement
}
When you want to split your class file into a *.cpp and a *.h file it always has the following form:
rectangle.h:
#ifndef __RECTANGLE_H_
#define __RECTANGLE_H_
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double width, double length);
double get_perimeter();
double get_area();
void resize(double factor);
private:
double width;
double length;
double area;
double factor;
};
#endif
now the rectangle.cpp should have the following form:
#include <iostream>
#include "rectangle.h"
using namespace std;
Rectangle(double width, double length)
{
width = width; //I have no idea how to use this.something as its in Java
length = length; //Problem probably occurs at here
}
double Rectangle::get_perimeter()
{
return ((width * 2) + (length * 2)) ;
}
double Rectangle::get_area()
{
return (width * length);
}
void Rectangle::resize(double factor)
{
width *= factor;
length *= factor;
}
so when as an explanation:
The header file tells you which fields and methods are available and the *.cpp file implements the methods.
In your main.cpp you just need o include the rectangle.h
this code below works
#include<iostream>
#include<iomanip>
using namespace std;
class student
{
private:
int area;//hours;
float perimeter;//gpa;
public:
void addcourse(int len, float wid)
{
float sum;
sum = area * perimeter;
area += len;
sum += wid * len;
perimeter = sum / area;
}
student() // constructor
{
area = 0;
perimeter= 0.0;
}
};
int main()
{
student s;
int length;//classhours;//l
float width;//classgpa;//w
cout << "Enter length ( 1 to end): ";
cin >> length;
while(length != 1)
{
cout << "Enter width: ";
cin >> width;
s.addcourse(length, width);
cout << "Enter length ( 1 to end): ";
cin >> length;
}
// cout << "Rectangle's length = " << r.getlength() << endl;
// cout << "Rectangle's width = " << r.getwidth() << endl;
// cout << "Rectangle's area = " << r.getarea() << endl;
// cout << "Rectangle's perimeter = " << r.getperimeter() << endl;
}