5 Replies - 470 Views - Last Post: 11 January 2013 - 09:44 AM Rate Topic: -----

#1 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Passing instance variables to derived class

Posted 03 December 2012 - 05:32 PM

Hello everyone I have did good so far with this program but the only thing that is confusing me is passing the instance variables from the base class to the derived class. I need to get the instance variable from the HotelRoom to the GuestRoom. I am trying to get these instance variables sent to the Calculate_Bill method and its not working because I am getting garbage back in return.


//Lynette Wilkins
//Week 8
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

class HotelRoom
{
protected:
	int k;  // room num
	int occup_stat;
	double daily_rt;

	// guest and daily rate will be added here in weeks ahead 
public:
	HotelRoom(int i = 0, int occup = 0, double rate = 89.00); // one argument constructor for now
	HotelRoom(const HotelRoom&); //copy constructor
	~HotelRoom(); //deconstructor

};

HotelRoom::HotelRoom(int i, int occup, double rate) : k(i)
{
	cout <<endl;
	cout<< "HotelRoom room number: "
		<<k<< endl;
}

HotelRoom::HotelRoom(const HotelRoom& room_r) :k(room_r.k)
{
	cout<<endl;
	cout<<"(HotelRoom copy constructor executed)"<<endl;
}
HotelRoom::~HotelRoom()
{
	cout <<endl;
cout<<"(HotelRoom destructor executed)"<<endl;

}

class GuestRoom : public HotelRoom
{
protected:
	int capacity;
	int status;
	int days;
	double gbill;

public:
	GuestRoom(int c = 0, int s = 0, int d = 0);
	GuestRoom(const GuestRoom &);
	~GuestRoom(); 
	double Calculate_Bill();
	void Display();



};

GuestRoom::GuestRoom(int c, int s, int d) : HotelRoom(k = 123), capacity(c), status(s), days(d)
{
	cout<<endl;
	cout<<"**GuestRoom constructor executed**"<<endl<<endl;		
	    cout <<"Maximum number of guest per room: "<<capacity<<endl;
		cout <<"Number of guest occupying room: "<<status<<endl;
		cout <<"Days that room is going to be occupied: "<<days<<endl;
}

GuestRoom::GuestRoom(const GuestRoom & hroom_r)
	:HotelRoom(hroom_r), capacity(hroom_r.capacity)
{
	cout<<endl;
	cout<<"**GuestRoom copy constructor executed**"
		<<endl;
}
GuestRoom::~GuestRoom()
{
	cout<<endl;
	cout<< "**GuestRoom destructor executed**"<<endl;
}

double GuestRoom::Calculate_Bill() 
{
	
	gbill = occup_stat*days*daily_rt;

	return gbill;
}


void GuestRoom::Display()
{
	
	cout<< setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);
	cout<<endl<<endl<<endl;
	cout<<"Daily Rate: "<<daily_rt<<endl;
	cout<<"Days Occupying: "<<days<<endl;
	cout<<"Occupancy Status: "<<occup_stat<<endl;
	cout<<"Guest bill: "<<gbill<<endl;
}



class MeetingRoom : public HotelRoom
{
protected:
	int seats;
	int status;
	double mbill; 
	

public:
	MeetingRoom(int s = 0, int st = 0);
	MeetingRoom(const MeetingRoom &);
	~MeetingRoom();
	double Calculate_Bill();
	void Display();

};

MeetingRoom::MeetingRoom(int s, int st) : HotelRoom(k = 123), seats(s), status(st)
{
	cout<<endl;
	cout<<"Constructor Executed "<<seats<<endl;
	
		
	    
}

MeetingRoom::MeetingRoom(const MeetingRoom & mroom_r)
	:HotelRoom(mroom_r), seats(mroom_r.seats)
{
	cout<<endl;
	cout<<"~~MeetingRoom copy constructor executed~~"
		<<endl;
}
MeetingRoom::~MeetingRoom()
{
	cout<<endl;
	cout<< "~~MeetingRoom destructor executed~~"
		<<endl;
}
	
		
double MeetingRoom::Calculate_Bill()
{
		
mbill = seats * 10.00 + 500.00;
	return mbill;
}

void MeetingRoom::Display()
{
	
	cout<< setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);

	
	cout<<endl<<endl<<"Meeting Room Information"<<endl<<endl;

	cout<<"Seats in the Meeting Room: "<<seats<<endl;
	cout<<"Status of the Meeting Room:"<<status<<endl;
	cout<<"Bill for renting the room: "<<mbill<<endl<<endl<<endl;
}

	
int main()
{
	

	HotelRoom room_object1(123, 4, 89.00);
	GuestRoom groom_object1(5,3,4);
	GuestRoom groom_object2 = groom_object1;
	MeetingRoom mroom_object1(100,1);
	MeetingRoom mroom_object2 = mroom_object1;
	cout <<endl;
        mroom_object1.Calculate_Bill();
	mroom_object1.MeetingRoom::Display();
	groom_object1.Calculate_Bill();
	groom_object1.GuestRoom::Display();
	system("PAUSE");
	return 0;
}

		




When the "groom_object1.GuestRoom::Display();" everything works except the calculation_bill and that is because the instance variables are not passing like I thought they should from the base class. Please help!!

Is This A Good Question/Topic? 0
  • +

Replies To: Passing instance variables to derived class

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3422
  • View blog
  • Posts: 10,538
  • Joined: 05-May 12

Re: Passing instance variables to derived class

Posted 03 December 2012 - 05:47 PM

It's not a matter of passing the variables between base and derived class. The issue is that you aren't even initializing all the variables in the base class.
Was This Post Helpful? 0
  • +
  • -

#3 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Passing instance variables to derived class

Posted 04 December 2012 - 06:53 AM

Is there a way you can show me an example b/c I have asked and asked my instructor and he still has not given me an example with multiple variables. He has only shown an example with a single variable.
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Passing instance variables to derived class

Posted 04 December 2012 - 07:34 AM

Well! You have messed the code so much, its confusing.. Most of the data members are even left uninitialized... You need to start with creating a simple class, when it works fine, you can then inherit from it...

Here is an example, just for learning purpose:
#include <iostream>

class A {
    private:
        int a;
    public:
        A(int);
		
		int getA() { return a; }
};

A::A(int x) : a(x) {
}

class B : public A{
    private:
        int b;
    public:
        B(int, int);
		
		int getB() { return b; }
};

B::B(int x, int y) : A(x), b(y) {
}

class C : public B{
    private:
        int c;
    public:
        C(int, int, int);
		
		int getC() { return c; }
};

C::C(int x, int y, int z) : B(x, y), c(z) {
}

int main()
{
	C c(1,2,3);
	
	std::cout << c.getA() << ' '  << c.getB() << ' ' << c.getC();
	
	return 0;
}



Remember, when c constructor is called it in turn calls its parent before constructing... Mean that, when a derived class construct is called, before it is constructed, its parent construct is called, and if it has another parent it will be called until the most base class... The most base class is constructed first, then its child, till the most derived class...

Hope this Helps!
Was This Post Helpful? 0
  • +
  • -

#5 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Passing instance variables to derived class

Posted 11 January 2013 - 09:22 AM

OK, so I need to fix the SalariedEmployee and it should work ok?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,255
  • Joined: 25-December 09

Re: Passing instance variables to derived class

Posted 11 January 2013 - 09:44 AM

What? Where do you even have a salaried employee in this program? Maybe you meant to post this question in one of your other topics, maybe this one?

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1