473,794 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\nPlea se 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!=currentT emp)
{
spaceUsed=space Used+currentTem p->LengthBoat;
currentTemp=cur rentTemp->next;
}
if (150<(LengthBoa tTemp+spaceUsed ))
{
cout << "\n\nThe marina is full, sorry:" <<endl;
}
else
{
cout << "\n\nDepth of Boat: ";
cin >DepthBoatTem p;

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*LengthBoatT emp)*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=Len gthBoatTemp;
current->DepthBoat=Dept hBoatTemp;

current->next=NULL;

if (NULL==head)
{
head=current;
}
if (NULL!=last)
{
last->next = current;
}
cout << "\n";
cout <<"\n\nBookin g 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=hea d;
spaceUsed=0;

while (NULL!=currentT emp)
{
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=space Used+currentTem p->LengthBoat;
currentTemp=cur rentTemp->next;
}

cout<<"Availabl e 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 1332
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_owners hip(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_owners hip(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
1945
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 objects of my classes Person(superclass), Student(inherit Person), Teacher(inherit Person) in that array. The name will be the unique key. These classes are all working ok. I want to be able to add, remove, find etc. objects. To all of this, I...
8
2848
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 list class must Ioverride in order for this to work?
5
3487
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 by adding a WebReference, since it does not allow me to keep state (cookiecontainer) or to specify functions on the classes (like if i want to override the ToString() function on a class from my webservice). So the only way i can see how i can get...
1
2492
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" anywhere. I looked around and found a class called HttpWorkerRequest which has a method called GetHttpVersion() which returns the exact thing I am missing. However, I can't see how to get at the HttpWorkerRequest from a Page or from the HttpApplication.
12
5342
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 the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and the windows application. I face the following problem when I send this class to the webservice.
18
2344
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. Here's the scenario: We have a base class with all virtual functions. We'll call this the Animal class. We then make two classes Fish and Bird that both inherit from Animal. In the program, we have a single array of Animal pointers that will...
15
2176
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 (a_obj) of a class type (A), then we need to define A as well, but if B has a pointer to A, then we only need to forward declare A. I was told this is because the compiler needs to see the implemenation of A when allocating memory for a_obj. ...
10
2118
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 #these are the operations + = , - = , * = , 1/ = only if 0 not in .
1
2106
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 the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life points, a name, and a bonus factor that is used in special occasions. In this initial phase of the...
20
1484
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 variable within the included class (InventoryInfo) from the "including" class (InventoryItem). Here's the code, trimmed down. I've included ********* at the start of the first line that's blowing up on me. I'm sure others that try to access the...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.