Class ship C++ program

0

Class ship C++ program:


Create a class called ship that incorporates a ship’s number and location. Use the approach of Question 4 to number each ship object as it is created. Use two variables of the angle class from Question 3 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.

 

Class ship c++ program


Codes of Program:
 

#include <iostream>

using namespace std;

class angles

{

            public:

            float degree;

            float min;

            char direc;

            void getdata()

            {

                        cout << "Enter the angle value between (180 and 0): " << endl;

                        cin >> degree;

                        cout << "now enter the minutes (1-60)" << endl;

                        cin >> min;

                        cout << "enter the direction (w,e,s,n)" << endl;

                        cin >> direc;

            }

};

class ship

{

            int serial;

            public:

            angles longitude, latitude;

            void intial();

            ship()

            {

                        static int count;

                        ++count;

                        serial = count;

            }

            void set_value();

            void get_loc();

            void print(angles longt, angles litit);

};

void ship::get_loc()

{

            angles inp;

            for (int i = 0; i < 2; i++)

            {

                        {

                                    inp.getdata();

                                    if (inp.direc == 'w' || inp.direc == 'e')

                                    longitude = inp;

                                    else if (inp.direc == 'n' || inp.direc == 's')

                                    latitude = inp;

                        }

            }

}

void ship::print(angles longt, angles litit)

{

            cout << "The ship serial number is :" << serial << endl;

            cout << "and it's position is :" << endl;

            cout << " "<<litit.degree << '\xF8' << latitude.min << " " << latitude.direc << " Latitute " << endl;

            cout << " "<<longt.degree << '\xF8' << longt.min << " " << longt.direc << " Longtitute" << endl;

}

int main()

{

            ship obj[3];

            for (int i = 0; i < 3; i++)

            {

                        if (i == 0)

                        cout << "Location of ship 1 :" << endl;

                        else if (i == 1)

                        cout << "Location of ship 2 :" << endl;

                        else if (i == 2)

                        cout << "Location of ship 3 is:" << endl;

                        obj[i].get_loc();

            }

            for (int i = 0; i < 3;i++)

            obj[i].print(obj[i].longitude, obj[i].latitude);

}


Out put of Program:


Class Ship



Post a Comment

0Comments
Post a Comment (0)