Program in C++ to Show the Direction of a Ship

by | Mar 22, 2020 | Programming

In this article, I am going to show you how you can make a program in C++ programming language by creating a class called Ship, which will get some inputs from the user, and then the program will show the exact location of the ships according to the “Latitude” and “Longitude” and the “Minutes” that we entered…

Actually this program that I am about to show you was my assignment in my University and I had to search a lot but couldn’t find a website that help with these kind of simple and “college/Uni material” programs that is why I am here to help you all guys with some simple but life saving programs so without further do lets get started:

#include\"stdfx.h\"

Creating a Program in Object Oriented Programming (OOP)

after you attach that particular header file in “MS Visual Studio” now you can follow along. (Note: when you open \”Visual Studio\” this header file might have already attached so remove all other codes except this line).

Attach the following header file:

#include<iostream>
using namespace std:

Now we have to make two “classes” in C++ to get the Details and show it in you console. Now lets make a class:

class angle {
    Private:
        int degrees;
        float minutes;
        char direction;

    Public:
        void getdata() {
            cout << "\nEnter Degrees: ";
            cin >> degrees;
            cout << "\nEnter Minutes: ";
            cin >> minutes;
            cout << "\nEnter Direction (E, W, N, S): ";
            cin >> direction;
        }
        void display1() {
            cout << "\nLatitude : " << degrees << "\xf8" << minutes << "'" << direction << endl;
        }
        void display() {
            cout << "\nLongitude : " << degrees << "\xf8" << minutes << "." << direction << endl;
        }
};

Since we’ve built a class to record the angles of our ship. Now we have to makes ships to use this class in those ships to get multiple results for each ship.

Class Ship {
    Private:
        angle latitude;
        angle longitude;
        int shipnumber;
        static int count;

    Public:
        void getposition() {
            count++;
            shipnumber = count;
            cout << "\nEnter direction on Latitude: ";
            latitude.getdata();
            cout << "\nEnter Direction on Longitude: ";
            longitude.getdata();
        }
        void display() {
            cout << "\nDirection of Ship Number " << shipnumber << " is: \n";
            latitude.display1();
            longitude.display();
        }
};

We\’ve completely made the class called ship. “Don’t worry i will explain each and every line in this code after completing this program”.

int ship::count = 0;
int main() {
    ship shipobj1, shipobj2;
    shipobj1.getposition();
    shipobj2.getposition();
    shipobj1.display();
    shipobj2.display();
    system("Pause");
    return 0;
}

Hurrah! We’ve successfully written our program in C++.
Now it is time for an explanation “boys & girls :D”

Explaining header files:

#include<iostream>

This line represent a header file that contain all the pre-built functions that are used in the program.
for example:
The keyword “Class” is defined in this header file so whenever we write the word “Class“, the compiler goes to that header file search for the definition of that word and come back to our \”Source Code\”. If it finds the definition it performs the task specifically other wise gives us an error.

using namespace std;

This line is written to avoid its repetition. If we don’t write this line along with the header file, we have to write “std” before “cin & cout” functions like.

std cout << "Something";

So to avoid this std repetition we write this line on the top of our source code.

now you must be wondering what is “Class“,
in simple words “Class” is a user defined data-type.
like when we want to Initialize an integer we write ” int variable_name ” where ” int ” is a data-type in the same manner class is also a data-type.

Explaining Classes:

Private:
    int degrees;
    float minutes;
    char direction;
Public:
        void getdata() {
            cout << "\nEnter Degrees: ";
            cin >> degrees: \";
            cout << "\nEnter Minutes: ";
            cin >> minutes;
            cout << "\nEnter Direction (E, W, N, S): ";
            cin >> direction;

Private:
is a section of class where all the variables and functions are defined to use it in ” Public: \” section.
int, float, char ” those are all the data-type along with their names.

Public:
Is a section where all the functions are defined which will be used in the ” main()” function.

Class Ship {
    Private:
        angle latitude;
        angle longitude;
        int shipnumber;
        static int count;

    Public:
        void getposition() {
            count++;
            shipnumber = count;
            cout << "\nEnter direction on Latitude: ";
            latitude.getdata();
            cout << "\nEnter Direction on Longitude: ";
            longitude.getdata();
        }
        void display() {
            cout << "\nDirection of Ship Number " << shipnumber << " is: \n";
            latitude.display1();
            longitude.display();
        }
};

In the \” Private: \” section of this class we\’ve used the previous \” class \” as the data-type of \” latitude & longitude \”. We\’ve also created some other variables to use in public section of this class.
The public section contain a few functions using the variable in Private section.

Explaining Main Function:

int ship::count = 0

since we cannot use the variable which are defined inside the \” Private \” section of the class. That is why we\’ve created a new variable called \” ship \” and connected it with the variable inside the class to use in main() function.
note: \” :: \” these operators are \” scope resolution \” operators.

int main() {
    ship shipobj1, shipobj2;
    shipobj1.getposition();
    shipobj2.getposition();
    shipobj1.display();
    shipobj2.display();
    system("Pause");
    return 0;
}

“int main()” Is the main function that runs and show us the output it we don’t write anything in this section we will not get any result.

We access the functions which are inside the class through the object of that class. This is how we make objects of a particular class, ” ship shipobj1, shipobj2; “, in this line ” ship ” is the class we are using in main program or we can say that ” ship ” is the data-type of ” shipobj1 & shipobj2 “.

system("Pause");

This line of code holds the program until a key from keyboard is press. As soon as you press any key the program will end and the console will be closed.

We use this line of code to hold the screen and to see how our program worked. If we don’t write this line of code our program will run and come back to our source code window. We won’t be able to see how our program worked that is why this line of code is used.

return 0;

since we’ve used “int main()” that means we said our compiler that it will return a number. At the end of the program and if we don’t return \” 0 \” our program will give us an error. It will say that \” you said it will return a number but it didn’t why?” that is why we have to return a number to finish the program without any error.

Thank you for visiting our website! Make sure to leave a comment if it is helpful.

ads