Need to add a class

D

Daz01

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;
}
}
 
M

mlimber

Daz01 said:
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
 
D

Daz01

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?
Daz01 said:
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
 
O

osmium

Daz01 said:
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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Daz01 said:
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.
 
B

BobR

Daz01 wrote in message ...

Do not Top-Post, Re-ordered.
mlimber said:
Daz01 said:
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;

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top