CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Struct at the inside of class

    Hello,

    while writing code i got a question. For example i created a class named as unit.

    Think a simple game, and the unit class will belong the units.

    İf i give the preferences as private one by one, it will be irregular. For a detailed game; height, weight, race, hair preferences, eyes... Strength, dexterity, charisma, intelligence, wisdom, constution... experience, damage, armor...
    and should i use struct to group them? And how to can i use struct at the inside of class as private? Can u give me examples.
    Thank you.

  2. #2
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Struct at the inside of class

    Hi

    Code:
    class CAtributes {
    	public: 
    	CAtributes();
    	~CAtributes();
    	int age, height, weight;	
    };
    
    class CPerson {
    	private: 
    	CAtributes atributes;
    	public:
    	CPerson();
    	~CPerson();
    	int get_age {
    		return atributes.age;
    	}
    	int get_height {
    		return atributes.height;
    	}
    	int get_weight {
    		return atributes.weight;
    	}
    	void set_age(int age) {
    		atributes.age = age;
    	}
    	void set_height(int height) {
    		atributes.height = height;
    	}
    	void set_weight(int weight) {
    		atributes.weight = weight;
    	}
    };
    Best regards.


    EDIT: you can use, struct unless class for atributes ...
    Last edited by juanpast; June 24th, 2013 at 05:44 AM.

  3. #3
    Join Date
    Jun 2013
    Posts
    3

    Re: Struct at the inside of class

    Thank you, this is helpfull and simple




    Quote Originally Posted by juanpast View Post
    Hi

    Code:
    class CAtributes {
    	public: 
    	CAtributes();
    	~CAtributes();
    	int age, height, weight;	
    };
    
    class CPerson {
    	private: 
    	CAtributes atributes;
    	public:
    	CPerson();
    	~CPerson();
    	int get_age {
    		return atributes.age;
    	}
    	int get_height {
    		return atributes.height;
    	}
    	int get_weight {
    		return atributes.weight;
    	}
    	void set_age(int age) {
    		atributes.age = age;
    	}
    	void set_height(int height) {
    		atributes.height = height;
    	}
    	void set_weight(int weight) {
    		atributes.weight = weight;
    	}
    };
    Best regards.


    EDIT: you can use, struct unless class for atributes ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured