Make a Program in C++ to Show the Direction of a Ship OOP Classes.

0
21
How to Write a program in c++

In this article I am going to show you how you can make a program in C++ programming language by making 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…

It is a very Basic C++program. That might help you if you are studying in a University/College. they’ve asked you to write a program that shows the location of the ships by entering the “Latitude, Longitude & Minutes”.

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:

If you are using “Microsoft Visual Studio” to code this program make sure to enter the following line before starting with the program:

#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 result 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 made our program in C++.
Now it is time for explanation “boys & grils :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 mean 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.

If you didn’t understand anything feel free to ask below or send us an Email here. We’ll get back to you as soon as possible. Thanks!

LEAVE A REPLY

Please enter your comment!
Please enter your name here