Thread: Transition from Structs to Classes

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Transition from Structs to Classes

    This compiles and runs as intended as it is, but I need for those functions in the commented out "private:" to actually be private. I know I need to pass the data differently, but it has me stumped. If someone could please point(no pun intended) me in the right direction.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string.h>
    
    const int MAXname = 128;
    const int MAXdetail= 4096;
    const int MAXdate = 64;
    
    using namespace std;
    
    class Task
    {
    
        public:
            static int loadData(Task x[], int &y);
            static int saveData(Task x[], int &y);
    
        //private:
            char taskName[MAXname];
            char taskDescription[MAXdetail];
            char taskDate[MAXdate];
    };
    
    class TaskList
    {
        public:
            static int menuControl(Task x[], int &i, char z);
            static void showMenu();
            
        //private:
            static void inputAdd(Task x[], int &y);
            static void findTask(Task x[], int &y);
            static void printTask(Task x[], int &y);
    };
    
    int main()
    {
        Task tmp[MAXname];
        int i = 0;
        char ch = ' ';
        Task::loadData(tmp, i);
        cout << "Welcome to Simple Task Organizer.\n" << endl;
        while(ch != 'q' && ch != 'Q')
        {
            TaskList::showMenu();
            cin.get(ch);
            cin.ignore(MAXname, '\n');
            TaskList::menuControl(tmp, i, ch);
        }
    }
    
    int Task::loadData(Task x[], int &y)
    {
        ifstream in;
        in.open("tasks.txt");
        if(!in)
        {
            cout << "Could not find previously saved file, creating a new one!" << endl << endl;
            return 1;
        }
        while(in)
        {
            in.get(x[y].taskName, MAXname, ';');
            in.ignore(MAXname, ';');
            in.get(x[y].taskDescription, MAXdetail, ';');
            in.ignore(MAXdetail, ';');
            in.get(x[y].taskDate, MAXdate, '\n');
            in.ignore(MAXdate, '\n');
            y++;
        }
    }
    
    int Task::saveData(Task x[], int &y)
    {
        int i = 0;
        
        ofstream in;
        in.open("tasks.txt");
        
        if(!in)
        {
            cout << "Could not save file. Where did it go?" << endl;
            return 1;
        }
    
        for(i = 0; i < (y - 1); i++)
            in << x[i].taskName << ';'
                << x[i].taskDescription << ';'
                << x[i].taskDate << endl;
                
        for(i = i; i < y; i++)
            in << x[i].taskName << ';'
                << x[i].taskDescription << ';'
                << x[i].taskDate;
        
    }
    
    void TaskList::showMenu()
    {
        cout    << "********" << endl
                << "* Menu *" << endl
                << "********" << endl
                << "(a) add a task" << endl
                << "(p) show the task list" << endl
                << "(s) search for a previously entered task" << endl
                << "(q) exits the program" << endl
                << "Please enter an option: ";
    }
    
    int TaskList::menuControl(Task x[], int &y, char z)
    {
        if (z == 'a' || z == 'A') 
        {
            TaskList::inputAdd(x, y);
            Task::saveData(x, y);
        }    
        
        if (z == 'p' || z == 'P')
            TaskList::printTask(x, y);
    
        if (z == 's' || z == 'S')
            TaskList::findTask(x, y);
    
        if (z == 'q' || z == 'Q')
        {
            cout << endl << "Thank you for using the Simple Task Organizer" << endl;
            return 0;
        }
    }
    
    void TaskList::inputAdd(Task x[], int &y)
    {
        char a[MAXname];
        char b[MAXdetail];
        char c[MAXdate];
        char d;
        
        cout << "Please enter the name of the task: ";
        cin.get(a, MAXname, '\n');
        cin.get(d);
        
        cout << "Please enter the description of the task: ";
        cin.get(b, MAXdetail, '\n');
        cin.get(d);
        
        cout << "Please enter the due date of the task: ";
        cin.get(c, MAXdate, '\n');
        cin.get(d);
        
        strcpy(x[y].taskName, a);
        strcpy(x[y].taskDescription, b);
        strcpy(x[y].taskDate, c);
        
        y++;
    }
    
    void TaskList::findTask(Task x[], int &y)
    {
        int a = 0;
        char b[MAXname];
        char c;
        
        cout << "Enter the name of the task to search for: ";
        cin.get (b, MAXname, '\n');
        cin.get (c);
        
        for(int i = 0; i < y; i++)
        {
            if (strcmp (x[i].taskName, b) == 0)
            {
                cout << endl << "********************************"
                        << endl << "* Task Planner ( Found Entry!) *" 
                        << endl << "********************************" 
                        << endl
                        << left << x[i].taskName << endl
                        << left << x[i].taskDescription << endl
                        << left << x[i].taskDate << endl << endl;
                a = 1;
            }
        }
        
        if (a == 0)
            cout    << endl << "************************************"
                    << endl << "* Task Planner (No Results Found!) *" 
                    << endl << "************************************\n*\n*\n*"
                    << endl;
    }
    
    void TaskList::printTask(Task x[], int &y)
    {
        cout << endl << "****************"
                << endl << "* Task Planner *" 
                << endl << "****************" 
                << endl;
        
        for(int i = 0; i < y; i++)
            cout << left << x[i].taskName << endl
                    << left << x[i].taskDescription << endl
                    << left << x[i].taskDate << endl << endl;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Make the data members private and any member functions that manipulate those data members public. Ensure the member functions do any error checking required, rather than blithely making changes that leave class members in an invalid state.

    Don't pass arguments to functions by (non-const) reference unless you intend the function to change that argument - pass by value instead, which has the effect of passing a copy of the supplied argument to the function. If you want to pass by reference (eg because the argument is a large data structure, so creating a copy is expensive) but not change the argument at all, then pass by const reference.

    The above are initial tips to get you started, not laws of nature. If you can't justify doing things differently than I have suggested, then don't. However, as you get more experience you will find circumstances where the tips don't apply - and find justifications for doing things differently.
    Right 98% of the time, and don't care about the other 3%.

    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.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you want to grant TaskList access to (what would be private) members of Task, you can do that. That is the purpose of the friend keyword.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes vs Structs
    By afesheir in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2011, 01:52 PM
  2. structs vs classes
    By _Mike in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2010, 11:39 AM
  3. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  4. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  5. Classes or Structs?
    By DeanDemon in forum C++ Programming
    Replies: 12
    Last Post: 12-05-2002, 03:58 AM