473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need to add a class

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

Dec 4 '06 #1
5 1312
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

Dec 4 '06 #2
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
Dec 5 '06 #3
"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.
Dec 5 '06 #4
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
Dec 5 '06 #5

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
Dec 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Benne Smith | last post by:
Hi, I have three enviroments; a development, a testing and a production enviroment. I'm making a big application (.exe), which uses alot of different webservices. I don't use the webservices...
1
by: Juan Dent | last post by:
Hi, The Request.SaveAs method stores in a file the complete HTTP request including the HttpVersion. However when I try to reproduce in memory the same thing, I cannot find the "HTTP/1.1"...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
10
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes ...
1
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
20
by: d.s. | last post by:
I've got an app with two classes, and one class (InventoryInfoClass) is an object within the other class (InventoryItem). I'm running into problems with trying to access (get/set) a private...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.