c++ code

Write your question here.

#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance //English Distance class
{
private:
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0)
{ }
//constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
void getdist() //get length from user
{
cout << "\nEnter feet: "; cin >> feet;
cout << "Enter inches: "; cin >> inches;
}
void showdist() //display distance
{ cout << feet << "\’-" << inches << '\”'; }
void add_dist( Distance, Distance ); //declaration
};
//--------------------------------------------------------------//add lengths d2 and d3
/*void Distance::add_dist(Distance d2, Distance d3)
{
inches = d2.inches + d3.inches; //add the inches
feet = 0; //(for possible carry)
if(inches >= 12.0) //if total exceeds 12.0,
{ //then decrease inches
inches -= 12.0; //by 12.0 and
feet++; //increase feet
} //by 1
feet += d2.feet + d3.feet; //add the feet
}*/
////////////////////////////////////////////////////////////////
int main()
{
Distance dist1, dist3; //define two lengths
Distance dist2(11, 6.25); //define and initialize dist2
dist1.getdist(); //get dist1 from user
dist3.add_dist(dist1, dist2); //dist3 = dist1 + dist2
//display all lengths
cout << "\ndist1 = "; dist1.showdist();
cout << "\ndist2 = "; dist2.showdist();
cout << "\ndist3 = "; dist3.showdist();
cout << endl;
return 0;
}
help!
please any one explain above code.


Please put your code in code tags to make it more readable.

First thing I noticed was that you have a member function called add_dist declared in your class, you attempt to call it in your main yet the actual function has been commented out.

There may be other problems.

Here, we have a class, Distance with two private members, inches:double and feet:int.
A public default constructor that sets inches and feet to zero, another constructor(overload) that takes two arguments to be assigned to thei respective class variables. A method to diplay their values, set their values, and add two Distance objects.
In the main function, we create two Distance objects, 1 and 3, with the default constructor.
dist2 is created with initial values(through the overload ctor). dist1 is assigned values by the user by calling the getdist member function. dist3 is given the outcome of the sum of dist1 and dist2
Thei lengths are finally outputted.

Aceix.
Do you know class syntax? Do you understand the cout and what it does? Do you know what endl and other stuff mean?
In other words, do you know programming syntax?
If not, read the explanation below. If you do, ignore this comment.

Classes are ways to group variables together. Public: is used to let other classes and functions inherit the variables, and Private: is used for making sure the data doesn't get used by other classes or functions (though subclasses can use them)

cout: as in c- out, because the C programming language is displaying data from the computer "out" to the monitor, hence cout.)

cin: as in c-in. Basically vice versa from cout.

endl means end-line, or move the cursor one line over, like pressing Enter.
Last edited on
Topic archived. No new replies allowed.