Having problem with inheritance and polymorphism

Hello guys, I have written a code which includes inheritance and polymorphism, the code is about furniture management system here is the code in the "furniture management system.h" and "furniture management system.cpp" I have problem with assigning a new Sofa and Bookcase to my double-pointer array.
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
109
110
111
112
113
114
115
116
117
118
119
 "furniture management system.h"
#include "Furniture.h"
#include "Sofa.h"
#include "Bookcase.h"

class Furmansys
{
private:
	Furniture **furnArray;
	int size;
	int numFurn;

public:
	Furmansys();
	Furmansys(const Furmansys &obj);
	void addFurniture();
	~Furmansys();
}


and here is 

"furniture management system.cpp"

#include "Furniture Management System.h"

Furmansys::Furmansys()
{
	size=1;
	numFurn=0;
	furnArray=new Furniture*[size];
	for (int i=0; i<size; i++)
	{
		furnArray[i]=NULL;
	} 
}

Furmansys::Furmansys(const Furmansys &obj)
{
	size=obj.size;
	furnArray=new Furniture*[size];
	for (int i=0; i<size; i++)
	{
		furnArray[i]=obj.furnArray[i];
	}
}

void Furmansys::addFurniture()
{
	string furntype;
	string name;
	string price;
	string stock;
	string seats;
	string color;
	string wood;
	string height;
	if (numFurn >= size)
	{
		size+=1;
		Furniture **temp= new Furniture*[size];
		for (int i=0; i<size; i++)
		{
			temp[i]=NULL;
		}

			for (int i=0; i<numFurn; i++)
			{
				temp[i]=furnArray[i];
			}
			delete [] furnArray;
			furnArray=temp;
			temp=NULL;
	}

	cin.ignore();
	cout<<"Please wite the type of the furniture you wish to add (Sofa or Bookcase): ";
	getline(cin,furntype);

	if (furntype=="Sofa" || furntype=="sofa")
	{
		cout<<"Please write your Sofa's name: ";
		getline(cin,name);
		cout<<"Please write your Sofa's price: ";
		getline(cin,price);
		cout<<"Please write your Sofa's stock availability: ";
		getline(cin,stock);
		cout<<"Please write your Sofa's seat number: ";
		getline(cin,seats);
		cout<<"Please write your Sofa's color: ";
		getline(cin,color);
		furnArray[numFurn++]=new Sofa(name, price, stock, seats, color);
	}

	else if (furntype=="Bookcase" || furntype=="bookcase")
	{
		cout<<"Please write your Bookcase's name: ";
		getline(cin,name);
		cout<<"Please write your Bookcase's price: ";
		getline(cin,price);
		cout<<"Please write your Bookcase's stock availability: ";
		getline(cin,stock);
		cout<<"Please write your Bookcase's wood type: ";
		getline(cin,wood);
		cout<<"Please write your Bookcase's height: ";
		getline(cin,height);
		furnArray[numFurn++]=new Bookcase(name, price, stock, seats, color);
	}
}
Furmansys::~Furmansys()
{
	for (int i=0; i<numFurn; i++)
	{
		delete furnArray[i];
	}

	delete [] furnArray;
	furnArray=NULL;
}
Last edited on
> I have problem with assigning a new Sofa and Bookcase to my double-pointer array.
Be more specific.
And indent your code
here is the assignent description:

