Nooooo, I was writing to you but I signed out automatically while submitting the reply. Hopefully I loaded the auto-restore:
Jim, I finally solved the problems. I put the function print() in Furniture.cpp and I also had some errors in int main() because I printed the result 4 times and float Getvolume_furniture(); should be put below j1->Setlength(10); j1->Setwidth(20); and j1->Setdepth(30);
I put the code, still I didn't fixed the pointers but the program works, run:
First file: main.cpp (here I removed several j1->print() and moved j1->Setvolume_furniture();
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Furniture.h"
using namespace std;
int main()
{
Furniture *j1 = new Furniture();
j1->Setfurniture("Furniture");
j1->Setlength(10);
j1->Setwidth(20);
j1->Setdepth(30);
j1->Setvolume_furniture();
j1->print();
delete j1;
return 0;
}
Second File: Furniture.h (I didn't touch anything here this time)
Code:
#ifndef FURNITURE_H
#define FURNITURE_H
class Furniture
{
public:
Furniture();
Furniture(char* furniture, float volume_furniture, float length, float width, float depth);
virtual ~Furniture();
char* Getfurniture();
void Setfurniture(char* val);
float Getvolume_furniture();
void Setvolume_furniture();
float Getlength();
void Setlength(float val);
float Getwidth();
void Setwidth(float val);
float Getdepth();
void Setdepth(float val);
void print();
protected:
private:
char* furniture;
float volume_furniture;
float length;
float width;
float depth;
};
#endif // FURNITURE_H
Third File: Furniture.cpp (here I added the function print to "add properties of the method to the console", words of my instructor)
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Furniture.h"
Furniture::Furniture()
{
this->furniture=NULL;
this->volume_furniture=0.0;
this->length=1.0;
this->width=1.0;
this->depth=1.0;
}
Furniture::Furniture(char* furniture, float volume_furniture, float length, float width, float depth)
{
this->furniture=new char[strlen(furniture)+1];
strcpy(this->furniture,furniture);
this->volume_furniture=volume_furniture;
this->length=length;
this->width=width;
this->depth=depth;
}
Furniture::~Furniture()
{
if (this->furniture!=NULL) {
delete[] this->furniture;
}
}
char* Furniture:: Getfurniture()
{
return furniture;
}
void Furniture:: Setfurniture(char* val)
{
furniture=val;
}
float Furniture:: Getvolume_furniture()
{
return volume_furniture;
}
void Furniture:: Setvolume_furniture()
{
volume_furniture=length*width*depth;
}
float Furniture:: Getlength()
{
return length;
}
void Furniture:: Setlength(float val)
{
if (val<0) {
length=0;
} else {
length=val;
}
}
float Furniture:: Getwidth()
{
return width;
}
void Furniture:: Setwidth(float val)
{
if (val<0) {
width=0;
} else {
width=val;
}
}
float Furniture:: Getdepth()
{
return depth;
}
void Furniture:: Setdepth(float val)
{
if (val<0) {
depth=0;
} else {
depth=val;
}
}
void Furniture::print()
{
printf("Characteristics of the furniture: \n");
printf("Furniture: %s \n", this->furniture);
printf("Height: %.2f \n", this->Getlength());
printf("Width: %.2f \n",this->Getwidth());
printf("Depth: %.2f \n", this->Getdepth());
printf("Volume of the furniture: %.2f \n", this->Getvolume_furniture());
}
The program works, I know, I still have the pointers but with pointers the program works, I will fix it tomorrow (it's night in Spain). But... there is still something I have to fix: the program prints what I declared in int main(), a word Furniture, the declared values of length, width, depth and the volume. What I need to do is... a description that should be inputted through keyboard and the values should be inputted through keyboard as well. I know how to do that in int main() but now with classes I am lost. Could you help me to fix it? and we will finally solve the problem.