9 Replies - 5167 Views - Last Post: 11 January 2006 - 03:21 AM Rate Topic: -----

#1 Purplemonkeydishwasher   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-January 06

Link Lists using Classes/Polymorphism

Post icon  Posted 10 January 2006 - 02:59 PM

Hey I'm a bit of a newbie, I started using Dev-C++ from Bloodshed today (dont own my own copy of MVC++6.0), and I'm not very comfortable with it.

I am doing a program that must be object orientated eg:overloading etc..

The program is concerned with establishing customers records for a Hire purchase firm. Records are to be in a link list.

Now I'm trying to get started, and rather than the big bang approach im trying to get it bit by bit. Am I heading in the right direction!! I reckon i could do a link list, but how do i make it Object oriented??

#include <iostream.h>
#include <stdlib.h>

class node
{
   int acno;
   char name;
   float bal;
   char acstat;
   int day_payment;
   int month_payment;
   node *p;

   public:
   node *start;
   node *prev;
   node *current;
   node *nxt;
   void display();
   void add();
   void remove( int num);
   void details (int num);
   ~node();	// destructor
   };

   node::node() // constructor to initilise class
   {
   p = NULL;
   start = NULL;
   }
   
   void node::display()
   {
        node *temp;     
        temp = start;    // copies start to temp
        
        if (temp == NULL)
        cout << "No Customers In List!" << endl;
        else
        {
            while (temp != NULL)
            {
                  // display all customer details
                  cout << "Name : " <<temp ->name << endl;
                  cout << "Account No:" << temp ->acno <<endl;
                  cout << "Balance: " << temp ->bal << endl;
                  cout << "Account Status" <<temp ->acstat <<endl;
                  cout << "Date of Payment (dd/mm)" <<temp ->day_payment <<"/" << temp ->month_payment <<endl;
                  if( temp == current)
                  cout << "---Current Node---" <<endl;
                  temp = temp ->nxt;
                  }// while
                  cout << "End Of List" <<endl;
                  }    // else
                  }       // end function


   /*void node::details(int num)
   {
   if (p == NULL)
   {

   }
   */
   int main()
   {
        
        int option = 0;
        do
        {
              cout << "Please Select Option :" << endl;
              cout<< "0. Exit the Program" <<endl;
              cout<< "1. Find A Particular Customer and List Details" << endl;
              cout<< "2. List All Customer Details" <<endl;
              cout<< "3. Add a Customer to the List" <<endl;
              cout <<"4. Remove Customer" <<endl;
              cout <<"5. Remove all Customers with 0 Status" <<endl;
              cout <<"6. Move Person with Highest Debit to Head of List" <<endl;
              cout<< endl;
              cin >> option;
              
              switch (option)
              {
                     case 2 : display();
                              break;
                              
              }// switch
              } // do
              while (option !=0);
              return();
              }


I know theres a lot more work to go into it!!

Is This A Good Question/Topic? 0
  • +

Replies To: Link Lists using Classes/Polymorphism

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Link Lists using Classes/Polymorphism

Posted 10 January 2006 - 03:10 PM

Is your question how to make it OOP? You have defined a class...that in itself is a large part of OOP...it's the first O...the object. By making a class that can be resued, and acts like a black box to the user, you have implemented one of the main facets of object oriented programming.

Is your question about overloading?
Was This Post Helpful? 0
  • +
  • -

#3 Mrafcho001   User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: Link Lists using Classes/Polymorphism

Posted 10 January 2006 - 03:20 PM

Check out the snippets, i have a linked list pretty well wrapped up in a class. You might like it.
Was This Post Helpful? 0
  • +
  • -

#4 Purplemonkeydishwasher   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-January 06

Re: Link Lists using Classes/Polymorphism

Posted 10 January 2006 - 03:23 PM

So by declaring a class instead of a struct, it makes it OO. Well thanks so thats means I'm on the right track. But what would be a good way to show overloading in a link list dealing with customers? Sorry if my questions arent so clear...
Was This Post Helpful? 0
  • +
  • -

#5 Jessehk   User is offline

  • New D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 24
  • Joined: 06-January 06

Re: Link Lists using Classes/Polymorphism

Posted 10 January 2006 - 07:54 PM

One thing that is usually not the best idea is making members public. A key point of OOP (or so I have read/learned) is data protection.

Secondly, an OOP linked-list would probably consist of several Nodes.
Where a Node could be a structure of data that you want to store. For example, the person's name, salary, and address.

Then you could develop methods like list.add(params), or list.remove(), or overload the square bracket operator to access individual elements.

Lastly (as much as I can tell you with my (limited) level of knowledge), there is a lot more to OOP then wrapping a class around a struct.

Here is an example (which might not be well coded, as I am relatively new to this too), of a linked list I made when I was bored.

http://hakuch.tripod...esse/linked.cpp

This post has been edited by Jessehk: 10 January 2006 - 08:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 Mrafcho001   User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: Link Lists using Classes/Polymorphism

Posted 10 January 2006 - 08:51 PM

Jessehk, on 10 Jan, 2006 - 08:51 PM, said:

One thing that is usually not the best idea is making members public. A key point of OOP (or so I have read/learned) is data protection.

Secondly, an OOP linked-list would probably consist of several Nodes.
Where a Node could be a structure of data that you want to store. For example, the person's name, salary, and address.

Then you could develop methods like list.add(params), or list.remove(), or overload the square bracket operator to access individual elements.

Lastly (as much as I can tell you with my (limited) level of knowledge), there is a lot more to OOP then wrapping a class around a struct.

Here is an example (which might not be well coded, as I am relatively new to this too), of a linked list I made when I was bored.

http://hakuch.tripod...esse/linked.cpp

One thing that makes your linked list somewhat not usable in all programs is that you output messages when you are in the linked list, what if hte user wants to use it in win32? They would have to go in and edit the code, that is somewhat destroying the purpose of OOP and reusable code.

http://code.dreaminc...pet.php?sid=300

there is a good example of wrapping a linked list in a class.

When making object in OOP program, try to make the object as independable as possible, try to keep them as independed from the rest of the code as possible. I have a really good example of this. When wrapping DirectX APIs in a class, you make it so it can be used in any othe rapplication. If you make it application specific, for example add drawing functions and render objects, then the person using the classes might have to go and edit the code. As for re-usable code, they migth just go in and edit a line or two of code and make it work with their game.

This post has been edited by Mrafcho001: 10 January 2006 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#7 Jessehk   User is offline

  • New D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 24
  • Joined: 06-January 06

Re: Link Lists using Classes/Polymorphism

Posted 10 January 2006 - 08:57 PM

The intention is not reusability. It is a Student list that can store information on students. I didn't make it for practical purposes, I just made it for my own enjoyment. If I wanted to write a container-class, I would, but that was never the intention of that program. :)

I wrote it for a specific purpose, and for that purpose, I think it does well.

furthermore, why would anyone copy and paste your version when the STL pre-made linked list is available?

This post has been edited by Jessehk: 10 January 2006 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#8 Mrafcho001   User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: Link Lists using Classes/Polymorphism

Posted 10 January 2006 - 09:01 PM

Yes, im sure it does well for what its ment to do, but im just pointing out that OOP, independend and reusable code go hand in hand.
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Link Lists using Classes/Polymorphism

Posted 10 January 2006 - 09:02 PM

Purplemonkeydishwasher, on 10 Jan, 2006 - 06:20 PM, said:

So by declaring a class instead of a struct, it makes it OO. Well thanks so thats means I'm on the right track. But what would be a good way to show overloading in a link list dealing with customers? Sorry if my questions arent so clear...

Sorry, I should have been clearer...a program is not automatically OO because classes are used. The declaration of a class as an object is the first step...the next is the implementation. I mentioned earlier the idea of the 'black box'...that is the concept that tasks/manipulations can be performed without the need for all the data to be exposed to the user (as Jessehk noted - data protection).

Although it is meant for java, I find this set of articles to be an excellent guide to OOP concepts. Check them out...it's a good read, and will explain exactly what OOP means.

Here's the Wikipedia article as well
Was This Post Helpful? 0
  • +
  • -

#10 Purplemonkeydishwasher   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-January 06

Re: Link Lists using Classes/Polymorphism

Posted 11 January 2006 - 03:21 AM

Thanks for all the help guys, I'll let you know how i get on, got some nice snippets to learn from.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1