Thread: Can someone help with this (classes)

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

    Can someone help with this (classes)

    I want this to work


    Code:
    class A 
    {
        int basedata;
    };
    
    class B 
    {
        int otherdata;
    };

    now if it included an instance of class A inside class B.

    Code:
    class B 
    {
        int otherdata;
        A base;
    };
    then i could do this to get that base data variable.

    Code:
    B.base.basedata = 0;
    What I want to do is, do something to the B class so this statement will work. What do i need to do to make B have the members of A?

    Code:
    B.basedata = 0;

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Nothing wrong with what you did. You just made everything private. Make them public if you want to get to them externally.
    Code:
    class A 
    {
    public:
        int basedata;
    };
    
    class B 
    {
    public:
        int otherdata;
        A base;
    };
    edit: well one problem:

    B.base.basedata = 0; // no

    B b;
    b.base.basedata = 0; // yes
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    wait, never mind. I misunderstood.
    Code:
    class A 
    {
    public:
        int basedata;
    };
    
    class B : public A
    {
    public:
        int otherdata;
    };
    
    int main(void)
    {
       B b;
       b.basedata = 0;
    }
    This is inheritance. You can make class B a derived class of A
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Thanks man, now another question.

    will this work?

    Code:
    class A 
    {
    public:
    void use();
    };
    
    class B : public A {
    };
    
    class C : public A {
    };
    
    
    B::use()
    {
        //code here
    }
    C::use() 
    {
        //different code here
    }

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    no, not like that but depending on what you're attempting there are ways you can.

    virtual functions:
    Code:
    class A
    {
    public:
       virtual void use()=0;
    };
    
    class B : public A
    {
    public:
       void use()
       {
          //Do something
       }
    };
    
    class C : public A
    {
    public:
       void use()
       {
          //Do something else
       }
    
    };
    This allows you to call the one you intend from an "A" pointer. But this may not be what you're trying to do. Can you be more detailed instead of just asking "Will this work?"
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Actually, I believe what you want to do would work. It would simply hide class A's function...unfortunately it would also hide any methods in class A with the same name...
    Code:
    void A::use() 
    {
    ...
    }
    
    void A::use(int num) const
    {
    ...
    }
    
    void B::use()
    {
    ...//hides both methods of A
    }
    But you can do:
    Code:
    int main()
    {
    
    B myB;
    myB.A::use(10);
    }

    But i think it is better to also make the functions in the base class virtual...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Im writing the design doc for a project. It is going to deal with a ton of different classes, each class will have different content, but many function names are going to be the same across different classes. I could simply declare a function with the same name in each seperate class (which is the effect im after), im just curious if the function could be inserted into the class another way to avoid any typo errors and having to type the thing so much. More of a curiosity question than a need question. I just want to know whats possible in laying out how im gonna write this thing.

    So its, different functions, same names, different function definitions.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yes, you can do that. the method inside two different classes can have the same name.

    My example was one of virtual functions. They are functions that you can use to call those different implementations when you don't know exactly which class you have a pointer to.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

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. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM