//Lynette Wilkins
//Week 5
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class HotelRoom
{
private:
char room_num[3]; //Character array that stores a 3-character room number
char* guest; //Character pointer,which stores the name of the guest occupying the room
int room_cap;
int occup_stat;
double daily_rt;
public:
HotelRoom(char room[], char* g_p, int roomcap, int occup, double rate = 89.00);
HotelRoom(const HotelRoom&); //copy constructor
~HotelRoom();
void* Get_Number(); //Displays room number and add the method Display_Guest()
int Get_Capacity();
int Get_Status();
double Get_Rate();
int Change_Status(int);
double Change_Rate(double);
void* Get_Guest();
};
HotelRoom::HotelRoom(char room[], char* g_p, int roomcap,int occup, double rate )
{
strcpy(room_num, room); //copy first argument into room_num[]
guest = new char[strlen(g_p) + 1]; //reserve space for the guest name
strcpy(guest, g_p); //copy fourth argument into new space
room_cap = roomcap;
daily_rt = rate;
occup_stat = occup;
}
HotelRoom::HotelRoom(const HotelRoom& room_r)
{
strcpy(room_num, room_r.room_num); //Copy first argument into room_num[]
guest = new char[strlen(room_r.guest) + 1]; //create space for the guest
strcpy(guest, room_r.guest); //Copy second argument into new space
room_cap= room_r.room_cap;
daily_rt = room_r.daily_rt;
occup_stat = room_r.occup_stat;
cout <<endl<<endl;
cout << "Copy constructor executed. " <<endl;
}
HotelRoom::~HotelRoom()
{
cout << endl<<endl;
cout << "Guest in room "<<room_num << " has checked out." <<endl;
delete [] guest;
}
void* HotelRoom::Get_Number()
{
cout << room_num;
}
int HotelRoom::Get_Capacity()
{
return room_cap;
}
int HotelRoom::Get_Status()
{
return occup_stat;
}
int HotelRoom::Change_Status(int occup)
{
occup_stat = occup;
if (occup > room_cap)
{
return -1;
}
else
return occup_stat;
}
double HotelRoom::Get_Rate()
{
return daily_rt;
}
double HotelRoom::Change_Rate(double rate)
{
daily_rt = rate;
return daily_rt;
}
void* HotelRoom::Get_Guest()
{
cout<< guest;
}
int main()
{
cout<< setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
char room[4];
char buffer[100]; //temporarily stores guest name
int roomcap = 4;
int occup;
double rate = 89.00;
cout<<"\nEnter the room number: "<<endl;
cin.getline(room, 5);
cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;
cout<<"\nEnter the primary guest name: "<<endl;
cin.ignore();
cin.getline(buffer, 100);
HotelRoom room1(room, buffer, roomcap, occup, rate); //initialize the object
if (room1.Change_Status(occup) == -1)
{
cout<<"You have exceeded the room capacity"<<endl;
}
else
{
cout <<"\nThe room number is ";
room1.Get_Number();
cout<<"."<<endl;
cout<<"\nThe name of the primary guest is ";
room1.Get_Guest();
cout <<"."<<endl;
cout<<"\nThe number of guest in the room is "<<room1.Change_Status(occup)<<"."<<endl;
cout<<"\nThe daily rate for room "<<room<< " is "<<room1.Get_Rate()<<"."<<endl<<endl;
}
cout<<"\nRoom ";
room1.Get_Number();
cout<<" is vacant."<<endl;
HotelRoom room2 = room1;
return 0;
}
Using void with pointer?
Page 1 of 16 Replies - 167 Views - Last Post: 20 November 2012 - 07:39 AM
#1
Using void with pointer?
Posted 19 November 2012 - 09:22 AM
Hello guys!! I am a little confused about one thing with my program. I was asked to recode my original program and to return a pointer for two of my functions. Well the thing with this is both functions are "void" functions. I have not recalled anything we have went over in class talking about void and using functions. I am getting an error saying that both functions must return a value. That is really confusing me because I thought that was what they were doing. Any help would be greatly appreciated. Thanks in advance!!!
Replies To: Using void with pointer?
#2
Re: Using void with pointer?
Posted 19 November 2012 - 09:43 AM
Are you sure they should be returning void pointers? It looks to me that Get_Number should return 'room_num' and Get_Guest should return 'guest', which are both char pointers.
This post has been edited by Aphex19: 19 November 2012 - 09:46 AM
#3
Re: Using void with pointer?
Posted 19 November 2012 - 10:05 AM
Aphex19, on 19 November 2012 - 09:43 AM, said:
Are you sure they should be returning void pointers? It looks to me that Get_Number should return 'room_num' and Get_Guest should return 'guest', which are both char pointers.
Yes the assignment states that each returns a pointer to the corresponding instance variable.
#4
Re: Using void with pointer?
Posted 19 November 2012 - 10:12 AM
Aphex19 is of course correct,
lets take a look at one of your problem functions
it should look something like this and implemented something like
Now look at what you have done and have a think about your implementation.
Snoopy.
lets take a look at one of your problem functions
void HotelRoom::Get_Guest(char* Client)
{
strcpy(Client, guest);
}
it should look something like this and implemented something like
cout<<"\nThe name of the primary guest is "; char Client[20]; room1.Get_Guest(Client); cout <<Client<<"."<<endl;
Now look at what you have done and have a think about your implementation.
Snoopy.
#5
Re: Using void with pointer?
Posted 19 November 2012 - 10:15 AM
llwilkins, on 19 November 2012 - 10:05 AM, said:
Aphex19, on 19 November 2012 - 09:43 AM, said:
Are you sure they should be returning void pointers? It looks to me that Get_Number should return 'room_num' and Get_Guest should return 'guest', which are both char pointers.
Yes the assignment states that each returns a pointer to the corresponding instance variable.
Let me be a little more precise. Get_Number() and Get_Guest() each returns a pointer to the corresponding instance variable.
#6
Re: Using void with pointer?
Posted 19 November 2012 - 03:11 PM
Usually use only use void pointers to create an abstraction between your client data and the data that you are processing. They are also used for passing private information that a class or function as no business modifying or using. In your case Get_Number should be returning a number, their is no need to abstract the type of data that your are returning, it is an integer.
#7
Re: Using void with pointer?
Posted 20 November 2012 - 07:39 AM
To be clear, there is no reason at all to do this:
It should be:
If you wanted to change the returned value and have it reflect in the instance, you'd still use a reference:
However, it would be far better to be clear about these things:
There is also no good reason to use c-strings in C++... Unless explicitly required, use std::string for guest and make your life easier.
Hope this helps.
class HotelRoom {
void *Get_Number();
void *Get_Guest();
It should be:
class HotelRoom {
// you are returning an int
// this doesn't change the instance
int getNumber() const;
const char *getGuest() const;
If you wanted to change the returned value and have it reflect in the instance, you'd still use a reference:
class HotelRoom {
int &getNumber();
However, it would be far better to be clear about these things:
class HotelRoom {
int getNumber() const;
const char *getGuest() const;
void setNumber(int);
void setGuest(char *);
There is also no good reason to use c-strings in C++... Unless explicitly required, use std::string for guest and make your life easier.
Hope this helps.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|