Thread: Advice on Class with Stuct

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    Advice on Class with Stuct

    Hi,
    I'm converting this golf program into a prog using a class. I want to use the old structure in the class but am not sure how to use the structure or functions of the class. Here is what I have.
    Code:
    //golff.h	for prblm 2 pg 417 c++ primer plus
    
    class Score  // golf score class
    {
        private:
        const int Len=40;
        struct golf
        {
    	    char fullname[Len];
    	    int handicap;
        };
        public:
            void score(golf & g, const char *name, int hc);
            int setgolf(golf & g);
            void setgolf(golf & g, const char *name, int hc);
            void handicap(golf & g, int hc);
            void showgolf(const golf & g);
    };
    Second file:
    Code:
    #include <iostream>
    #include "golfh.cpp"
    using namespace std;
    
    void Score::Score(golf & g, const char *name, int hc)// constructor
    {
    	strcpy(g.fullname,name);
    	g.handicap=hc;
    }
    
    int Score::setgolf(golf & g)
    {
    	cout << "Enter golfers name:";
    	cin.get(g.fullname,Len);
    	if (strlen(g.fullname)==0)
    	{
    		return 0;
    	}
    	else
    	{
    		cout << "Enter player's handicap:";
    		cin >> g.handicap;
    		cin.get();
    		return 1;
    	}
    
    }
    
    void Score::handicap(golf & g, int hc)
    {
    	g.handicap=hc;
    }
    void Score::showgolf(const golf & g)
    {
    	cout << "Player's name:"<<g.fullname<<endl;
    	cout << "Handicap:"<<g.handicap<<endl;
    	cout << endl;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    you should take the struct out

    i'd advise working from this point (although i may not be clear on what you're trying to do)

    class Score // golf score class
    {
    private:
    const int Len=40;
    char fullname[Len];
    int handicap;
    public:
    void score(golf & g, const char *name, int hc);
    int setgolf(golf & g);
    void setgolf(golf & g, const char *name, int hc);
    void handicap(golf & g, int hc);
    void showgolf(const golf & g);
    };

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    struct in a class

    Yes that would be much easier it seems. I just thought I would leave the original structure intact in order to learn how to use the structure in the class. I'm book taught and the book hasn't attempted to use a struct in a class yet so I am forging new ground. I can see that the struct has class scope so passing s of a struct to the member function would mean making another structure outside the class which of course would be a waste. Since the struct is private to the class I could not create an object of that struct type in main, but could I create an object of that class and pass the struct within to the member function somehow? I don't know this is a bit confusing. I guess what I am asking is how do you use a struct in a class definition and how do you access the members of the struct that is part of the class. Could I create an object say folf of type golf in the class definition(say the constructor) then remove the passing by refrence of the struct from the member functions. In main then I wold create an array holding golf objects and use the member functions to alter the properties of the individual objects.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    you could define the struct outside the class and have a member of the class be of your struct type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM