array of objects and inheritance

I have Class A as a base class , and Class B , C derived classes from A and there's class D who have a data member (pointer to Array) of type A (Composition)



class D{
A **a;
int size;
.......
a = new A*[size];
......
};
and i have Print method , in its body i have to specific element (if it from class B or C ) with a given ID(both B and C have a data member ID ) there should be 2 options in print function .. printing elements for class B , or printing elements for class C ? how can i specific the elements ?
Just define print() as virtual in A, override it in B and C.
Do want to be able to print everything, and indicate what type is being printed,
Or you want to be able to select just one type and print just that type?
think u @tipaye !
i want 3 options :
1 printing elements from class B .
2 printing elements from class C .
3 printing All elements from class B and class C .
Last edited on
@ResidentBiscuit !
i did it before !!
but what i want is in PRINT() of Class D, how can i specific the elements if it from class B (so i call print from class B) , or class C ?
Last edited on
See if you can get any ideas from this:
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
#include <iostream>
#include <string>	
#include <vector>	

using namespace std;

class A
{
public:
	virtual ~A() {};
	virtual const string &classID() const = 0;
	virtual void Print() const = 0;
	virtual const string &Id() const = 0;
};

class B : public A
{
	static const string id;
	
public:
	const string &classID() const { return id; }
	void Print() const { cout << id << endl; }
	const string &Id() const { return id; }
	virtual ~B() {}
};

const string B::id("class B");

class C : public A
{
	static const string id;
	
public:
	const string &classID() const { return id; }
	void Print() const { cout << id << endl; }
	const string &Id() const { return id; }
	virtual ~C() {}
};

const string C::id("class C");

class D
{
	A **a;
	size_t asize;
	size_t index;
	
public:
	D(int num);
	~D();
	
	void Add(A* anA);
	void Print(string id = "");
	const size_t &size() const { return asize;}
};

D::D(int num) : asize(num), index(0)
{
	a = new A *[asize];
}
	
D::~D()
{
	delete [] a;
	a = 0;
}

void D::Add(A* anA) 
{ 
	if (index < asize)
		a[index++] = anA;
	else
		cout << "Error! A is full!" << endl;
}

void D::Print(string id)
{
	for (size_t i = 0; i < asize; ++i)
	{
		if ((id == a[i]->Id()) || (id == ""))
		{
			a[i]->Print();
		}
	}
}

int main()
{
	const int num = 20;
	D d(num);
	
	for (size_t i = 0; i < d.size(); ++i)
	{
		if (i % 2)
		{
			d.Add(new B);
		}
		else
		{
			d.Add(new C);
		}
	}
	
	cout << "Printing only class B" << endl;
	d.Print("class B");
	cout << endl << endl;
	cout << "Printing only class C" << endl;
	d.Print("class C");
	cout << endl << endl;
	cout << "Printing both class B and class C" << endl;
	d.Print();

	return 0;
}
Topic archived. No new replies allowed.