Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++, Write a class template List for modeling a list of five elements which will have values from one of four different data types (int,

C++, Write a class template List for modeling a list of five elements which will have values from one of four different data types (int, float, char, Distance). This class should support these list manipulation tasks:

Initialize a list to zero values.

Initialize a list to the values stored in a list of the same type.

Allow for user input of a list.

Display a list.

Sort a list into ascending order.

Then, write a template function demo, that will accept a flag of the type data you would like and then create a list of that type to demonstrate the list class it will do these tasks:

Create a list of the selected type and display it, showing the initialized zero values it contains.

Have the user enter values into the list and then display the values.

Create a second list of the selected type, initializing it to the values stored in the first list, and then display the second list.

Sort the values in the first list.

Display the sorted list.

Program Requirements:

To perform the sorting, use either a selection or bubble sort algorithm. See the document posted with the assignments sort and search.doc for details on these algorithms.

The Distance class is given below:

class Distance //English Distance class

{

private:

int feet;

float inches;

public:

Distance() : feet(0), inches(0.0) //constructor (no args)

{ }

//constructor (two args)

Distance(int ft, float in) : feet(ft), inches(in)

{ }

Distance( float fltfeet ) //constructor (one arg)

{ //convert float to Distance

feet = int(fltfeet); //feet is integer part

inches = 12*(fltfeet-feet); //inches is what's left

}

bool operator < (Distance) const; //compare distances

friend istream& operator >> (istream& s, Distance& d);

friend ostream& operator << (ostream& s, Distance& d);

};

bool Distance::operator < (Distance d2) const

{

float bf1 = feet + inches/12;

float bf2 = d2.feet + d2.inches/12;

return (bf1 < bf2) ? true : false;

}

//--------------------------------------------------------------

istream& operator >> (istream& s, Distance& d) //get Distance

{ //from user

cout << " Enter feet: "; s >> d.feet; //using

cout << "Enter inches: "; s >> d.inches; //overloaded

return s; //>> operator

}

//--------------------------------------------------------------

ostream& operator << (ostream& s, Distance& d) //display

{ //Distance

s << d.feet << "\'-" << d.inches << '\"'; //using

return s; //overloaded

} //<< operator

Note: A char value can be initialized to zero when displayed, no output appears.

To test your templates, try using the following main driver:

int main()

{

int sel;

bool end=false;

int iFlag=0;

float fFlag=0;

char cFlag=0;

Distance dFlag;

cout << "TEMPLATE DEMO PROGRAM ";

do{

cout << "Enter list type (1=int 2=float 3=char 4=Distance 5=exit): ";

cin >> sel;

switch (sel)

{

case 1:

demo(iFlag);

break;

case 2:

demo(fFlag);

break;

case 3:

demo(cFlag);

break;

case 4:

demo(dFlag);

break;

default:

end=true;

cout << "Bye... ";

break;

}

}while(!end);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions