Q)In ocean navigation, locations are measured in degrees and minutes of latitude and longitude. Thus if you’re lying off the mouth of Papeete Harbor in Tahiti, your location is 149degrees 34.8 minutes west longitude, and 17 degrees 31.5 minutes south latitude. This iswritten as 149°34.8’W, 17°31.5’S. There are 60 minutes in a degree. (An older systemalso divided a minute into 60 seconds, but the modern approach is to use decimal minutesinstead.) Longitude is measured from 0 to 180 degrees, east or west from Greenwich,England, to the international dateline in the Pacific. Latitude is measured from 0 to 90degrees, north or south from the equator to the poles.Create a class anglethat includes three member variables: an intfor degrees, a floatfor minutes, and a charfor the direction letter (N, S, E, or W). This class can hold eithera latitude variable or a longitude variable. Write one member function to obtain an anglevalue (in degrees and minutes) and a direction from the user, and a second to display theangle value in 179°59.9’E format. Also write a three-argument constructor.
Create a class called ship that incorporates a ship’s number and location. Use the approach of Exercise 1 to number each ship object as it is created. Use two variables of the angle class from Exercise 7(Above) 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.
#include<iostream>
using namespace std;
class Angle { // to
store the angle of a ship
int
degree; //variable to store the degree
float min;
//variable to store the min
char dir;
//variable to store the direction
public:
Angle() :degree(0), min(0), dir(0) {} //deafault
constructor intizalizes the private members to zero
Angle(int de, float mi, char an)
:degree(de), min(mi), dir(an) {} //constructor
intizalizes the private members to accordngto the user given values
//set_angle(int, float,
char);
void
display(); //declaation of a function to display the angle
};
class Ship { // to
store the ship position
int
serial_no; //variable to store the serial no.
static int num;
Angle
lattitude, longitude; //objects to store the lattitude and
longitude of a ship
public:
Ship(); //constructor to set
the serial number
int
get_serial(); //declaration of a function to display serial number
void
set_ship_direction(Angle, Angle); //declaration
of a function to set the lattitude and longitude of a ship
void
display_angle(); //declaration of a function to display the
ship postion
};
int Ship::num
= 0;
Angle
set_A(); //declaration of a function to take the position from the user
int
main()
{
Angle lat,
longi; //creating objects of Angle class
Ship S1,
S2, S3; //creating object of Ship class
cout << "Ship
#" << S1.get_serial() <<
endl;
cout << "
Enter the Lattitude\n";
lat =
set_A(); //calling of a function to take the lattitude from the user
cout << "
Enter the Longitude \n";
longi =
set_A(); //calling of a
function to take the lattitude from the user
S1.set_ship_direction(lat, longi); //set
the lattitude and longitude of ship 1
cin.clear();
cout << "Ship
#" << S2.get_serial() <<
endl;
cout << "
Enter the Lattitude\n";
lat =
set_A(); //calling of a function to take the lattitude from the user
cout << "
Enter the Longitude \n";
longi =
set_A(); //calling of a
function to take the longitude from the user
S2.set_ship_direction(lat, longi);; //set
the lattitude and longitude of ship 2
cin.clear();
cout << "Ship
#" << S3.get_serial() <<
endl;
cout << "
Enter the Lattitude\n";
lat =
set_A(); //calling of a function to take the lattitude from the user
cout << "
Enter the Longitude \n";
longi =
set_A(); //calling of a function to take the longitude from the user
S3.set_ship_direction(lat, longi); //set
the lattitude and longitude of ship 3
//displyas the postions of
ships
S1.display_angle();
S2.display_angle();
S3.display_angle();
system("pause");
}
/*void
Angle::set_angle(int d, float m, char a) //function to set the angle
{
degree
= d;
min =
m;
dir =
a;
}*/
void Angle::display()
//function to display the angle
{
//cout << "Ship
#" << serial_no << endl;
cout <<
degree << "\xF8" << min << "\'
" << dir <<
endl;
}
Ship::Ship()
//constructor to set the serial number
{
num++;
serial_no = num;
}
void Ship::set_ship_direction(Angle la, Angle lon) //
function to set the lattitude and longitude of a ship
{
lattitude = la;
longitude = lon;
}
void Ship::display_angle()
//function to display the ship postion
{
cout << "Ship
#" << serial_no <<
endl;
cout << " Lattitude :";
lattitude.display();
cout << " Longitude :";
longitude.display();
}
Angle
set_A() // function to take
the position from the user
{
int d; float m; char a;
cout << " Degree :";
while (!(cin >> d))
{
cin.clear();
cin.ignore(900, '\n');
cout << " Again Enter the Degree:";
}
cout << " Minuties :";
while (!(cin >> m))
{
cin.clear();
cin.ignore(900, '\n');
cout << " Again Enter the Minutes:";
}
cout << " Direction :";
while (!(cin >> a))
{
cin.clear();
cin.ignore(900, '\n');
cout << " Again Enter the Direction:";
}
return Angle(d,
m, a);
}
int Ship::get_serial()
//function to display serial number
{
return
serial_no;
}
No comments:
Post a Comment