Thread: Classes

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question Classes

    Hi there,

    I have a class within a class. How do I create an object of the sub class and then use it.

    eg)
    Code:
    class setA
    {
    protected:	
    	int Weight;
    	string Name;
    
    	void SetWeight(int Weight_in) { Weight = Weight_in; }
    	void SetName(string Name_in) { Name = Name_in; }
    
    public:
    	class part1
    	{
    	protected:
    		int DescentRate;
    		double MaxHeight;
    
    		void SetDescentRate(int DescentRate_in) { DescentRate = DescentRate_in; }
    		void SetMaxHeight(double MaxHeight_in) { MaxHeight = MaxHeight_in; }
    	};
    };

    How could I access and hence use setdescentrate() in the main???

    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    With the scope resolution operator, "setA::part1" is the full type name.

    gg

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by strokebow View Post
    Hi there,

    I have a class within a class. How do I create an object of the sub class and then use it.

    eg)
    Code:
    class setA
    {
    protected:	
    	int Weight;
    	string Name;
    
    	void SetWeight(int Weight_in) { Weight = Weight_in; }
    	void SetName(string Name_in) { Name = Name_in; }
    
    public:
    	class part1
    	{
    	protected:
    		int DescentRate;
    		double MaxHeight;
    
    		void SetDescentRate(int DescentRate_in) { DescentRate = DescentRate_in; }
    		void SetMaxHeight(double MaxHeight_in) { MaxHeight = MaxHeight_in; }
    	};
    };

    How could I access and hence use setdescentrate() in the main???

    Thanks

    Thanks for replying

    I thought that but it didnt and still doesnt work

    Code:
    void main(void)
    {
    
    	SetA::part1  x;
    
    	x.SetDescentRate(12);
    
    	
    
    }
    Gives an error.

    error C2248: 'SetA:art1::SetDescentRate' : cannot access protected member declared in class 'SetA:art1'

    Can anyhone explain? any ideas? explanations?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Well, you can hardly expect to make something "protected" and then access it from a public context...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    ha! sorry yeah

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As the error clearly outlines, the problem was not with creating an instance of the class, but calling a protected member function of that instance.
    You should learn to interpret error messages better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM