63 103
Assignments Done
99%
Successfully Done
In July 2018

Answer to Question #50651 in C++ for zexy

Question #50651
Create a class named RealEstate that has data members to hold the price of a house, the
number of bedrooms, and the number of baths. Member functions include overloaded
insertion and extraction operators. Write a main()function that instantiates a RealEstate
object, allows the user to enter data, and displays the data members entered. The main()
function should display an appropriate thrown error message if non-numeric or negative
values are entered for any of the data members. Save the file as RealEstate.cpp.
Expert's answer
#include <stdlib.h>
#include <iostream>  
#include <stdio.h>
#include <cstdlib> 
using namespace std;
class RealEstate{
private:
int numberbedrooms;
int numberBath;
double price;
public:
RealEstate() {numberbedrooms = 0; numberBath = 0; price = 0;};
RealEstate(int _numberbedrooms, int _numberBath, double _price){
numberbedrooms = _numberbedrooms; 
numberBath = _numberBath; 
price = _price;
}
friend ostream& operator<<(ostream& stream, const RealEstate& obj){

stream <<"----------------------------------\n"<<endl;
   stream <<" number of bedrooms  = "<< obj.numberbedrooms << "\n ";
   stream <<"number of bath      = "<<obj.numberBath << "\n";
stream <<" price               = "<<obj.price << "\n";
   stream <<"----------------------------------\n"<<endl;


return stream;
}
friend istream& operator>>(istream& stream,  RealEstate &obj;){

do{
system&#40;"cls"&#41;;
cout << "Enter a price of the house: ";
stream.clear();
stream.sync();
stream>>obj.price;
if (obj.price < 0) { cout<<"Error!!!Negative values are entered"<<endl; system&#40;"pause"&#41;; }
if(cin.fail()) {cout<<"Error!!!Non-numeric values are entered"<<endl; system&#40;"pause"&#41;;}

} while (obj.price < 0 || cin.fail());
do{
system&#40;"cls"&#41;;
cout << "Enter a number of bedrooms: ";
stream.clear();
stream.sync();
stream>>obj.numberbedrooms;
if (obj.numberbedrooms < 0) { cout<<"Error!!!Negative values are entered"<<endl; system&#40;"pause"&#41;;}
if(cin.fail()) {cout<<"Error!!!Non-numeric values are entered"<<endl; system&#40;"pause"&#41;;}

} while (obj.numberbedrooms < 0 || cin.fail());
do{
system&#40;"cls"&#41;;
cout << "Enter a number of bath: ";
stream.clear();
stream.sync();
stream>>obj.numberBath;
if(cin.fail()) {cout<<"Error!!!Non-numeric values are entered"<<endl; system&#40;"pause"&#41;;}
else
if (obj.numberBath < 0) { cout<<"Error!!!Negative values are entered"<<endl; system&#40;"pause"&#41;;} 

} while (obj.numberBath < 0 || cin.fail());
            return stream;
}
};
int main(){
RealEstate real;
cin>>real;
cout<<real;
system&#40;"pause"&#41;;
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions