Thread: 'Hello World' of Inheritance (need help)

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    30

    'Hello World' of Inheritance (need help)

    Hello,
    I need to understand the basic syntax of C++ and object oriented design including inheritance. So I'm trying to compile a sort of template to aid my understanding. This code has multiple problems, and I hardly know where to start with all the errors. I was also perplexed when I thought of creating an instance of the most derived object (the truck class) with all the qualities of the base classes; this particular point-of-confusion is that each constructor (as I wrote them) does not set the instance variables for the upper base classes.

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    
    class Automobile{
    	protected:	/* protected means only derived classes can use directly */
    		int a;
    		int b;
    	public:
    		Automobile(int a_, int b_){
    			a = a_;
    			b = b_;
    		}
    };
    
    Class Truck : Automobile{
    	protected:
    		int c;
    		int d;
    	public:
    		Truck(int c_, int d_){
    			c = c_;
    			d = d_;
    		}
    };
    	
    Class PickUp : Truck{
    	private:
    		int e;
    		int f;
    	public:
    		PickUp(int e_, int f_){
    			e = e_;
    			f = f_;
    		}
    };
    
    PickUp::print(){
    	printf("%d %d %d %d %d %d", a,b,c,d,e,f);
    }
    		
    int main(){
    	PickUp x(?????);
    	//I don't understand how to declare a new PickUp object with a,b,c,d,e,f
    	
    	x.print();
    }
    Thanks in advance.
    Last edited by Roger; 12-02-2009 at 08:42 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you want a constructor to take 6 arguments, then you'll need to declare with that many arguments. Then, using the initialization list, you can forward arguments to the parent's constructor.

    There are also other errors, like needing to use public for normal "is-a" inheritance (or the protected members of the grand-parent class will still be unavailable.

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    
    class Automobile
    {
        protected:	/* protected means only derived classes can directly use */
            int a;
            int b;
        public:
            Automobile(int a_, int b_)
            {
                a = a_;
                b = b_;
            }
    };
    
    class Truck : public Automobile  //use public inheritance, or Automobile's a and b won't be accessible to classes derived from this
    {
        protected:
            int c;
            int d;
        public:
            Truck(int a_, int b_, int c_, int d_):
                Automobile(a_, b_) //invoke parent constructor with arguments
            {
                c = c_;
                d = d_;
            }
    };
    
    class PickUp : public Truck  //use public inheritance
    {
        private:
            int e;
            int f;
        public:
            PickUp(int a_, int b_, int c_, int d_, int e_, int f_):
                Truck(a_, b_, c_, d_) //invoke its constructor
            {
                e = e_;
                f = f_;
            }
    
            void print(); //needs to be declared
    };
    
    void PickUp::print() //has return type
    {
    	printf("%d %d %d %d %d %d", a,b,c,d,e,f);
    }
    
    int main()
    {
        PickUp x(1, 2, 3, 4, 5, 6);
        x.print();
    }
    Last edited by anon; 12-02-2009 at 08:53 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    30
    Great! Thanks anon, that makes perfect sense too.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The print member function should also be const since it does not modify the object:
    Code:
    class PickUp : public Truck  //use public inheritance
    {
    ...
    void print() const;
    };
    
    void Pickup::print() const
    {
        printf("%d %d %d %d %d %d", a,b,c,d,e,f);
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM
  5. No More Technology After World War Three
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-20-2001, 07:02 PM

Tags for this Thread