StefiIOE

RealEstate : inheritance

Apr 28th, 2020
728
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. class RealEstate {
  5. protected:
  6.     char*address;
  7.     int area;
  8.     int price;
  9.     void copy(const RealEstate &r) {
  10.         this->address=new char[strlen(r.address)+1];
  11.         strcpy(this->address,r.address);
  12.         this->area=r.area;
  13.         this->price=r.price;
  14.     }
  15. public:
  16.     RealEstate(char *address="",int area = 0 , int price = 0) {
  17.         this->address=new char[strlen(address)+1];
  18.         strcpy(this->address,address);
  19.         this->area=area;
  20.         this->price=price;
  21.  
  22.     }
  23.     RealEstate(const RealEstate &r) {
  24.         copy(r);
  25.     }
  26.     RealEstate &operator=(const RealEstate &r) {
  27.         if(this!=&r) {
  28.             delete [] address;
  29.             copy(r);
  30.         }
  31.         return *this;
  32.  
  33.     }
  34.     int Price () {
  35.         return this->area*this->price;
  36.     }
  37.     void print() {
  38.         cout<<address<<", Kvadratura: "<<area<<", Cena po Kvadrat: "<<price<<endl;
  39.     }
  40.  
  41.     float estateTax() {
  42.         return Price() * 0.05;
  43.     }
  44.  
  45.     friend istream &operator>>(istream &in , RealEstate &r) {
  46.         in>>r.address>>r.area>>r.price;
  47.         return in;
  48.     }
  49.     char *getAddress() {
  50.         return this->address;
  51.     }
  52.  
  53.  
  54.     ~RealEstate() {
  55.         delete [] address;
  56.     }
  57. };
  58.  
  59. class Villa :public RealEstate {
  60. protected:
  61.     int tax;
  62. public:
  63.  
  64.     Villa(char *address="",int area = 0 , int price = 0 , int tax= 0):RealEstate(address,area,price) {
  65.         this->tax=tax;
  66.  
  67.     }
  68.  
  69.     void print() {
  70.         cout<<address<<", Kvadratura: "<<area<<", Cena po Kvadrat: "<<price<<", Danok na luksuz: "<<tax<<endl;
  71.     }
  72.     int estateTax() {
  73.         return RealEstate::estateTax()+(area*price*tax/100);
  74.     }
  75.     friend istream &operator>>(istream &in , Villa &v) {
  76.         in>>v.address>>v.area>>v.price>>v.tax;
  77.         return in;
  78.     }
  79.  
  80. };
  81.  
  82. int main() {
  83.     RealEstate re;
  84.     Villa v;
  85.     cin >> re;
  86.     cin >> v;
  87.     re.print();
  88.     // cout<<"test1"<<endl;
  89.     cout << "Danok za: " << re.getAddress() << ", e: " << re.estateTax() << endl;
  90.     v.print();
  91.     // cout<<"test2"<<endl;
  92.     cout << "Danok za: " << v.getAddress() << ", e: " << v.estateTax() << endl;
  93.     return 0;
  94. }
RAW Paste Data