Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. Write a program that sets up a base class Sailor containing protected data fo

ID: 3694137 • Letter: 4

Question

4. Write a program that sets up a base class Sailor containing protected data for name, rank, and serial number. There should be two constructor functions: one is empty and the other has the three data values as inputs. The other member functions should include a GetInfo function (asks the user for Sailor information) as well as a WriteInfo (writes all the Sailor data to the screen). Next derive a new class from Sailor. The Sailor_At_Sea class has the name of the ship to which the Sailor has been assigned, as well as the name of the ship's home port. The Sailor_At_Sea class also has two constructor functions: one that does nothing and the other that receives all the possible Sailor data. This derived class constructor passes the sailor data to the base class constructor. There are also WriteInfo and GetInfo functions that call the associated Sailor functions before handling the derived class specific data. The main function has several steps. 1. Initially it should declare one Sailor and one Sailor_At_Sea objects. Simply make up all the data and pass it into the objects when they are created. 2. Call the WriteInfo functions and verify that the objects were created with the data you passed into them. 3. Next, change the information in both objects by calling the GetInfo functions, and then call

Explanation / Answer

#include<iostream>
using namespace std;

class Sailor{
  
   protected:
       string name;
       int rank;
       int serial;
      
   public:
  
       Sailor(){}
       Sailor(string n, int r, int s){
           name = n;
           rank = r;
           serial = s;
       }
      
       void GetInfo(){
           cout<<"Enter name: ";
           cin>>name;
           cout<<"Enter rank: ";
           cin>>rank;
           cout<<"Enter serial number: ";
           cin>>serial;
       }
      
       void WriteInfo(){
           cout<<"Name: "<<name<<endl;
           cout<<"Rank: "<<rank<<endl;
           cout<<"Serial: "<<serial<<endl;
       }
  
};


class Sailor_At_Sea: public Sailor{
  
   private:
       string ship_name;
       string port_name;
  
   public:
  
       Sailor_At_Sea(){};
       Sailor_At_Sea(string n, int r, int s,string ship, string port): Sailor(n,r,s){
           ship_name = ship;
           port_name = port;
       }
   void GetInfo(){
           Sailor::GetInfo();
           cout<<"Enter ship name: ";
           cin>>ship_name;
           cout<<"Enter port name: ";
           cin>>ship_name;
       }
      
   void WriteInfo(){
       Sailor::WriteInfo();
       cout<<"Ship Name: "<<ship_name<<endl;
       cout<<"Port Name: "<<port_name<<endl;
   }
      
};


int main(){
  
   Sailor sailor("Alex", 2, 12345);
  
   Sailor_At_Sea sailor_at_sea("Bob", 3, 234567, "WathsUp","Landon");
  
   sailor.WriteInfo();
   cout<<endl;
   sailor_at_sea.WriteInfo();
  
   cout<<endl;
   sailor.GetInfo();
   sailor.WriteInfo();
   cout<<endl;
  
   sailor_at_sea.GetInfo();
   sailor_at_sea.WriteInfo();
  
   return 0;
}

/*

Sample run:

Name: Alex
Rank: 2
Serial: 12345

Name: Bob
Rank: 3
Serial: 234567
Ship Name: WathsUp
Port Name: Landon

Enter name: Pravesh
Enter rank: 5
Enter serial number: 0987
Name: Pravesh
Rank: 5
Serial: 987

Enter name: Mukesh
Enter rank: 1
Enter serial number: 123
Enter ship name: dwd3
Enter port name: 3e3r
Name: Mukesh
Rank: 1
Serial: 123
Ship Name: 3e3r
Port Name: Landon

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote