Inhertince simple question.

This is a discussion on Inhertince simple question. within the C++ Programming forums, part of the General Programming Boards category; Hi I wanted to know one thing about inhertiance (which seems to be the most important one ). Lets say ...

  1. #1
    DeSeis
    Guest

    Inhertince simple question.

    Hi
    I wanted to know one thing about inhertiance (which seems to be the most important one ).
    Lets say we define a base class:

    class A {

    private:
    int i;

    public:
    void check(void);

    }
    then a Child class inherited from B;

    class B : public A
    {
    private:
    int j;

    public:
    Void Show(void);
    }

    Now I read the tutorials and read a lame book but i still dun undersntad if B inherits A variables and functions how can i use them?
    does B has an int i ? does it have check function?
    so i call it like this ? -->>> B::check(); ?

    Please Explain
    Thanks

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Hope this explains a little:

    Code:
    #include <iostream.h>
    
    class A
    {
    public:
    	int a;
    
    	int get_a (void) 
    	{
    		return a;
    	}
    
    	void set_a (int na) 
    	{
    		a = na;
    	}
    };
    
    class B : public A
    {
    public:
    	int b;
    	
    	int get_b (void) 
    	{
    		return b;
    	}
    
    	void set_b (int nb) 
    	{
    		b = nb;
    	}
    };
    
    
    int main (void)
    {
    	class B object;
    	int var;           
                    
    	object.set_a (2);
    	var = object.get_a ();
    
    	cout << var << endl;
    
    	return 0;
    }
    If you have any questions....

  3. #3
    DeSeis
    Guest
    from ur example if a is public u could also do:
    object.a=5; to change the value of a;

    my question is if class B got its OWN a?! or are we changing the class A's int a?

    if B inherits its function is it like copy pasting the functions and variables code!?

    thx

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >object.a=5

    That's correct. If the member a of class A was private, the we must use the interface functions since then we cannot access member a directly.

    >my question is if class B got its OWN a?! or are we changing the >class A's int a?

    It is class A's member variable a.

    >if B inherits its function is it like copy pasting the functions and >variables code!?

    If the member functions and variables of class A were all public, yes. But if you put member functions and member variables of class A in the private part, then B has no access to them. Unless class B is friend of A.

    Code:
    class A
    {
    	friend class B;
    
    private:
    	int a;
    
    public:
    	int get_a (void) 
    	{
    		return a;
    	}
    
    	void set_a (int na) 
    	{
    		a = na;
    	}
    };
    
    class B : public A
    {
    public:
    	int b;
    	
    	int get_b (void) 
    	{
    		return b;
    	}
    
    	void set_b (int nb) 
    	{
    		b = nb;
    	}
    
    	int get_a_from_a (void)
    	{
                                    /* B is friend of A, so B can access private
                                        data of A */
    		return a;
    	}
    };
    Last edited by Shiro; 01-11-2002 at 02:20 PM.

  5. #5
    DeSeis
    Guest
    Thanks bro I think im begining to understand the hang of it.
    If I want to define lets say 10 objects of the same class?
    like
    Class A{...................};

    A object[10];

    then I can call the functions like this?

    for (i=0;i<10;i++)
    object[i].function();

    Thanks
    btw, any tutorials on the net u know of?

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    While we're on the subject: If class A has int I, class B has int I, and class C is multipl-ly (or whatever the adverb would be) inherited from A and B, which one would oberride the other, or would an exception occur or what?!

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Hope this explains a bit:

    Code:
    #include <iostream.h>
    
    class A
    {
    public:
    	int a;
    
    	int get_a (void) 
    	{
    		return a;
    	}
    
    	void set_a (int na) 
    	{
    		a = na;
    	}
    };
    
    class B : public A
    {
    public:
    	int a;
    	int b;
    
    	int get_b (void) 
    	{
    		return b;
    	}
    
    	void set_b (int nb) 
    	{
    		b = nb;
    	}
    };
    
    class C : public B
    {
    };
    
    int main (void)
    {
    	class C CInstance;
    	int var;                           
    	
    	CInstance.set_a (2);
    	CInstance.a = 4;
    	var = CInstance.a;
    	
    	// This will print 2
    	cout << CInstance.get_a () << endl;
    
    	// This will print 4
    	cout << var << endl;
    
    	return 0;
    }
    There are quite a lot C++ tutorials on the internet. A good one is:

    http://www.glenmccl.com/tutor.htm

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>>
    If class A has int I, class B has int I, and class C is multipl-ly (or whatever the adverb would be) inherited from A and B,
    <<<

    Both would have a variable called I. To access one or the other, use the scope resolution operator i.e. A::I or B::I.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    There is one thing that I do not understand. I know that virtual allows the function to be overridden by the inheritted function. However, what would happen if virtual was not included.

    class a
    {public:
    void print(){cout<<"class a";}
    }

    class bublic class a
    {public:
    void print(){cout<<"class b";}
    }


    int main()
    {a instance;
    b otherinstance;
    instance.print();
    otherinstance.print();
    return 0;
    }


    what would happen. Sorry if my syntax is off a little.

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Try it?

    Then remember the scope resolution operators, and try them - you'll learn.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    i am so stupid. I cannot even get the code to work. I never have had to use classes before so I forget the syntax. Please correct the code so that I can find out what would happen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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