Your task is to build a system for furniture management for a well-known furniture company.
The program will handle different types of furniture but your task is to deal with only two
types: sofas and bookcases. The program will later on be developed further by other system
developers, so remember to be clear about the naming and comments in the source code.
For all furniture it shall be possible to store the furniture's name, price and stock availability.
For sofas it shall also be possible to store the number of seats (eg "4") and color (such as
"beige") while the bookshelves wood (eg "birch") and height (eg, "183 "[cm]). All furniture
should be able to return all of their information as a string. It should also be possible to know
the inventory records by entering the name of the furniture.
All furniture should be handled in a furniture management system that contains / stores all
the furniture. Furniture management system should be able to:
1 ) Presenting all the furniture
2) Present stock availability of a given furniture
3) Introducing the total balance of the selected type of furniture
4) Presenting all the sofas with all related information
5) Presenting all the bookcases with all related information
6) Adding a furniture
7) Remove a furniture
You shall create an inheritance hierarchy for furniture. Remember that abstract baseclass,
polymorphism and dynamic binding is used (hint: virtual).
You shall also create a class for the furniture management system in which one (1)
dynamically allocated array is used (declared as a "double-pointer") to keep / store all the
furniture.
In the furniture management system You shall also implement copy constructor, assignment
operator and a virtual destructor.
Finally You shall implement an executable file containing the main () which corresponds to a
menu-based system for handling furniture.
Of course, there should be no memory leaks.
I am going to write each header and cpp file i have:

1) Furniture.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

class Furniture
{
protected:
	string Fname;
	string Price;
	string Stock;
public:
	Furniture();
	virtual void print()=0;
	Furniture(string name, string price, string stock);
	void setName(string name);
	string getName();
	void setPrice(string price);
	string getPrice();
	void setStock(string stock);
	string getStock();
	~Furniture();
};
Last edited on
2) Furniture.cpp

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
#include "Furniture.h"

Furniture::Furniture()
{
	Fname="";
	Price="";
	Stock="";
}

Furniture::Furniture(string name, string price, string stock)
{
	Fname=name;
	Price=price;
	Stock=stock;
}

void Furniture::setName(string name)
{
	Fname=name;
}

string Furniture::getName()
{
	return Fname;
}

void Furniture::setPrice(string price)
{
	Price=price;
}

string Furniture::getPrice()
{
	return Price;
}

void Furniture::setStock(string stock)
{
	Stock=stock;
}

string Furniture::getStock()
{
	return Stock;
}

Furniture::~Furniture()
{

}
Last edited on
3) Sofa.h
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 "Furniture.h"

class Sofa : public Furniture
{
private:
	string Seats;
	string Color;
public:
	Sofa();
	Sofa(string name, string price, string stock, string seats, string color) : Furniture(name,price,stock)
	{
		Seats=seats;
		Color=color;
	}
	virtual void print()
	{
		cout<<Fname<<", "<<Price<<", "<<Stock<<", "<<Seats<<", "<<Color<<endl;
	}
	void setSeats(string seats);
	string getSeats();
	void setColor(string color);
	string getColor();
	~Sofa();
};
Last edited on
4)Sofa.cpp

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
#include "Sofa.h"

Sofa::Sofa()
{
	Seats="";
	Color="";
}

void Sofa::setSeats(string seats)
{
	Seats=seats;
}

string Sofa::getSeats()
{
	return Seats;
}

void Sofa::setColor(string color)
{
	Color=color;
}

string Sofa::getColor()
{
	return Color;
}

Sofa::~Sofa()
{

}
Last edited on
5)Bookcase.h

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
#include "Furniture.h"

class Bookcase : public Furniture
{
private:
	string Wood;
	string Height;
public:
	Bookcase();
	Bookcase(string name, string price, string stock, string wood, string height) : Furniture(name, price, stock)
	{
		Wood=wood;
		Height=height;
	}

	virtual void print()
	{
		cout<<Fname<<", "<<Price<<", "<<Stock<<", "<<Wood<<", "<<Height<<endl;
	}

	void setWood(string wood);
	string getWood();
	void setHeight(string height);
	string getHeight();
	~Bookcase();
};
Last edited on
8)Bookcase.cpp

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
#include "Bookcase.h"

Bookcase::Bookcase()
{
	Wood="";
	Height="";
}

void Bookcase::setWood(string wood)
{
	Wood=wood;
}

string Bookcase::getWood()
{
	return Wood;
}

void Bookcase::setHeight(string height)
{
	Height=height;
}

string Bookcase::getHeight()
{
	return Height;
}

Bookcase::~Bookcase()
{

}
Last edited on
9) furniture management system.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Furniture.h"
#include "Sofa.h"
#include "Bookcase.h"

