Reputation: 929
So here's the question:
Create a 'DISTANCE' class with:
Write an application program with a main function to create 2 objects of DISTANCE class, namely d1and d2. Then using object d3, sum the objects d1 and d2 and store the sum in d3 and then display the object d3.
I tried to do it but i'm getting error messages. Can someone tell me what's wrong with my program? I'm learning it on my own. Any help will be appreciated. Thank you. :)
Here's my code for the .h file:
#ifndef DISTANCE_H
#define DISTANCE_H
#include <iostream>
using namespace std;
class Distance
{
private:
double feet_1,feet_2;
double inches_1,inches_2;
public:
double inputfeet1(double feet1);
double inputfeet2(double feet2);
double inputinch1(double inch1);
double inputinch2(double inch2);
double sumFeet(double feets);
double sumInch(double inches);
};
#endif // DISTANCE_H
This one is for the .cpp file
#include "../include/Distance.h"
#include <iostream>
using namespace std;
double Distance::inputfeet1(double feet1)
{
if(feet1 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is used.";
}
else
{
feet_1 = feet1;
}
}
double Distance::inputfeet2(double feet2)
{
if(feet2 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is used.";
}
else
{
feet_2 = feet2;
}
double Distance::inputinch1(double inch1)
{
if(inch1 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is being used.";
}
else
{
inch_1 = inch1;
}
}
double Distance::inputinch2(double inch2)
{
if(inch2 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is being used.";
}
else
{
inch_2 = inch2;
}
}
double Distance::sumFeet()
{
return feet_1 + feet_2;
}
double Distance::sumInch()
{
return inches_1 + inches_2;
}
}
And this one is the main.cpp
#include "include/Distance.h"
#include <iostream>
using namespace std;
int main()
{
Distance d1,d2,d3;
double f1,f2,I1,I2;
double sum1,sum2;
cout<<"Distance 1.";
cout<<"\nEnter the feet: ";
cin>>f1;
cout<<"Enter the inch: ";
cin>>I1;
cout<<"Distance 2.";
cout<<"\nEnter the feet: ";
cin>>f2;
cout<<"Enter the inch: ";
cin>>I2;
d1.inputfeet1(f1);
d1.inputfeet2(f2);
d2.inputinch1(I1);
d2.inputinch2(I2);
sum1 = f1 + f2;
d3.sumFeet(sum1);
cout<<"Feet: "<<d3.sumFeet();
return 0;
}
Upvotes: 1
Views: 2682
Reputation: 2892
I see only minor errors, and they amount to what you would class as bad punctuation in language:)
I have annotated the changes I made ion the code below: mainly you need to indent your code and ensure that you match all open brackets with a closing bracket.
The next thing to do is ensure you have correct return types. I will post better instructions on the basics tonight when I get home and have the time to post a better tutorial rather than a fix with little instructive value.
I have more correctly indented the .cpp file for distance for you and if you change the code in main.cpp to have
sum1 = d3.sumFeet();
instead of
sum1 = f1 + f2;
d3.sumFeet(sum1);
it will work with the following modifications to the remaining code:
Distance.h
needs to be changed to the following:
#ifndef DISTANCE_H
#define DISTANCE_H
#include <iostream>
using namespace std;
class Distance
{
private:
double feet_1,feet_2;
double inches_1,inches_2;
public:
void inputfeet1(double feet1);
void inputfeet2(double feet2);
void inputinch1(double inch1);
void inputinch2(double inch2);
double sumFeet();
double sumInch();
};
#endif // DISTANCE_H
and Distance.cpp
needs to be amended as follows:
void Distance::inputfeet1(double feet1)
{
if(feet1 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is used.";
}
else
{
feet_1 = feet1;
}
}//added this
void Distance::inputfeet2(double feet2)
{
if(feet2 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is used.";
}
else
{
feet_2 = feet2;
}
}//added this too
void Distance::inputinch1(double inch1)
{
if(inch1 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is being used.";
}
else
{
inches_1 = inch1;
}
}
void Distance::inputinch2(double inch2)
{
if(inch2 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is being used.";
}
else
{
inches_2 = inch2;
}
}
double Distance::sumFeet()
{
return feet_1 + feet_2;
}
double Distance::sumInch()
{
return inches_1 + inches_2;
}
//removed curly brace here
To achieve your aims I would utilise a more object oriented approach, it may seem like overkill for such a simple problem, however the practice will pay off when you eventually start using larger constructs and hierarchies:
Basic needs:
Feet
and Inches
, we wont bother with them as they are effectively just different names for Doubles in your example code so they are fine as doubles.Feet
and Inches
, so we want to do something with that.... (lets call it DistanceBase
)Distance
So we will have a class called DistanceBase
composed of Feet
and Inches
and a class called Distance
composed of 2 DistanceBase
.
Header File:
As this is a conceptually trivial (not necessarily trivial in the C++ sense 1) class we can make everything be in the header file, that way it makes the code more portable and transparent, though less secure.
#pragma once
#ifndef DISTANCE_BASE_H
#define DISTANCE_BASE_H
/*
Header file for the DistanceBase class
#pragma once effectively does the same as the #ifndef loop on compilers that support it.
Both are included simply as a belt-and-braces approach
*/
// So that we have a nice simple and clear to read indexing system, lets set up an enum...
enum Units
{
FEET = 0, // typing Units::FEET will be the same as 0 but more readable and maintainable
INCHES = 1 // and likewise Units::INCHES will be the same as 1
};
class DistanceBase
{
private:
double distance[2]; // we can store the 2 doubles in one array (easier to throw around as a pair then:)
public:
void inputFeet(double feet) { distance[0] = feet; } // need a way to set the feet
void inputInches(double inch) { distance[1] = inch; } // and inches
double* getDistance() { return distance; } // a way to get both in one go
double getFeet() { return distance[Units::FEET]; } // a way to get just the feet
double getInches() { return distance[Units::INCHES]; } // a way to get just the inches
};
#endif // DISTANCE_BASE_H
Now that we have a class with such a simple level of complexity we can utilise it to create a simple class for your purposes, Distance
:
Header File
#pragma once
#ifndef DISTANCE_H
#define DISTANCE_H
/*
Header file for the distance class
*/
#include "DistanceBase.h"
class Distance
{
private:
DistanceBase *D1, *D2; // 2 instances of the DistanceBase class to use together
public:
Distance() // this is the constructor, where we can initialise
{ // the 2 instances we created earlier
D1 = new DistanceBase(); // initialise D1
D2 = new DistanceBase(); // initialise D2
}
DistanceBase* getD1() { return D1; } // this will be the function we use to access all of the properties and members of D1
DistanceBase* getD2() { return D2; } // this will be the function we use to access all of the properties and members of D2
double sumFeet() { return D1->getFeet()+D2->getFeet(); } // add the feet components of D1 and D2
double sumInches() { return D1->getInches()+D2->getInches(); } // add the inch components of D1 and D2
};
#endif // DISTANCE_H
Again, as this class is very simple (and for ease of posting) I have made everything fit in the header.
Now for the main function, which is now much more simple as the details are take care of by the respective classes:
#include "Distance.h"
#include <iostream>
using namespace std;
int main() {
Distance *dist = new Distance(); // instantiate a Distance class as dist
dist->getD1()->inputFeet(3); // set the feet of D1 to 3
dist->getD2()->inputFeet(7); // set the feet component of D2 to 7
cout << dist->getD1()->getFeet() << endl; // check that the values stored match the values input
cout << dist->getD2()->getFeet() << endl; // check that the values stored match the values input
cout << dist->sumFeet(); // add the 2 distances together
// now lets use user inputs:
cout << "Please input the first distance in feet: ";
// we can reuse the same variable to save CPU time and memory for all our inputs....
double tempValue;
cin >> tempValue;
dist->getD1()->inputFeet(tempValue);
cout << "Please input the first distances inch component: ";
cin >> tempValue;
dist->getD1()->inputInches(tempValue);
cout << "Please input the Second distance in feet: ";
cin >> tempValue;
dist->getD2()->inputFeet(tempValue);
cout << "Please input the second distances inch component: ";
cin >> tempValue;
dist->getD2()->inputInches(tempValue);
cout << dist->getD1()->getFeet() << endl; // check that the values stored match the values input
cout << dist->getD2()->getFeet() << endl; // check that the values stored match the values input
cout << dist->sumFeet() << endl; // add the 2 distances together
cout << dist->sumInches() << endl; // add the inches components together
return 0;
}
Some things you can try out to test yourself:
.cpp
and .h
files12.0
into feet and inchesFeet
class and Inches
class to encapsulate the functionality aboveint
for feet and float
for inches)add error handling to trap things like text input instead of numbers etc.
Let me know if this all seems OK, or if you need more info, I will happy to help :)
1 Trivial class: in C++ a trivial class
is one defined (defined with class
, struct
or union
) as both trivially constructible and trivially copyable, which implies that:
Upvotes: 1