Self referential class || Stroring an address of a class

This is a discussion on Self referential class || Stroring an address of a class within the C++ Programming forums, part of the General Programming Boards category; Can we use a self referential class in C++? If yes what is the difference between self referencing in C ...

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Here is the code. The program is very simple, a book shop - in admin mode we need add books as in client mode we can subscribe them. I want to do this program using dynamic memory allocation not array of objects.

Your code is all over the place. Be careful with your global pointers, you are redeclaring local variables with the same name as your globals. Additionally:

1. void main is wrong. Read how to define main - FAQ
2. This is C++, not C. You should be using std::string vice char arrays.
3. Again since this is c++, you should be using std::vector vice trying to implement a linked list.

Letting us know what isn't working and the error messages you recieve is the way to get more specific help here.

1) This isn't C++, it's "C with classes"
2) Replace your compiler with a standard-compliant one. iostream.h is pre-standard and deprecated.
3) Why are you rolling your own linked list? Use std::list
4) Why does each bookshop only hold one book? I suggest a single class bookshop which can hold several class book, in for example an std::list or vector
5) main is leaking memory. Look up RAII and smart pointers.
6) "bookshop *temp=new bookshop(temp);" temp is uninitialized when passed to bookshop()

I really appreciate pointing out mistakes, but whenever a newcomer is asking for help, everyone is proposing already-made tools. That's great, but keep in mind that C/C++ is used for learning BASIS of programming. The fact that you have smart pointers, linked-lists, vectors, strings here does not mean that these will be present in every other language. Telling him to use std::list to solve the problem is not the way to go. Let him get his idea working first and then prove it is not a good solution.

Yet you require from him distinguishing C from C++, while one is generally a superset of the other.

About the leaking memory:
The global pointer variable "start", which points to the first node, has to be deleted. The memory it points to has to be freed when it is no longer used. You might want to do this at the end of your main():

This will delete only the first node, since you pass to the delete operator only start. Nodes should be deleted recursively. You can do this implementing a destructor:

Code:

bookshop::~bookshop() // destructor
{
if(next!=NULL) // if this is not the last node, delete the next one
{
delete next;
}
}

Your constructor does not need the 'temp' parameter. You do not need to pass the object's address to any of its method, since it is done automatically by the language. Remove this parameter and use 'this' pointer instead, which has the functionality you want.

I really appreciate pointing out mistakes, but whenever a newcomer is asking for help, everyone is proposing already-made tools. That's great, but keep in mind that C/C++ is used for learning BASIS of programming. The fact that you have smart pointers, linked-lists, vectors, strings here does not mean that these will be present in every other language. Telling him to use std::list to solve the problem is not the way to go.

I disagree. Computer science is not the only basis for programming; programmatic problem solving is another basis (now I feel like I'm talking linear algebra, but anyway...), and in this it makes sense to use the tools provided by C++. Sure, one should also learn how to implement these data structures, but this should be done as a separate exercise which comes with the teaching of how to utilise RAII. I suggest that you read Stroustrup's essay on Learning Standard C++ as a New Language (PDF) and Koenig and Moo's Accelerated C++ as an example of the general approach. (Sara th: you may want to consider working through Accelerated C++ yourself to get a good grounding in C++ and programming technique.)