0% found this document useful (0 votes)
2 views

6.1 constructor example

The document contains a C++ program that defines a 'Distance' class to represent distances in feet and inches. It includes methods for getting user input, displaying distances, and performing addition and subtraction of distances. The main function demonstrates the usage of the class by creating instances, performing operations, and displaying the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

6.1 constructor example

The document contains a C++ program that defines a 'Distance' class to represent distances in feet and inches. It includes methods for getting user input, displaying distances, and performing addition and subtraction of distances. The main function demonstrates the usage of the class by creating instances, performing operations, and displaying the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

using namespace std;

class Distance

private:

int feet;

int inch;

public:

Distance (); //Constructor

void getDist ();

void showDist ();

Distance addDist( Distance d2 );

Distance subDist( 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:: subDist( Distance d2 )


{

Distance temp;

temp.feet = feet - d2.feet;

temp.inch = inch - d2.inch;

if( temp.inch < 0 )

temp.feet--;

temp.inch = 12 + temp.inch;

return temp;

int main()

Distance d1;

Distance d2;

Distance d3;

Distance d4;

cout << "Enter Distance1 : " << endl;

d1.getDist();

cout << "Enter Distance2 : " << endl;

d2.getDist();
d3 = d1.addDist(d2);

d4 = d1.subDist(d2);

cout << endl << "Distance1 : " ;

d1.showDist();

cout << endl << "Distance2 : " ;

d2.showDist();

cout << endl << "Distance3 : " ;

d3.showDist();

cout << endl << "Distance4 : " ;

d4.showDist();

cout << endl;

return 0;

You might also like