• September 25, 2021

PHSARIT

Bring Technology Ideas with you

 Simple house booking in C++ using Class

Class is one of the most favorite for developer to use. This tutorial will bring you with simple code using class in C++ to server as booking requested in the guest houses. First you need to includes some STD library above the int main( ) function.

The Above int main () function

This section while you have created the .ccp file and name it, then please copy as the code below:

#include<iostream>
#include<string>
using namespace std;
//create customers/guest class to store guest information
class guests 
{
private: 
	int id;
	string first_name;
	string last_name;
	double day;
	double total;
	//string roomid;
	//string room_type;

public:
	void getCustomer() //getcustomer info
	{
		cout << "Give us your information detial:" << endl;
		cout << "Your National Identification: ";
		cin >> id;
		cout << "Your First Name Please: ";
		cin >> first_name;
		cout << "Your Last Name Please: ";
		cin >> last_name;
		cout << "Number of staying days: ";
		cin >> day;
	}
	void Choice() // Roomtype selection
	{
		char choice;
		cout << "Which type of room do you want to book?" << endl;
		cout << "Please type: " << endl;
		cout << "1. press 1" << endl;
		cout << "2. press 2" << endl;
		cin >> choice;
		switch (choice)
		{
		case '1':
			cout << "You have selected first choice" << endl;
			firstchoice();
			getCustomer();
			total=day*50;
			print();
			break;
		case '2':
			cout << "You have selected second choice" << endl;
			secondchoice();
			getCustomer();
			total = day * 30;
			print();
			break;
		default:
			cout << "Invalid selection" << endl;
			break;
		}

	}
	void firstchoice()
	{
		cout << "1. Cost/night: 50.00 dollars" << endl;
		cout << "2. Room type is VIP" << endl;
	}
	void secondchoice()
	{
		cout << "Cost/night: 30.00 dollars" << endl;
		cout << "Room type is normal" << endl;
	}
	void print()
	{
		cout << "----Payment Detial----" << endl;
		cout << "- Book ID: " << id << endl;
		cout << "- Full Name: " << first_name <<" "<< last_name << endl;
		cout << "- Total Cost: " << total <<" dollars"<< endl;
		cout << "- Your room is Tramkok and floor number 2 is assigned";
	}
};

The above codes were include the class for operating the concepts of book.

Main Function

To call your class to work in the main program, you need to call in by inheritance using dot (.) operators as the below code:

int main()
{
	guests myCustomers;
	myCustomers.Choice();

	cout << endl;
	system("pause==0");
}

We have the class which is called “guests”; therefore in order to allow call each of the class containers includes variables, functions, etc. to work in the main, you need to create a new name which is pointing to the class name that have already created in the head. See the full code below:

//www.pharit.org
// Simple Guest House Reservation Project
#include<iostream>
#include<string>
using namespace std;
//create customers/guest class to store guest information
class guests 
{
private: 
	int id;
	string first_name;
	string last_name;
	double day;
	double total;


public:
	void getCustomer() //getcustomer info
	{
		cout << "Give us your information detial:" << endl;
		cout << "Your National Identification: ";
		cin >> id;
		cout << "Your First Name Please: ";
		cin >> first_name;
		cout << "Your Last Name Please: ";
		cin >> last_name;
		cout << "Number of staying days: ";
		cin >> day;
	}
	void Choice() // Roomtype selection
	{
		char choice;
		cout << "Which type of room do you want to book?" << endl;
		cout << "Please type: " << endl;
		cout << "1. press 1" << endl;
		cout << "2. press 2" << endl;
		cin >> choice;
		switch (choice)
		{
		case '1':
			cout << "You have selected first choice" << endl;
			firstchoice();
			getCustomer();
			total=day*50;
			print();
			break;
		case '2':
			cout << "You have selected second choice" << endl;
			secondchoice();
			getCustomer();
			total = day * 30;
			print();
			break;
		default:
			cout << "Invalid selection" << endl;
			break;
		}

	}
	void firstchoice()
	{
		cout << "1. Cost/night: 50.00 dollars" << endl;
		cout << "2. Room type is VIP" << endl;
	}
	void secondchoice()
	{
		cout << "Cost/night: 30.00 dollars" << endl;
		cout << "Room type is normal" << endl;
	}
	void print()
	{
		cout << "----Payment Detial----" << endl;
		cout << "- Book ID: " << id << endl;
		cout << "- Full Name: " << first_name <<" "<< last_name << endl;
		cout << "- Total Cost: " << total <<" dollars"<< endl;
		cout << "- Your room is Tramkok and floor number 2 is assigned";
	}
};

int main()
{
	guests myCustomers;
	myCustomers.Choice();

	cout << endl;
	system("pause==0");
}

See the result below:

    Result of the class booking/reservation using C++

    Related post