class Furmansys
{
private:
	Furniture **furnArray;
	int size;
	int numFurn;

public:
	Furmansys();
	Furmansys(const Furmansys &obj);
	void addFurniture();
	~Furmansys();
}
Last edited on
10) furniture management system.cpp

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
#include "Furniture Management System.h"

Furmansys::Furmansys()
{
	size=1;
	numFurn=0;
	furnArray=new Furniture*[size];
	for (int i=0; i<size; i++)
	{
		furnArray[i]=NULL;
	} 
}

Furmansys::Furmansys(const Furmansys &obj)
{
	size=obj.size;
	furnArray=new Furniture*[size];
	for (int i=0; i<size; i++)
	{
		furnArray[i]=obj.furnArray[i];
	}
}

void Furmansys::addFurniture()
{
	string furntype;
	string name;
	string price;
	string stock;
	string seats;
	string color;
	string wood;
	string height;
	if (numFurn >= size)
	{
		size+=1;
		Furniture **temp= new Furniture*[size];
		for (int i=0; i<size; i++)
		{
			temp[i]=NULL;
		}

			for (int i=0; i<numFurn; i++)
			{
				temp[i]=furnArray[i];
			}
			delete [] furnArray;
			furnArray=temp;
			temp=NULL;
	}

	cin.ignore();
	cout<<"Please wite the type of the furniture you wish to add (Sofa or Bookcase): ";
	getline(cin,furntype);

	if (furntype=="Sofa" || furntype=="sofa")
	{
		cout<<"Please write your Sofa's name: ";
		getline(cin,name);
		cout<<"Please write your Sofa's price: ";
		getline(cin,price);
		cout<<"Please write your Sofa's stock availability: ";
		getline(cin,stock);
		cout<<"Please write your Sofa's seat number: ";
		getline(cin,seats);
		cout<<"Please write your Sofa's color: ";
		getline(cin,color);
		furnArray[numFurn++]=new Sofa(name, price, stock, seats, color);
	}

	else if (furntype=="Bookcase" || furntype=="bookcase")
	{
		cout<<"Please write your Bookcase's name: ";
		getline(cin,name);
		cout<<"Please write your Bookcase's price: ";
		getline(cin,price);
		cout<<"Please write your Bookcase's stock availability: ";
		getline(cin,stock);
		cout<<"Please write your Bookcase's wood type: ";
		getline(cin,wood);
		cout<<"Please write your Bookcase's height: ";
		getline(cin,height);
furnArray[numFurn++]=new Bookcase(name, price, stock, wood, height);
	}
}
Furmansys::~Furmansys()
{
	for (int i=0; i<numFurn; i++)
	{
		delete furnArray[i];
	}

	delete [] furnArray;
	furnArray=NULL;
}


Compiler makes error of a value type of "Sofa*" cannot be assigned to an entity of type "Furniture*"

I have not written a main for my program yet.
Last edited on
Again, please indent your code.
1
2
3
4
class foo{};
class bar: public foo{};

foo *ptr = new bar(); //perfectly legal 


Paste the exact error message, along with the name and version of your compiler, and the command used to build.
the exact error message is:

a value type of "Sofa*" cannot be assigned to an entity of type "Furniture*" in line 68 of "furniture management system.cpp"

my version of compiler is v100 (Visual studio 2010 default platfrom toolset).

Last edited on
You are missing include guards, which is causing me `redefinition' errors
http://cplusplus.com/forum/articles/10627/

Also, missing semicolon in line 17 of `furniture management system.h'

After fixing that, it compiles fine with g++ or clang++, giving the warning
$ g++ -Wall -c 'furniture management system.cpp'
In destructor ‘Furmansys::~Furmansys()’:
warning: deleting object of abstract class type ‘Furniture’ which has non-virtual destructor will cause undefined behaviour [-Wdelete-non-virtual-dtor]
Thank you very kindly, it really helped.
Topic archived. No new replies allowed.