Question

Write a program in C++ with comments! Create an abstract class Property, containing the data items...

Write a program in C++ with comments!
Create an abstract class Property, containing the data items owner, address and price. Add an abstract method showOwner(). Also provide getter/setters for all the data items. Make validations if necessary in the setter so that no invalid values are entered.

Create a class House, to inherit the Property class. This class should contain additional data item – yardSize – an integer – the size of the yard of the house, getter and setter for the additional data item, constructors and implementation of showOwner() method to print “The owner of the house is xxx”, where xxx is the name of the owner.

Create a class Apartment to inherit Property class. This class should contain one additional data item – typeOfAparment that should be enum with values oneroom, onebedroom, twobedrooms, multibedrooms. Implement showOwner() to print “The xxx aparment’s owner is yyy”, where xxx is the type of the apartment (convert the enum value to proper string) and yyy is the name of the owner.

Create a main function that declares a vector of pointers to Property objects and initialize it with some instances of the House and Apartment classes. Write a for loop to iterate through all elements of that vector and show information about the owner of the properties on a separate line each.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
#include<vector>

using namespace std;
//Abstract class Proprty
class Property
{
public:
   Property(string,string,double);
   void setOwner(string);
   void setAddress(string);
   void setPrice(double);
   string getOwner();
   string getAddress();
   double getPrice();
   //abstract method
   virtual void showOwner() = 0;
private:
   //instance vatibals
   string owner, address;
   double price;
};
//constructore with perameter
Property::Property(string owner, string address, double price)
{
   setAddress(address);
   setOwner(owner);
   setPrice(price);
}
//setter method with validations
void Property::setOwner(string owner)
{
   if (owner != "")
   {
       this->owner = owner;
   }
   else
       this->owner = "Unknown";
}

void Property::setAddress(string adderss)
{
   if (address != "")
       this->address = address;
   else
       this->address = "Unknown";
}

void Property::setPrice(double price)
{
   if (price > 0)
       this->price = price;
   else
       this->price = 0;
}
//getter methods
string Property::getOwner()
{
   return owner;
}

string Property::getAddress()
{
   return address;
}

double Property::getPrice()
{
   return price;
}

//Class House that inherites the Property
class House:public Property
{
public:
   House(string,string,double,int);
   void setYardSize(int);
   int getYardSize(int);
   void showOwner();
private:
   //instance variable
   int yardSize;
};
//Constructor that called parent class constructor
House::House(string owner, string address, double price, int size):Property(owner,address,price)
{
   setYardSize(size);
}
void House::setYardSize(int size)
{
   if (size > 0)
       yardSize = size;
   else
       yardSize = 0;
}

int House::getYardSize(int)
{
   return yardSize;
}
//implements the showOwner abstracr method here
void House::showOwner()
{
   cout << "The owner of the house is " << this->getOwner() << endl;
}
//Apartment class that inheritce the Property class
class Apartment :public Property
{
public:
   //enum type varibale ApartmentType
   enum ApartmentType { oneroom , onebedroom,twobedrooms, multibedrooms};
   Apartment(string,string,double, ApartmentType);
   void showOwner();
private:
   ApartmentType typeOfAparment;
};
Apartment::Apartment(string owner, string address, double price, ApartmentType type) :Property(owner, address, price)
{
   typeOfAparment = type;
}
//implements the showOwner abstracr method here
void Apartment::showOwner()
{
   string type;
   if (typeOfAparment == oneroom)
       type = "one room";
   else if (typeOfAparment == onebedroom)
       type = "one bed room";
   else if (typeOfAparment == twobedrooms)
       type = "two bed room";
   else
       type = "multi bed room";

   cout << "The "<<type<<" aparment's owner is " << this->getOwner() << endl;
}


int main()
{
   vector<Property*>properties;
   properties.push_back(new House("Bob Smith", "America", 2000,25));
   properties.push_back(new Apartment("Alice smith", "London", 23500, Apartment::oneroom));
   properties.push_back(new House("Johan Cena", "Jamnagar", 23000, 200));
   properties.push_back(new House("Arth Vyas", "Bhuj", 20040, 100));
   properties.push_back(new Apartment("John smith", "Australia", 23500, Apartment::twobedrooms));
   properties.push_back(new Apartment("Will smith", "India", 23500, Apartment::multibedrooms));
   for (int i = 0; i < properties.size(); i++)
   {
       properties.at(i)->showOwner();
       cout << endl;
   }
   system("pause");
   return 0;
}
output

- x ID: Chegg\C++\Property Abstract class\Debug\Property Abstract class.exe The owner of the house is Bob Smith The one room

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ with comments! Create an abstract class Property, containing the data items...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class named Date in C++ Class has 3 private data items (name the data...

    Create a class named Date in C++ Class has 3 private data items (name the data items month, day, year) int month int day int year Member functions ‘getters’   a getter for each data item Use ‘get’ and the data items name The data item name must be capitalized Return the data item Example: int getMonth()        {return month;} ‘setters’   a setter for each data item Use ‘set’ and the data items name The data item name must be capitalized Change...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • Help with this coding assignment for C++! Add any comments for better understanding. Create a class...

    Help with this coding assignment for C++! Add any comments for better understanding. Create a class named CupCake. It contains: • Has private instance variables 1. egg 2. butter 3. baking powder 4. sugar 5. flour 6. milk • All members must have public methods: getter() and setter(). • A constructor - a default constructor with no arguments. The constructor will initial all member variable to a default value 0. • One public pure virtual function showingredients() that returns void....

  • Ticket Hierarchy Ticket Abstract Class Create a static variable called nextTicketId. This is an integer representing...

    Ticket Hierarchy Ticket Abstract Class Create a static variable called nextTicketId. This is an integer representing the next available integer for the ticketId                                 private static int nextTicketId = 1000; getPrice method: Abstract method that returns a double. NOTE: Do not make price an instance variable in Ticket. Noargument constructor: Sets event name to “none”, event location to “none”, and ticket id to 0. Create a constructor that accepts the event name and event location as parameters. Set the ticket Id...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT