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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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