Need to add a class

Discussion in 'C++' started by Daz01, Dec 4, 2006.

  1. Daz01

    Daz01 Guest

    Hi, Ive written a program. But I want to add a class in it somehow? Is
    there any easy way to do it.

    Below is the code for the program:

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    struct Boat
    {
    char BoatOwn[50];
    char BoatNme[50];
    int TypeBoat;
    int LengthBoat;
    int DepthBoat;
    Boat* next;
    };



    int main()
    {
    int menuOption;
    int LengthBoatTemp;
    int DepthBoatTemp;
    int duration;
    int rate;
    char confirm;
    int spaceUsed=0;

    //Pointers don't point to any destination at the moment
    Boat* head = NULL;
    Boat* current =NULL;
    Boat* last = NULL;
    Boat* currentTemp= NULL;

    while (menuOption!=4)

    {
    //User options

    cout << "\n\n1: New Booking" <<endl;
    cout << "\n\n2: Delete Record" <<endl;
    cout << "\n\n3: Display all Records" <<endl;
    cout << "\n\n4: Exit" <<endl;

    cout << "\n\n\nPlease select an option: ";
    cin >> menuOption;

    if (menuOption ==1)
    {
    system ("CLS");
    cout << "\t\t\t" <<endl;
    cout << "Please enter boat details:" <<endl;
    cout << "Boat length:" <<endl;
    cin >> LengthBoatTemp;

    if (LengthBoatTemp >15)
    {
    cout << "\n\n Your boat is too big to fit into the marina,
    sorry:" <<endl;
    }
    else
    {
    //check if there is any space in the marina
    currentTemp= head;
    spaceUsed=0;

    while (NULL!=currentTemp)
    {
    spaceUsed=spaceUsed+currentTemp->LengthBoat;
    currentTemp=currentTemp->next;
    }
    if (150<(LengthBoatTemp+spaceUsed))
    {
    cout << "\n\nThe marina is full, sorry:" <<endl;
    }
    else
    {
    cout << "\n\nDepth of Boat: ";
    cin >> DepthBoatTemp;

    if (DepthBoatTemp > 5)
    {
    cout << "Your boat is too deep for the marina, it exceeds the
    max depth:" <<endl;
    }
    else
    {
    cout <<"\n\nPlease enter the length of your stay in months:"
    <<endl;
    cin >> duration;

    rate= (10*LengthBoatTemp)*duration;

    cout << "\n" <<endl;
    cout << "\n\nTotal cost:" << rate <<endl;
    cout << "\n" <<endl;

    cout<< "\n\nDo you want to proceed with the booking? (Y/N)"
    <<endl;
    cin >>confirm;

    if (confirm=='Y'||confirm=='Y')
    {
    last=current;
    current = new Boat;

    cout <<"\n\n\n:" <<endl;

    cout << "\n\nPlease enter Owner and Boat details into the
    system:" <<endl;

    cout << "Enter Owners Name:" <<endl;
    cin >> current->BoatOwn;

    cout << "Enter the Boats Name:" <<endl;
    cin >> current->BoatNme;

    cout << "Enter what type of boat it is:" <<endl;
    cout << " Motor, Sailing or Narrow:" <<endl;
    cout << "Choice";
    cin >> current->TypeBoat;
    current->LengthBoat=LengthBoatTemp;
    current->DepthBoat=DepthBoatTemp;

    current->next=NULL;

    if (NULL==head)
    {
    head=current;
    }
    if (NULL!=last)
    {
    last->next = current;
    }
    cout << "\n";
    cout <<"\n\nBooking completed successfully:" <<endl;
    }
    else
    {
    cout <<"Your booking couldn't be completed:";

    } //this is the end of Y/N
    } //this is the end of else Depth of Boat
    } //this is the end of else Marina Length
    } //this is the end of Length of Boat
    } // this is the end of Option1

    else if (menuOption==2)
    {
    }
    else if (menuOption==3)
    {
    cout << "Boats already in marina:"<<endl;

    currentTemp=head;
    spaceUsed=0;

    while (NULL!=currentTemp)
    {
    cout << "Owners Name:" <<
    currentTemp->BoatOwn <<endl;
    cout << "Boat Name:" <<
    currentTemp->BoatNme <<endl;
    cout << "Type of Boat:" <<
    currentTemp->TypeBoat <<endl;
    cout << "Length of Boat:" <<
    currentTemp->LengthBoat << "meteres" <<endl;
    cout << "Depth of Boat:" <<
    currentTemp->DepthBoat << "meteres" <<endl;

    spaceUsed=spaceUsed+currentTemp->LengthBoat;
    currentTemp=currentTemp->next;
    }

    cout<<"Available Space: " << (500-spaceUsed)<<"
    metres\n\n:" <<endl;
    system("PAUSE");
    system("CLS");

    //* char buffer[180];
    char lineName[20];
    long shotPoint, x, y;





    }


    system("PAUSE");
    return EXIT_SUCCESS;
    }
    }
     
    Daz01, Dec 4, 2006
    #1
    1. Advertising

  2. Daz01

    mlimber Guest

    Daz01 wrote:
    > Hi, Ive written a program. But I want to add a class in it somehow? Is
    > there any easy way to do it.


    Yes:

    class X {};

    Done. :)

    > Below is the code for the program:
    >
    > #include <cstdlib>
    > #include <iostream>
    >
    > using namespace std;
    >
    > struct Boat
    > {
    > char BoatOwn[50];
    > char BoatNme[50];
    > int TypeBoat;
    > int LengthBoat;
    > int DepthBoat;
    > Boat* next;
    > };

    [snip]

    Seriously, though... you should use std::strings instead of character
    arrays and std::list instead of your own linked list unless your
    instructor has told you otherwise. You don't really have a class
    (behavior + data) so much as an aggregation of data, so changing Boat
    to a class with setter/getter functions seems like over-kill.

    Cheers! --M
     
    mlimber, Dec 4, 2006
    #2
    1. Advertising

  3. Daz01

    Daz01 Guest

    Hi
    Thanks for your reply, i know what your saying, but weve been told we
    need to add classes in the program, i just can't see how I could do 1.
    Is there any examples you could show me?

    mlimber wrote:
    > Daz01 wrote:
    > > Hi, Ive written a program. But I want to add a class in it somehow? Is
    > > there any easy way to do it.

    >
    > Yes:
    >
    > class X {};
    >
    > Done. :)
    >
    > > Below is the code for the program:
    > >
    > > #include <cstdlib>
    > > #include <iostream>
    > >
    > > using namespace std;
    > >
    > > struct Boat
    > > {
    > > char BoatOwn[50];
    > > char BoatNme[50];
    > > int TypeBoat;
    > > int LengthBoat;
    > > int DepthBoat;
    > > Boat* next;
    > > };

    > [snip]
    >
    > Seriously, though... you should use std::strings instead of character
    > arrays and std::list instead of your own linked list unless your
    > instructor has told you otherwise. You don't really have a class
    > (behavior + data) so much as an aggregation of data, so changing Boat
    > to a class with setter/getter functions seems like over-kill.
    >
    > Cheers! --M
     
    Daz01, Dec 5, 2006
    #3
  4. Daz01

    osmium Guest

    "Daz01" writes:

    > Hi, Ive written a program. But I want to add a class in it somehow? Is
    > there any easy way to do it.
    >
    > Below is the code for the program:
    >
    > #include <cstdlib>
    > #include <iostream>
    >
    > using namespace std;
    >
    > struct Boat
    > {
    > char BoatOwn[50];
    > char BoatNme[50];
    > int TypeBoat;
    > int LengthBoat;
    > int DepthBoat;
    > Boat* next;
    > };


    <snip>

    We can't see the assignment you were given. Is the notion of a Boat yours
    or your instructors?

    The convention in C++ is for data only things to be struct and class is
    normally used for things that have member functions. Given this base, you
    could add a member function to transfer ownership of the boat to a new
    owner. Something like:

    void transfer_ownership(char* new_owner);

    If the notion of a boat is yours, devise a new problem where a class is not
    a force fit.
     
    osmium, Dec 5, 2006
    #4
  5. On 2006-12-05 15:23, osmium wrote:
    > "Daz01" writes:
    >
    >> Hi, Ive written a program. But I want to add a class in it somehow? Is
    >> there any easy way to do it.
    >>
    >> Below is the code for the program:
    >>
    >> #include <cstdlib>
    >> #include <iostream>
    >>
    >> using namespace std;
    >>
    >> struct Boat
    >> {
    >> char BoatOwn[50];
    >> char BoatNme[50];
    >> int TypeBoat;
    >> int LengthBoat;
    >> int DepthBoat;
    >> Boat* next;
    >> };

    >
    > <snip>
    >
    > We can't see the assignment you were given. Is the notion of a Boat yours
    > or your instructors?
    >
    > The convention in C++ is for data only things to be struct and class is
    > normally used for things that have member functions. Given this base, you
    > could add a member function to transfer ownership of the boat to a new
    > owner. Something like:
    >
    > void transfer_ownership(char* new_owner);
    >
    > If the notion of a boat is yours, devise a new problem where a class is not
    > a force fit.


    And, if possible, change any occurance of char* or char[] to std::string.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Dec 5, 2006
    #5
  6. Daz01

    BobR Guest

    Daz01 wrote in message ...

    Do not Top-Post, Re-ordered.

    >mlimber wrote:
    >> Daz01 wrote:
    >> > Hi, Ive written a program. But I want to add a class in it somehow? Is
    >> > there any easy way to do it.

    >>
    >> Yes:
    >> class X {};
    >> Done. :)
    >>
    >> > Below is the code for the program:
    >> >
    >> > #include <cstdlib>
    >> > #include <iostream>
    >> > using namespace std;
    >> >
    >> > struct Boat {
    >> > char BoatOwn[50];
    >> > char BoatNme[50];
    >> > int TypeBoat;
    >> > int LengthBoat;
    >> > int DepthBoat;
    >> > Boat* next;
    >> > };

    >> [snip]
    >>
    >> Seriously, though... you should use std::strings instead of character
    >> arrays and std::list instead of your own linked list unless your
    >> instructor has told you otherwise. You don't really have a class
    >> (behavior + data) so much as an aggregation of data, so changing Boat
    >> to a class with setter/getter functions seems like over-kill.



    >Hi
    >Thanks for your reply, i know what your saying, but weve been told we
    >need to add classes in the program, i just can't see how I could do 1.
    >Is there any examples you could show me?
    >


    class Boat{ public: // same as struct
    // >> > char BoatOwn[50];
    // >> > char BoatNme[50];
    std::string BoatOwn;
    std::string BoatNme;

    >> > int TypeBoat;
    >> > int LengthBoat;
    >> > int DepthBoat;
    >> > Boat* next;


    Boat( std::string Own, std::string Nme )
    : BoatOwn( Own ), BoatNme( Nme ),
    TypeBoat(0), LengthBoat(0),
    DepthBoat(0), next(0){
    std::cout << "Boat: " <<BoatNme<<" constructed\n";
    }
    >> > };


    { // main() or function
    Boat B1( "G. Bush", "Sunken Ofice" );
    }

    Your turn.
    --
    Bob R
    POVrookie
     
    BobR, Dec 5, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Teep
    Replies:
    2
    Views:
    787
    Larry Marburger
    Jun 21, 2004
  2. E11
    Replies:
    1
    Views:
    4,952
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    789
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    631
  5. Deep Mehta via .NET 247
    Replies:
    2
    Views:
    475
    Dave A
    May 31, 2005
Loading...

Share This Page