Laser, the best way I have to explain you what I need is with an example. This is what I have done, my first approach without any success. Hope that is easier to understand if I put you a code, but since I don't know how to post it since I have three files I will put them separately:
First file: main.cpp
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Furniture.h"
using namespace std;
void print(Furniture *j)
{
printf("\nPrint using a function\n");
printf("Furniture: %s ", j->Getfurniture());
printf("Volume of furniture: %.2f ", j->Getvolume_furniture());
printf("Length: %.2f ", j->Getlength());
printf("Width: %.2f ", j->Getwidth());
printf("Depth: %.2f", j->Getdepth());
}
int main()
{
Furniture *j1 = new Furniture();
j1->Setfurniture("Furniture");
j1->Setvolume_furniture(volume_furniture);
j1->print();
j1->Setlength(10);
j1->print();
j1->Setwidth(20);
j1->print();
j1->Setdepth(30);
j1->print();
return 0;
}
Second file: Furniture.h
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();//Destructor
char* Getfurniture();
void Setfurniture(char* val);
float Getvolume_furniture();
void Setvolume_furniture(float val);
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
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Furniture.h"
Furniture::Furniture()
{
this->furniture=NULL;
this->volume_furniture=0.0;
this->length=0.0;
this->width=0.0;
this->depth=0.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(float val)
{
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 Setdepth(float val)
{
if (val<0) {
depth=0;
} else {
depth=val;
}
}
This is only an attempt to adapt a code that stores names, ages and heigths of people to this exercise that asks a description and a volume of an object. By the way, I failed even declaring the description and the values of measurements of the object and I should ask the user to input them through keyboard but I am still trying this way before proceeding to ask data through keyboard.
I created a new class in Code Blocks, I have the classes public, protected and private (we still don't use protected until the next week, we are still learning classes/OOP), "Getters" and "Setters".
I hope with this information you have a little idea of what I am doing.