Thread: Value of data members

  1. #1
    Unregistered
    Guest

    Question Value of data members

    I'm not sure what I am doing!?
    I am trying to understand inheritance and want to see what values get passed by using my main().

    I'm not too sure if my argument constructors are correct.

    #include <iostream.h>

    class Class_A
    {
    private:
    int A_private;
    protected:
    int A_protected;
    public:
    Class_A(int, int);
    void A_Pub_Func();
    };

    class Class_B : public Class_A
    {
    private:
    int B_private;
    protected:
    int B_protected;
    public:
    Class_B(int, int);
    void B_Pub_Func();
    };

    class Class_C : private Class_A
    {
    private:
    int C_private;
    protected:
    int C_protected;
    public:
    Class_C(int, int);
    void C_Pub_Func();
    };

    class Class_D : public Class_B
    {
    private:
    int D_private;
    protected:
    int D_protected;
    public:
    Class_D(int, int);
    void D_Pub_Func();
    };

    class Class_E : public Class_C
    {
    private:
    int E_private;
    protected:
    int E_protected;
    public:
    Class_E(int, int);
    void E_Pub_Func();
    };

    Class_A::Class_A(int a, int b) {
    A_private = a;
    A_protected = b;
    }

    Class_B::Class_B(int a, int b) {
    B_private = a;
    B_protected = b;
    }

    Class_C::Class_C(int a, int b) {
    C_private = a;
    C_protected = b;
    }

    Class_D::Class_D(int a, int b) {
    D_private = a;
    D_protected = b;
    }

    Class_E::Class_E(int a, int b) {
    E_private = a;
    E_protected = b;
    }

    void main()
    {
    Class_A a_object(1,1);
    Class_B b_object(2,2);
    Class_C c_object(3,3);
    Class_D d_object(4,4);
    Class_E e_object(5,5);
    }

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    If Class_A is a superclass of Class_B, then you should write B:s constructor like this:
    Code:
    Class_B::Class_B(int a, int b) : Class_A(a,b) { 
      B_private = a; 
      B_protected = b; 
    }
    ..because you have no default constructor in Class_A.
    Do this to all inherited classes.

    Btw, you should write int main () instead of void main(). This is the standard way to do it. Find more with google.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. member functions can't see private data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2006, 02:36 PM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM