473,480 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Class Coding

26 New Member
Hi guys..i need some help transforming the following code to class style. i dont have any idea about classes. it's only functional but our prof wants it to be in a class coding. i need help.. here's my code.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5.   {  char name[20];
  6.      int age;
  7.      int id_num;
  8.      node *nxt;
  9.   };
  10.  
  11. node *start_ptr = NULL;
  12. node *current;
  13. int option = 0;
  14.  
  15.  
  16. void insert_at_end()
  17.   {  node *temp, *temp2;
  18.  
  19.      temp = new node;
  20.      cout << "Please enter the name of the student: ";
  21.      cin >> temp->name;
  22.      cout << "Please enter the age of the student : ";
  23.      cin >> temp->age;
  24.      cout << "Please enter the ID Number of the student : ";
  25.      cin >> temp->id_num;
  26.      temp->nxt = NULL;
  27.  
  28.      if (start_ptr == NULL)
  29.        { start_ptr = temp;
  30.      current = start_ptr;
  31.        }
  32.      else
  33.        { temp2 = start_ptr;
  34.          while (temp2->nxt != NULL)
  35.            {  temp2 = temp2->nxt;
  36.            }
  37.          temp2->nxt = temp;
  38.        }
  39.   }
  40.  
  41. void display_list()
  42.   {  node *temp;
  43.      temp = start_ptr;
  44.      cout << endl;
  45.      if (temp == NULL)
  46.        cout << "The list is empty!" << endl;
  47.      else
  48.        { while (temp != NULL)
  49.        {
  50.           cout << "Name : " << temp->name << " ";
  51.           cout << "Age : " << temp->age << " ";
  52.           cout << "ID Number : " << temp->id_num;
  53.           if (temp == current)
  54.           cout << " \t\t --> Current First Element";
  55.           cout << endl;
  56.           temp = temp->nxt;
  57.        }
  58.      cout << "\nEnd of list!" << endl;
  59.        }
  60.   }
  61.  
  62. void delete_start_node()
  63.    { node *temp;
  64.      temp = start_ptr;
  65.      start_ptr = start_ptr->nxt;
  66.      delete temp;
  67.    }
  68.  
  69. void delete_end_node()
  70.    { node *temp1, *temp2;
  71.      if (start_ptr == NULL)
  72.           cout << "The list is empty!" << endl;
  73.      else
  74.         { temp1 = start_ptr;
  75.           if (temp1->nxt == NULL)
  76.              { delete temp1;
  77.                start_ptr = NULL;
  78.              }
  79.           else
  80.              { while (temp1->nxt != NULL)
  81.                 { temp2 = temp1;
  82.                   temp1 = temp1->nxt;
  83.                 }
  84.                delete temp1;
  85.                temp2->nxt = NULL;
  86.              }
  87.         }
  88.    }
  89.  
  90. int main() { 
  91.  
  92.  system("CLS");
  93.  start_ptr = NULL;
  94.      do
  95.     {
  96.       display_list();
  97.       cout << endl;
  98.       cout << "1. Add a node to the end of the list." << endl;
  99.       cout << "2. Delete the start node from the list." << endl;
  100.       cout << "3. Delete the end node from the list." << endl;
  101.       cout << "4. Exit application." << endl;
  102.       cout << "\nEnter your choice: ";
  103.       cin >> option;
  104.  
  105.       switch (option)
  106.         {
  107.           case 1 : insert_at_end(); break;
  108.           case 2 : delete_start_node(); break;
  109.           case 3 : delete_end_node(); break;
  110.         }
  111.     }
  112.      while (option != 4);
  113.      cout << endl;
  114.      return 0;
  115.   }
Dec 28 '07 #1
3 1426
sicarie
4,677 Recognized Expert Moderator Specialist
Okay, have you read about classes? Do you know what would translate well from your program to a class?

PS - you should read up on current C++ standards, you're using the depricated header method (including the .h). I didn't look for it, but you should also make sure you're declaring the proper namespace(s).
Dec 28 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Before you get all caught up in classes, be aware that classes in C++ are implemented as structs. That is, this code:
Expand|Select|Wrap|Line Numbers
  1. struct MyStuff
  2. {
  3.      private:
  4.         int data;
  5.      public:
  6.         MyStuff(int arg);   //constrcutor
  7.       ~MyStuff();    //destructor
  8.      int GetData(MyStuff* arg);
  9. };
  10.  
is identical to this code:
Expand|Select|Wrap|Line Numbers
  1. class MyStuff
  2. {
  3.      private:
  4.         int data;
  5.      public:
  6.         MyStuff(int arg);   //constrcutor
  7.       ~MyStuff();    //destructor
  8.      int GetData(MyStuff* arg);
  9. };
  10.  
That is, all that changed was the keyword struct to class.

The only difference between a struct and a class is that the default access for a struct is public whereas the default access for a class is private.

So, your problem is already solved. You are already using classes.

All you need to do is move the function prototype of your functions inisde the struct where they become member functions. You should be able to recompile all everythng should still work. A function:
Expand|Select|Wrap|Line Numbers
  1. int GetData(MyStuff* arg)
  2. {
  3.  
  4. }
  5.  
becomes:
Expand|Select|Wrap|Line Numbers
  1. int MyStuff::GetData(MyStuff* arg)
  2. {
  3.  
  4. }
  5.  

Once your functions are inside the struct, then you can make the data members private. This will cut off any access to those members from anay function that is not a member function.

Once that works, then you can remove the function argument for the address of the struct variable since that address is supplied by the compiler as an invisible first argument for member functions:
Expand|Select|Wrap|Line Numbers
  1. struct MyStuff
  2. {
  3.      private:
  4.         int data;
  5.      public:
  6.         MyStuff(int arg);   //constrcutor
  7.       ~MyStuff();    //destructor
  8.      int GetData();   //<<<<<<<<<<no need for MyStuff* argument
  9. };
  10.  

Finally, change the keyword from struct to class. Or, just leave it as a struct. It makes no difference.
Dec 28 '07 #3
Synapse
26 New Member
oh i got it..thanks a lot for the help dudes.
Jan 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

50
6281
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
8
2083
by: Ares Lagae | last post by:
When adopting the coding style of the standard C++ library, you often run into naming problems because class names are lower case, and member functions do not have get/set prefixes. For example:...
21
4026
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
17
3537
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
2
1877
by: sck10 | last post by:
Hello, I created a class (public class General) with the following. My question is, is this good practice to have a group of methods(?) in one class, or should I have a separate class for each...
14
2604
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
1
4664
by: Jennifer Jazz | last post by:
My question is regarding the mapping of Class diagram to the C++ coding. There are 3 realtions in Class diagram 1) Assosication 2) Composition 3) Aggregation (Weak Composition). ...
5
1167
by: =?Utf-8?B?RU1hbm5pbmc=?= | last post by:
I'm very new to VB.Net and am struggling with the concept of classes and when to use them. I have found some coding to email exceptions to myself that might occur in this project I'm developing. ...
6
2123
by: jmarcrum | last post by:
Hi everyone! I'm using a super class (DVD.java) that handles another class (EnhancedDVD.java). I want to pass the "details" of the DVD into the super class DVD.java. The super class contains the...
15
7836
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.