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
|
#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);
{
name = new char[258];
strcpy(name, 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::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;
}
|
Got the followings errors :
line 14 - expected declaration
line 40 - no instance of overloaded function "Rectangle::Rectangle" matches the specified type
line 66 - expected declaration
line 77 - "Rectangle::~Rectangle()" (declared at line 25) is inaccessible and "Rectangle::Rectangle(double x, double y, char *z)" (declared at line 47) is inaccessible
getArea etc.. in the main are not accessible neither I do not understand why ?
Can you help me fix those errors?
Also I think I'm doing this part wrong too : Add a function called setName(char *) which sets the name of the
rectangle to a new name (it does not dynamically allocate memory)
Thank you