classes and friend-functions

Create a House class. A variable of type House must contain the following fields: street; number; number of apartments, floors, entrances, number of apartments on the floor; construction start date; construction completion date; name of the construction company.

Provide functions for performing the following operations: initialization of information, console input / output of information about the house, calculation of the number of apartments in the entrance, in the house using friend-functions.

PS by entrances they mean house parts. like a house can be devided into 2,3, 4 parts etc

I've just started working with classes, so this is quite confusing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include<string>
using namespace std;

class House
{
public:
    string street;
    int num;
    int amount;
    int floors;
    int entrances;
    int amount_on_a_floor;
    string date_start;
    string date_end;
    string name;
   
    
    void Chart()
    {
        
        
    }
};
This should get you started:

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
#include <iostream>
#include <string>

using std::string;
using std::ostream;
using std::cout;
using std::endl;

class House
{
public:
    House
    (//Constructor Argument List
        const string& street,
        int streetNumber,
        int amountOfApartments,
        int floors,
        int entrances,
        int amountOnAFloor,
        const string& dateStart,
        const string& dateEnd,
        const string& constructionCompanyName
            //Initializer List
    ) :     m_street(street),
            m_streetNumber(streetNumber),
            m_amountOfApartments(amountOfApartments),
            m_floors(floors),
            m_entrances(entrances),
            m_amountOnAFloor(amountOnAFloor),
            m_dateStart(dateStart),
            m_dateEnd(dateEnd),
            m_constructionCompanyName(constructionCompanyName)
    {}
    //the << operator does not know what to do with a house object since 
    //it is a custom type, so we tell it
    //what we want it to do using the operator keyword
    //This way we can simply cout FamilyHome object instead of creating
    //tons of getters and typing out all the data for each house object
    friend ostream& operator<<(ostream& os, House& house)
    {
        os << "Street: " << house.m_street << endl;
        os << "Street #: " << house.m_streetNumber << endl;
        os << "Number of Apartments: " << house.m_amountOfApartments << endl;
        os << "Floors: " << house.m_floors << endl;
        os << "Entrances: " << house.m_entrances << endl;
        os << "Amount on a floor: " << house.m_amountOnAFloor << endl;
        os << "Construction Date Start: " << house.m_dateStart << endl;
        os << "Construction Date End: " << house.m_dateEnd << endl;
        os << "Construction Company Name: " << house.m_constructionCompanyName << endl;

        return os;
    }


private:
    //Create variables and initialize them to a default value
    string  m_street{ "Street Name" };
    int     m_streetNumber{ 123 };
    int     m_amountOfApartments{ 1 };
    int     m_floors{ 3 };
    int     m_entrances{ 2 };
    int     m_amountOnAFloor{ 4 };
    string  m_dateStart{ "2/12/2021" };
    string  m_dateEnd{ "4/15/2021" };
    string  m_constructionCompanyName{ "Company Name" };
};

int main()
{
    House FamilyHome("Elm", 123, 4, 3, 2, 3, "2/13/2021", "4/20/2021", "Freddy's Dream Builds");

    cout << FamilyHome << endl;

    return 0;
}


Output:

Street: Elm
Street #: 123
Number of Apartments: 4
Floors: 3
Entrances: 2
Amount on a floor: 3
Construction Date Start: 2/13/2021
Construction Date End: 4/20/2021
Construction Company Name: Freddy's Dream Builds


Always make your data private unless you absolutely need it to be public. Also, I didn't add any getter methods, but they look like this:

string GetStreetName() const { return m_street; }

Although the operator<< will output the data for you so you don't need to call or make any getters unless you want to call specific information about the house. So lets say you just want the street name, then you would add that getter to the public section and call it in main like so:

cout << FamilyHome.GetStreetName() << endl;

Also, variables are initialized in the order they are declared in the class under the private section(Or wherever you define them inside the class definition). So you should declare them in the order you define them in the class, in the class constructor argument list AND the initializer list (the : after House()) so there's no undefined behavior.

https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP53-CPP.+Write+constructor+member+initializers+in+the+canonical+order#:~:text=Always%20write%20member%20initializers%20in,declared%20in%20the%20class%20definition.
Last edited on
thank you soooo much!! i really appreciate it!! :)
No problem :)
Topic archived. No new replies allowed.