classes and structures

This is a discussion on classes and structures within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering how a person could use a structure and have classes access it. (I'm not sure if ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    classes and structures

    Hi, I was wondering how a person could use a structure and have classes access it. (I'm not sure if I'm using the correct terminology or not.) Would the struct go in the public: portion of the base class? Does it go outside of the class? Could somebody try to explain how this would work? Thanks for any help on this matter! mcorn
    Code:
    struct mystruct
    {
         char name[20];
         int age;
    };
    
    class cBaseClass
    {
       private:
               int  whatever;
    
       public:
           cBase();
           int whatever();   Access the struct somehow?
    };
    
    class cChildClass
    { 
        private:
               etc
        public:
               cChildClass: public cBase : access the struct somehow?
    };

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Lookee Here

    You don't need inheritance for that, besides, your syntax is very far off. Do:

    Code:
    struct mystruct
    {
        char name[20];
        int age;
    };
    
    class SomeClass
    {
    public:
        int GetAge() const;
    protected:
        mystruct StructInstance;
    };
    
    int SomeClass::GetAge() const
    {
        return StructInstance.age;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about classes and structures.
    By RealityFusion in forum C++ Programming
    Replies: 19
    Last Post: 08-30-2005, 04:54 PM
  2. Replies: 1
    Last Post: 06-17-2005, 08:31 AM
  3. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 05:03 PM
  4. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  5. structures fooloing to look like classes
    By samsam1 in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2003, 11:43 AM

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