Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m working on my C++ const project, the project content three files, RoadDrive

ID: 3561603 • Letter: I

Question

I'm working on my C++ const project, the project content three files, RoadDriver.cpp, Road.cpp, Road.h

So the problem is that my project runs fine, but my first object copy test "const Road copy = road;" come out with wrong answers.

Here is what I wrote so far:

RoadDriver.cpp

#include
using namespace std;

#include "Road.h"
#include "Road.h" // testing ifndef

int main()
{
// print my name and this assignment's title
cout << "Lab 1a, The "Class Programming And Testing" Program ";
cout << "Programmer: Tien-Hui Feng ";
cout << "Editor(s) used: CodeBlocks ";
cout << "Compiler(s) used: CodeBlocks ";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

//set class, number, output.
Road road (5,10);
cout << "Width expected result is 5 feet ";
cout << "Actual result is: " << road.getWidth() << endl;
cout << "Length expected result is 10 mile ";
cout << "Actual result is: " << road.getLength() << endl;
cout << "Asphalt expected result is 44000 ";
cout << "Actual result is: " << road.asphalt(2) << endl << endl;

// object copy testing
{
const Road copy = road;
cout << "Object Copy Testing ";
cout << "Testing the fun function. Expected result is 5 ";
cout << " Actual result is " << copy.getWidth() << endl;
cout << "Testing the fun function. Expected result is 10 ";
cout << " Actual result is " << copy.getLength() << endl;
cout << "Asphalt expected result is 44000 ";
cout << " Actual result is " << copy.asphalt(2) << endl << endl;

}

// object assignment testing
{
Road copy (0,0); copy = road;
cout << "Object Copy Testing ";
cout << "Testing the fun function. Expected result is 5 ";
cout << " Actual result is " << copy.getWidth() << endl;
cout << "Testing the fun function. Expected result is 10 ";
cout << " Actual result is " << copy.getLength() << endl;
cout << "Asphalt expected result is 44000 ";
cout << " Actual result is " << copy.asphalt(2) << endl;
}
}

Road.cpp

// Lab 1a, The "Class Programming And Testing" Program
// Programmer: Tien-Hui Feng
// Editor(s) used: CodeBlocks
// Compiler(s) used: CodeBlocks

#include "Road.h"

Road::Road(float w, float l)
{
Width =w;
Length =l;
}

Road::Road(const Road& )
{

} ;

const void Road::setWidth(float width)
{
Width = width;
}

const void Road::setLength(float length)
{
Length = length;
}

float Road::getWidth() const
{
return Width;
}

float Road::getLength() const
{
return Length;
}

float Road::asphalt(float Thickness) const
{
return Width * Length*5280*Thickness*0.08333334;
}

Road.h

// Lab 1a, The "Class Programming And Testing" Program
// Programmer: Tien-Hui Feng
// Editor(s) used: CodeBlocks
// Compiler(s) used: CodeBlocks

#ifndef Road_h
#define Road_h

class Road
{
public:
Road(float w, float l); // a empty constructor
Road(const Road& w); //the copy constructor
const void setWidth(float width); // setter
const void setLength(float length); // setter
float getWidth() const; // getter
float getLength() const; // getter
float asphalt(float Thickness) const;

float Width;
float Length;
};

#endif

please help me to fix my project, thanks

Explanation / Answer

You have done one mistake in copy constructor, which is highlighted in bold, except that everything else is correct. Please look into the following code which works successfully.

#include
using namespace std;

#include "Road.h"
#include "Road.h" // testing ifndef

int main()
{
// print my name and this assignment's title
cout << "Lab 1a, The "Class Programming And Testing" Program ";
cout << "Programmer: Tien-Hui Feng ";
cout << "Editor(s) used: CodeBlocks ";
cout << "Compiler(s) used: CodeBlocks ";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

//set class, number, output.
Road road (5,10);
cout << "Width expected result is 5 feet ";
cout << "Actual result is: " << road.getWidth() << endl;
cout << "Length expected result is 10 mile ";
cout << "Actual result is: " << road.getLength() << endl;
cout << "Asphalt expected result is 44000 ";
cout << "Actual result is: " << road.asphalt(2) << endl << endl;

// object copy testing
{
const Road copy = road;
cout << "Object Copy Testing ";
cout << "Testing the fun function. Expected result is 5 ";
cout << " Actual result is " << copy.getWidth() << endl;
cout << "Testing the fun function. Expected result is 10 ";
cout << " Actual result is " << copy.getLength() << endl;
cout << "Asphalt expected result is 44000 ";
cout << " Actual result is " << copy.asphalt(2) << endl << endl;

}

// object assignment testing
{
Road copy (0,0); copy = road;
cout << "Object Copy Testing ";
cout << "Testing the fun function. Expected result is 5 ";
cout << " Actual result is " << copy.getWidth() << endl;
cout << "Testing the fun function. Expected result is 10 ";
cout << " Actual result is " << copy.getLength() << endl;
cout << "Asphalt expected result is 44000 ";
cout << " Actual result is " << copy.asphalt(2) << endl;
}
}

Road.cpp

// Lab 1a, The "Class Programming And Testing" Program
// Programmer: Tien-Hui Feng
// Editor(s) used: CodeBlocks
// Compiler(s) used: CodeBlocks

#include "Road.h"

Road::Road(float w, float l)
{
Width =w;
Length =l;
}

Road::Road(const Road& w)
{
Width = w.Width ;
Length = w.Length;
}

const void Road::setWidth(float width)
{
Width = width;
}

const void Road::setLength(float length)
{
Length = length;
}

float Road::getWidth() const
{
return Width;
}

float Road::getLength() const
{
return Length;
}

float Road::asphalt(float Thickness) const
{
return Width * Length*5280*Thickness*0.08333334;
}

Road.h

// Lab 1a, The "Class Programming And Testing" Program
// Programmer: Tien-Hui Feng
// Editor(s) used: CodeBlocks
// Compiler(s) used: CodeBlocks

#ifndef Road_h
#define Road_h

class Road
{
public:
Road(float w, float l); // a empty constructor
Road(const Road& w); //the copy constructor
const void setWidth(float width); // setter
const void setLength(float length); // setter
float getWidth() const; // getter
float getLength() const; // getter
float asphalt(float Thickness) const;

float Width;
float Length;
};

#endif