dynamic arrays

im creating a database of book and authors and ive been writing an implementation file..

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
#include "library.h"
#include <iostream>
#include <string>
#include <cassert>
using namespace std;

Library::Library()   //Creates an empty library without books and without authors.
{
   char *p;
   p = new char[library]
}

Library::~Library()  //Destructor
{
   delete [] p;
   p = NULL;
}

void Library::add(string title, string authors[], int nAuthors)// Add a book
{
     
     

}

void Library::print() const  // Print the list of all books ordered by book title.
{
     for(int i = 0; i<length; i++)
         cout << p[i] << " ";
     
}

void Library::printByAuthor(string author) const //Print the list of books of a given author, ordered by book title.
{
     
}


i wanted to know if you can see any problems with my definitions so far..thanks
I see no problems. But I wouldn't use "library" as the size of your array. Something more meaningful like "librarySize" etc

I also do
char *p = 0;

and
1
2
3
4
5
// indestructor
if (p != 0) {
 delete [] p;
 p = 0;
}


While not applicable now. It's a good technique to use for when you start doing lazy instantiation.

if i had to usea combination of link lists and dynamic arrays would this be suitable? Can ne1 help me with my empty functions

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
#include "library.h"
#include <iostream>
#include <string>
#include <cassert>
using namespace std;

Library::Library()
{
   first = NULL;
   last = NULL;
   count = 0;
}

Library::~Library()
{
   nodeType *temp;
   
   while (first != NULL)
   {
         temp = first;
         first = first->link;
         delete temp;
   }
   last = NULL;
   count = 0;
}

void Library::add(string title, string authors[], int nAuthors)
{


}

void Library::print() const
{
     bookType *current;

	 current = first;
	
	 while (current != NULL)
    {
	   cout << current->info << " ";
	   current = current->link;
    }
     
}

void Library::printByAuthor(string author) const
{
     
}
Why not simply use one of the STL container classes? - while it is (to a certain extent) useful to write a linked list from scratch as part of the learning process so that you understand the principals behind them, using the STL version lets you concentrate on the rest of your program.
1 of the conditions of this is that i cannot use stl i have to use pointer structures
Topic archived. No new replies allowed.