I’m learning C++ and I’m stuck in following task.
Create a class called Distance that has a separate integer member data
for feet and inches. Implement the following tasks:
- A constructor that initializes the data members to zero
- A parameterized constructor that initializes the data members to fixed values
- A member function to add the two distances
- A member function to display the added distances respectively
- There should be copy constructor that copies an object (which stores the result of addition) to another one.
I have already completed all of the tasks. I just want to create a copy of constructor. Please also let me know if there is any mistake in my code.
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inch;
public:
Distance (); //Constructor
void getDist ();
void showDist ();
Distance addDist( Distance d2 );
};
Distance:: Distance ()
{
feet = 0; inch = 0;
}
void Distance:: getDist()
{
cout << "Enter Value of feets : "; cin >> feet;
cout << "Enter value of inches : "; cin >> inch;
inch = (inch >= 12) ? 12 : inch;
}
void Distance:: showDist()
{
cout << endl << "\tFeets : " << feet;
cout << endl << "\tInches: " << inch;
}
Distance Distance:: addDist( Distance d2 )
{
Distance temp;
temp.feet = feet + d2.feet;
temp.inch = inch + d2.inch;
if( temp.inch >= 12)
{
temp.feet++;
temp.inch -= 12;
}
return temp;
}
Distance Distance:: addDist(Distance &obj) {
Distance temp;
temp.feet = feet + d2.feet;
temp.inch = inch + d2.inch;
if( temp.inch >= 12)
{
temp.feet++;
temp.inch -= 12;
}
return temp;
}
int main()
{
Distance d1;
Distance d2;
Distance d3;
Distance d4;
d4 = d3; // This also calls copy constructor
cout << "Enter Distance 1 : " << endl;
d1.getDist();
cout << endl << "Enter Distance2 : " << endl;
d2.getDist();
d3 = d1.addDist(d2);
cout << endl << "Distance 1 : " ;
d1.showDist();
cout << endl << "Distance 2 : " ;
d2.showDist();
cout << endl << "Distance 3 = Distance 1 and Distance 2 :" ;
d3.showDist();
cout << endl << "Distance 4 Copy constructor allocating ptr." ;
d4.showDist();
cout << endl;
return 0;
}
>Solution :
There are several problems with your code.
Problem 1
Your default constructor assigns values to the data members feet and inches while in the question they’ve asked you to initialize those members. Note that in C++ assignment and initialization are 2 different things.
To solve this you should change your default constructor to as shown below:
//use constructor initializer list to initialize feet and inch
Distance:: Distance(): feet(0), inch(0)
{
//no need to assign here
}
Problem 2
In the question they’ve asked you to provide a parameterized constructor which you have not provided. You can solve this by adding it as shown below:
//define the parameterized constructor
Distance::Distance(int pfeet, int pinch): feet(pfeet), inch(pinch)
{
}
Also don’t forget to add the corresponding declaration Distance(int pfeet, int pinch); for the above shown parameterized constructor inside the class definition.
Problem 3
You can add the copy construtor as shown below:
//define the copy constructor
Distance::Distance(const Distance& obj): feet(obj.feet), inch(obj.inch)
{
}
Again add the declaration Distance(const Distance& obj); for the above shown copy constructor inside the class definition.
Problem 4
You have defined two version(overloads) of addDist but you only have declaration for one of them. In this case you should either add the declaration for the other one or remove the definition for which you don’t already have a declaration.
So the modified program looks like this(shown below):
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inch;
public:
Distance (); //Constructor
void getDist ();
void showDist ();
Distance addDist( Distance d2 );
//declaration for parameterized constructor
Distance(int pfeet, int pinch);
//declaration for copy constructor
Distance(const Distance& obj);
};
//use constructor initializer list to initialize feet and inch
Distance:: Distance(): feet(0), inch(0)
{
//no need to assign here
}
//define the parameterized constructor
Distance::Distance(int pfeet, int pinch): feet(pfeet), inch(pinch)
{
}
//define the copy constructor
Distance::Distance(const Distance& obj): feet(obj.feet), inch(obj.inch)
{
}
void Distance:: getDist()
{
cout << "Enter Value of feets : "; cin >> feet;
cout << "Enter value of inches : "; cin >> inch;
inch = (inch >= 12) ? 12 : inch;
}
void Distance:: showDist()
{
cout << endl << "\tFeets : " << feet;
cout << endl << "\tInches: " << inch;
}
Distance Distance:: addDist( Distance d2 )
{
Distance temp;
temp.feet = feet + d2.feet;
temp.inch = inch + d2.inch;
if( temp.inch >= 12)
{
temp.feet++;
temp.inch -= 12;
}
return temp;
}
int main()
{
Distance d1;
Distance d2;
Distance d3;
Distance d4;
cout << "Enter Distance 1 : " << endl;
d1.getDist();
cout << endl << "Enter Distance2 : " << endl;
d2.getDist();
d3 = d1.addDist(d2);
cout << endl << "Distance 1 : " ;
d1.showDist();
cout << endl << "Distance 2 : " ;
d2.showDist();
cout << endl << "Distance 3 = Distance 1 and Distance 2 :" ;
d3.showDist();
d4 = d3; //this uses assignment operator and not copy constructor
cout << endl << "Distance 4 Copy constructor allocating ptr." ;
d4.showDist();
cout << endl;
return 0;
}
The output of the above program can be seen here.