Create a class called ship
that incorporates a ship’s number and location. Use the approach of Exercise 8
to number each ship object as it is created. Use two variables of the angle
class from Exercise 7 to represent the ship’s latitude and longitude. A member function
of the ship class should get a position from the user and store it in the
object; another should report the serial number and position. Write a main()
program that creates three ships, asks the user to input the position of each,
and then displays each ship’s number and position.
SOLUTION:
#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class ship
{
private:
int
s_num, degrees; static int count; float mints; char direc;
public:
ship(
) {
count++; s_num=count; }
void
get_position( )
{
cout << "Enter Position of
Ship " << s_num << ": ";
cin >> degrees >> mints
>> direc;
}
void
show_ship( ) const
{
cout
<< "Ship Number " << s_num << "is at the
position of ";
cout
<< degrees << "\xF8" << mints << "'
" << direc << endl;
}
};
int ship::count=0;
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
ship
ship1,ship2,ship3;
cout
<< "Input the Positions in Degrees, Minutes & Direction
Format.\n";
ship1.get_position(
); ship2.get_position( ); ship3.get_position( );
ship1.show_ship(
);
ship2.show_ship(
);
ship3.show_ship(
);
system("pause");
}
OUTPUT:
0 comments: