Solutions

Program For Ship Direction Using Class [Solved] | OOP | C++

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…

Coding

#include<iostream>
using namespace std;
class ship{
private:
int serial;
static int num;
int degrees;
float minutes;
char direction;
public:
ship()
{
num++;
serial = num;
}
void getLoc()
{
cout << “Enter Degrees: “; cin >> degrees;
cout << “Enter Minutes: “; cin >> minutes;
cout << “Enter Direction: “; cin >> direction;
}
void reportSerLoc()
{
cout << “Ship Number ” << serial << ” Is At ” << degrees << ‘\xF8’ << minutes << “\'” << direction << endl;
}
};
int ship::num=0;

int main()
{
ship s1,s2,s3;
cout << “Enter Location Of 1st Ship” << endl;
s1.getLoc();
cout << “Enter Location Of 2nd Ship” << endl;
s2.getLoc();
cout << “Enter Location Of 3rd Ship” << endl;
s3.getLoc();
s1.reportSerLoc();
s2.reportSerLoc();
s3.reportSerLoc();
}

Output

ship class

About the author

Syed Moin Ali

Hello My Name is Syed Moin Ali and I am the founder of PC Learnings Here on this blog I write about Blogging, SEO, Internet Tricks, Social Networking Site, and Make Money

Add Comment

Click here to post a comment