Been working on this problem about overloading function and private function but I got a couple of errors that I trying to solve but it's not successful until now.
Here the pb :
Enhance the Rectangle class with a private member function
called initName(char *). It should be the only function which
dynamically allocates a char array to hold the name. Your constructor functions should be the only functions which call it, and each constructor should call it instead of allocating their own arrays.
Add a function called setName(char *) which sets the name of the
rectangle to a new name (it does not dynamically allocate memory)
Demonstrate your class works with a main function that instantiates
an array of three Rectangle objects, one for each room of a house,
using the initializer list below. Then, use your setName() function to fix the name of “Offce” to “Office”. Finally, tell the user the name and area of each room, and the name of the largest room.
1 2 3
|
Rectangle house[] = { Rectangle(10, 12, "Kitchen"),
Rectangle(20, 20, "Bedroom"),
Rectangle(8, 12, "Offce") };
|
Here is my code :
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
|
#include <iostream> //
#include <iomanip>
#include <cstring>
#include <string>
using namespace std;
class Rectangle
{
private:
double width;
double length;
char *name;
void Rectangle::initName(char *n);
public:
//constructors
Rectangle();
Rectangle(double, double,
char *);
//destructor
~Rectangle();
void setWidth(double);
void setLength(double);
void setWidth(char *);
void setLength(char *);
void setName(char *);
int getWidth() const;
int getLength() const;
int getArea() const;
void printName() const
{
cout << name;
}
};
Rectangle::Rectangle()
{
width = 0;
length = 0;
initName("Default");
}
Rectangle::Rectangle(double x, double y, char *z)
{
width = x;
length = y;
initName(z);
}
void Rectangle::initName(char *n)
{
name = new char[258];
strcpy(name, n);
};
void Rectangle::initName(char *n)
{
name = new char[258];
strcpy(name, n);
};
void Rectangle::setWidth(double w)
{
width = w;
}
void Rectangle::setLength(double l)
{
length = l;
}
void setName(char *newname)
{
newname.name = "Office";
}
int Rectangle::getArea() const
{
return width * length;
}
int main()
{
Rectangle house[] = {Rectangle(10, 12, "Kitchen"),
Rectangle(20, 20, "Bedroom"),
Rectangle(8, 12, "Offce")};
for (int i = 0; i < 3; i++)
{
cout << "Area for " << house[i].printName() << "is :" << house[i].getArea();
}
if (house[1].getArea() > house[2].getArea() && house[1].getArea() > house[3].getArea())
{
cout << house[1].printName() << "has the biggest area";
}
else if (house[2].getArea() > house[1].getArea() && house[2].getArea() > house[3].getArea())
{
cout << house[2].printName() << "has the biggest area";
}
else
{
cout << house[3].printName() << "has the biggest area";
}
return 0;
}
|
I got the following three errors, can you help me fix it ?
Line 13 : rectangleEnhance.cpp:13:21: error: extra qualification on member 'initName'
void Rectangle::initName(char *n)
Line 27 : rectangleEnhance.cpp:67:12: error: member reference base type 'char *' is not a structure or union
newname.name = "Office";
Line 83 : rectangleEnhance.cpp:83:29: error: invalid operands to binary expression ('basic_ostream<char>' and 'void')
cout << "Area for " << house[i].printName() << "is :" << house[i].getArea();
For the line 83, I thought it has to do with the include <iostream> but not. And the others I have been looking how to fix it, but it not successful. Can you help me fic those 3 errors and give some explanation thank you.