class constructor help needed

This is a discussion on class constructor help needed within the C++ Programming forums, part of the General Programming Boards category; I am trying to get the hang of classes and I am using football management as the context. I have ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    class constructor help needed

    I am trying to get the hang of classes and I am using football management as the context.

    I have the following code so for

    Code:
    // classes again
    
    #include<iostream>
    #include<string.h>
    using namespace std;
    
    class Footballer
    {
    public:
        Footballer(char* name, int age, int passing, int shooting, int tackling);
        ~Footballer();
    
    private:
        char _name[256];
        int _age;
        int _passing;
        int _shooting;
        int _tackling;
    };
    
    Footballer::Footballer(char* name, int age, int passing, int shooting, int tackling)
    {
        strcpy(_name,name);
        _age=age;
        _passing=passing;
        _shooting=shooting;
        _tackling=tackling;
    }
    
    Footballer::~Footballer()
    {
    }
    
    int main(void)
    {
    
        Footballer Mark("Mark Sandles",26,5,2,8);
    
        return(0);
    }
    but to get the true representation of a footballer eventually i would want maybe twice as many parameters for the constructor. I am sure this would not be the way to go so can any one give me an insight into how they would approach the problem

    Thanks.

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    why would you need twice as many parameters again???
    and once again what seams to be the problem, b/c i just don't see your question here???


    matheo917

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if you have a complex object it may take twice as many data members. If you want each object to have the potential for unique data member values, then you may want a constructor with 20 parameters or 20 different mutators or whatever. Take a look at some of the structs in winAPI to see some examples of structs using a large number of parameters. If you need a lot of parameters then making a column of parameters is sometimes easier.

    player:layer(int age,
    int height,
    int weight,
    char gender,
    char eyeColor,
    int armReach,
    int yearOfEntry,
    int numberOfYearsInLeague,
    char maritalStatus,
    int numberOfDivorces,
    long salary,
    char * agentsName[80],
    char * cityOfBirth[80],
    char teamsPlayedOn[12][20],
    long largestFine,
    int maxBenchPressed,
    int whatever)
    {
    //etc.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    so basicly if a class needs 20 parameters then so be it.

    I just thought that either my program design would be at fault to use that many parameters and that there would be an alternative.

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can use default parameters where a parameter is often the same value, but you don't want to assume it will be the same value in every case.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    KingoftheWorld
    Guest
    Originally posted by elad
    if you have a complex object it may take twice as many data members. If you want each object to have the potential for unique data member values, then you may want a constructor with 20 parameters or 20 different mutators or whatever. Take a look at some of the structs in winAPI to see some examples of structs using a large number of parameters. If you need a lot of parameters then making a column of parameters is sometimes easier.

    player:layer(int age,
    int height,
    int weight,
    char gender,
    char eyeColor,
    int armReach,
    int yearOfEntry,
    int numberOfYearsInLeague,
    char maritalStatus,
    int numberOfDivorces,
    long salary,
    char * agentsName[80],
    char * cityOfBirth[80],
    char teamsPlayedOn[12][20],
    long largestFine,
    int maxBenchPressed,
    int whatever)
    {
    //etc.
    You can redesign this big class with divide it into subclasses( small component). Use abstract class as base class and you can
    use inheritance technique to incorporate all classes to together.
    By doing this you can eliminate a class with a bundle of data members.
    For example:
    Code:
    class person
    {
        // only specific attributes of a person(human)
       // weigh, height, gender....
    
    }
    class footballplayer::person
    {
       //add additional attributes for a specific football player;
       //score, numoftouchdown.............
    } 
    class personal_information
    {
    // married status, salary....
    
    }
    and so on...
    Hope you go an idea

    KingoftheWorld

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    You could also define a struct in the .h that takes all those parameters and the constructor can just take one of those. Also, thing that uses it must include the .h file and therefore the struct:

    Code:
    #include <iostream>
    using namespace std;
    
    // --------------------------
    //	Footballer.h
    // --------------------------
    typedef struct _structPLAYER
    {
    	int age;
    	int height;
    	int weight;
    	char gender;
    	char eyeColor;
    	int armReach;
    	int yearOfEntry;
    	int numberOfYearsInLeague;
    	char maritalStatus;
    	int numberOfDivorces;
    	long salary;
    	char * agentsName;
    	char * cityOfBirth;
    	char teamsPlayedOn[12][20];
    	long largestFine;
    	int maxBenchPressed;
    	int whatever;
    } PLAYER;
    
    class Footballer
    {
    public:
    	Footballer();
    	Footballer(PLAYER player);
    	// overridables
    	virtual ~Footballer();
    
    protected:
    	PLAYER playr;
    };
    
    //---------------------------
    //	Footballer.cpp
    //---------------------------
    Footballer::Footballer()
    	{}
    Footballer::Footballer(PLAYER player) : playr(player)
    	{}
    Footballer::~Footballer()
    	{}
    
    //------------------
    // Main
    //------------------
    #include "Footballer.h"
    
    int main()
    {
    	PLAYER p;
    	p.age = 10;
    	p.agentsName = "Test";
    	p.armReach = 12;
    	p.cityOfBirth = "Stockholm";
    	//etc
    	Footballer foot = Footballer(p);
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    when you do inheritance, shouldn;t you deploy of thoseulic friend private......?

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if you use inheritance then you have to be sure you get everything set up correctly and you still need to initialize each of the individual variables contained within each of the subclasses. Therefore, you don't save anything in terms of input, you create a complex heirachy of classes you need to keep track of, etc. I won't argue, you could reuse some of the classes in other projects, etc. --some day-- and in that regard (maybe) save time, but you introduce other potential problems, too. Either way works, though, so go with what you feel most comfortable as you are working on the project. Down the road, you or your employer may want to change tactics however.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 08:57 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21