Answer to Question #50651 in C++ for zexy
2015-02-12T09:05:01-05:00
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.
1
2015-02-16T09:40:36-0500
#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("cls"); 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("pause"); } if(cin.fail()) {cout<<"Error!!!Non-numeric values are entered"<<endl; system("pause");} } while (obj.price < 0 || cin.fail()); do{ system("cls"); 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("pause");} if(cin.fail()) {cout<<"Error!!!Non-numeric values are entered"<<endl; system("pause");} } while (obj.numberbedrooms < 0 || cin.fail()); do{ system("cls"); 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("pause");} else if (obj.numberBath < 0) { cout<<"Error!!!Negative values are entered"<<endl; system("pause");} } while (obj.numberBath < 0 || cin.fail()); return stream; } }; int main(){ RealEstate real; cin>>real; cout<<real; system("pause"); 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 !
Learn more about our help with Assignments:
C++
Comments
Leave a comment