For example, a linked list data type might be composed of code and routines that allow us to
class StudentRecord { public: // define the data and routines that are externally accessible // a constructor for a class has the same name // as the class type, and no return type // it is used to create and initialize a class variable StudentRecord(); // a destructor for a class has the same name as the // class type, but preceded by the tilde // it is used to clean up any memory allocations when // one is finished with a class variable ~StudentRecord(); // routine to set the student's name void SetName(char newname[]); // routine to set the student's id number void SetId(int newid); // routine to set the student's mark for void SetMark(float newmark); // routine to get the student's name char *GetName(); // routine to get the student's id number int GetId(); // routine to get the student's mark float GetMark(); private: // define the data and routines that can only // be accessed by StudentRecord class routines char Name[60]; float Mark; int Id; };To use the student record class, other functions could declare variables to be of type StudentRecord, then access the functions in much the same way as the fields of structs are accessed:
int main() { StudentRecord astudent; // declare a student record variable, // this automatically results in the // StudentRecord() constructor being called // call the SetName function to set the student's name astudent.SetName("Some Name"); // call the SetId function to set the student's id number astudent.SetId(9301); // call the SetMark function to set the student's mark astudent.SetMark(84.5); // use the GetName function to print the name back out printf("%s\n", astudent.GetName()); // use the GetId function to print out the student's id printf("%d\n", astudent.GetId()); // use the GetMark function to print out the student's mark printf("%g\n", astudent.GetMark()); }What we haven't shown so far is how to write the bodies of the class functions.
For these, we need a special declaration indicating both the function name and which class the function is a part of:
These routines can access things in the "private" section of StudentRecord, but no other routines can.
StudentRecord::StudentRecord() // a constructor for a class has the same name // as the class type, and no return type // it is used to create and initialize a class variable { strcpy(Name, " "); Mark = 0.0; Id = 0; } StudentRecord::~StudentRecord() // a destructor for a class has the same name as the // class type, but preceded by the tilde // it is used to clean up any memory allocations when // one is finished with a class variable { strcpy(Name, " "); Mark = 0.0; Id = 0; } void StudentRecord::SetName(char newname[]) // routine to set the student's name { if (strlen(newname) < 1) { printf("Empty name string supplied\n"); } else { strcpy(Name, newname); } } void StudentRecord::SetId(int newid) // routine to set the student's id number { if (newid < 0) { printf("Invalid id supplied: %d\n", newid); } else { Id = newid; } } void StudentRecord::SetMark(float newmark) // routine to set the student's mark for { if ((Mark < 0) || (Mark > 100)) { printf("Invalid mark supplied: %g", newmark); } else { Mark = newmark; } } char *StudentRecord::GetName() // routine to get the student's name { return Name; } int StudentRecord::GetId() // routine to get the student's id number { return Id; } float StudentRecord::GetMark() // routine to get the student's mark { return Mark; }
#include <cstdio> #include <cstdlib> using namespace std; class ListItem { public: // available constructors to create new list item // - default has no parameters, ListItem(); // - alternative version lets you create and initialize // with desired data at the same time ListItem(float data, ListItem* next); // destructor to free up space once finished ~ListItem(); // functions (aka methods) to get and set data field void SetData(float data); float GetData(); // functions to get and set pointer to next item void SetNext(ListItem* ptr); ListItem *GetNext(); // function to print list item contents void printdata(); // function to print all following list contents void printlist(); private: ListItem* nextitem; float listdata; }; // ============== MAIN ROUTINE ============ int main() { ListItem *head, *itemptr; char word[20]; bool quit = false; head = NULL; do { printf("Enter a positive integer to insert in the list\n,"); printf("or anything else to quit\n"); scanf("%20s", word); if (atoi(word) <= 0) { quit = true; printf("Goodbye!\n\n"); } else { itemptr = new ListItem(atoi(word), head); head = itemptr; printf("The new list contents are:\n "); head->printlist(); printf("\n\n"); } } while (!quit); return 0; } // ============= STAND-ALONE FUNCTIONS =========== // ============= LISTITEM CLASS ROUTINES ========= ListItem::ListItem() { listdata = 0.0; nextitem = NULL; } ListItem::ListItem(float data, ListItem* next) { listdata = data; nextitem = next; } ListItem::~ListItem() { if (nextitem != NULL) nextitem->~ListItem(); nextitem = NULL; listdata = 0.0; } void ListItem::SetData(float data) { listdata = data; } float ListItem::GetData() { return listdata; } void ListItem::SetNext(ListItem* ptr) { nextitem = ptr; } ListItem *ListItem::GetNext() { return nextitem; } void ListItem::printdata() { printf("%g\n", listdata); } void ListItem::printlist() { printf(" %g", listdata); if (nextitem != NULL) nextitem->printlist(); }