1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Apartment;
class Person {
public:
Person(string, string, int, Apartment*);
string get_name();
string get_family();
int get_age();
Apartment* get_apartment();
void print();//added this
private:
string name;
string family;
int age;
Apartment* apartments;
};
class Apartment {
public:
Apartment(int, double);
int get_rooms();
double get_dimension();
private:
int rooms;
double dimension;
};
Person personCreate() ///helper function
{
std::string _Family{},_Fstname{};
int _Age=0,_Rooms=0;
double _Dimens =0.0;
std::cout<<"Enter your family : "; std::cin>>_Family;
std::cout<<"Enter your first name : "; std::cin>>_Fstname;
std::cout<<"Enter your age : "; std::cin>>_Age;
std::cout<<"Enter # of rooms owned: "; std::cin>>_Rooms;
std::cout<<"Enter the dimension of each room: "; std::cin>>_Dimens;
Apartment *_Ap = new Apartment(_Rooms,_Dimens); ///create a pointer to an apartment
Person _TempPerson(_Fstname,_Family,_Age,_Ap);
return _TempPerson;
}
int main()
{
///an example on usage
Person _Me = personCreate();
_Me.print();
return 0;
}
/*******************************class Person defs***************************************/
Person::Person(string _name, string _family, int _age, Apartment* _apartmnt) ///let the parameters have some meaning
:name(_name),family(_family),age(_age),apartments(_apartmnt) ///a constructor list init saves time
{
}
string Person::get_name()
{
return name;
}
string Person::get_family()
{
return family;
}
int Person::get_age()
{
return age;
}
Apartment* Person::get_apartment()
{
return apartments;
}
void Person::print()
{
std::cout<<"\n{"<<name<<" "<<family<<"} "\
<<age<<" years\n"\
<<std::string(20,'*')<<"\n"\
<<"rooms owned: "<<apartments->get_rooms()<<"\n"\
<<"each room @: "<<apartments->get_dimension()<<" M2\n\n";
}
/****************************class Apartment defs *****************************************/
Apartment::Apartment(int _rooms, double _dimens)
:rooms(_rooms),dimension(_dimens)
{
}
double Apartment::get_dimension()
{
return dimension;
}
int Apartment::get_rooms()
{
return rooms;
}
|