Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have two structs for members one for date attributes and second for bookrecord attributes i want to know how they wil be accessible from main when we create objects of that type for class.im going right way or not and where in my code i should specify the size for linklist in constructor(where to declare variable for size and intialize.a little bit confused.

C++
class singlelink

{
  Private:
  struct date
  {
      char month[8]; 
      int day;
      int year;
  };
  struct bookrecord
  {
    char title[60];
    char name[50];
    date pub;
    string status;
    int num_ofcopies;
    Bookrecord *link;
  }*first;

  Public:
  singlelink()
  {
    first=NULL;
  }
}
Posted
Updated 9-Nov-11 20:36pm
v7

First: C++ is a formal language, and it is case sensitive. Public and Private (the way you wrote them) aren't keywords, and Bookrecord is not declared anywhere (but there is bookrecord: did you made a typo?)

Then: singlelink is a class that contain a pointer (first) and bookrecord contains a pointer as well (link).
Have I to assume you want siglelist to be the manager of the list formed by chaining book-record-s?
Not a good design in term of reuse, but for a start can work.

Now, OK the constructor for singlelink initializing as "linking nothing", but now you've to add some more methods:

-at least you need and addrecord, taking a bookrecord* tha pushes the bookrecord in front of the list (how pointer shold work, is part of your exercise...)
-then you need a destructor (~singlelink) that deletes all the bookrecord chain, deleting the bookrecord one by one.
-assuming you don't need to copy and assign singlelist-s between them, you have to disable the copy and assign operations (just declare singlelink(const singlelink&) and singlelinkl& operator=(const singlelink&) as private).

-At that point, in you main, just create a singlelink variable, than create whatever singlelink::bookrecord with new and give their pointer to your add method.

Right now, you have a consistent chain. To access it, your single link must have a method hat returns a bookrecord* (the head of the chain) and a bookrecord method that return the bookrecord* to the next.
Or, alternatively, you need an iterator class (but that's a more advanced thing, you are not yet at the proper level)
 
Share this answer
 
Comments
Member 8370427 10-Nov-11 3:21am    
you look like very advance programmer please suggest me a book or some easy material from root to understand this.
Sergey Alexandrovich Kryukov 10-Nov-11 11:10am    
Easy? If you want to do something good -- it's never easy. Better prepare yourself for some hard work.
--SA
Emilio Garavaglia 10-Nov-11 13:23pm    
You can start from here:
http://www.relisoft.com/book/web_preface.html
Sergey Alexandrovich Kryukov 10-Nov-11 11:09am    
My 5.
--SA
Look, C++ is a case-sensitive language; so you code won't compile just because of wrong casing. There are other reasons to fail compilation. Where is the ';' at the end? This code is not very much of C++; and it does nothing. The idea to specify the size of linked list (is it a number of nodes or what? no matter, makes no sense anyway) is totally wrong. Even though the support if the current size of a collection in a class member is good, you need to increment/decrement this number dynamically on each transaction (add/insert/delete an element).

—SA
 
Share this answer
 
First of all, be clear about what you are trying to ask or do? Linked list is simple to understand only if you are familiar with basic C++ and pointers.

A structure is simply a definition like a class. It also needs an object to be declared so that you can use it.

Anything inside Private access specifier cannot be accessed from outside of the class, not even using its own object.

There are lot more things to explain here, but that will make it as a C++ tutorial post.

Solution: Search internet and learn what is structure/class, pointers and linked list in C++.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900