Thread: Inheritance Classes w/ same function name

  1. #1
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    Question Inheritance Classes w/ same function name

    I have two classes, 1 base, and 1 the child. The base has a function print, so does the child.

    Base:
    Code:
    class Property {
    public:
    	Property();
    	char *getAddress();
    	void setAddress(char *s);
    	char *getOwner_Name();
    	void setOwner_Name(char *s);
    	char *getOwner_Address();
    	void setOwner_Address(char *s);
    	int getPrice();
    	void setPrice(int p);
    	char *getDate_Purchased();
    	void setDate_Purchased(char *s);
    	int getLot_Area();
    	void setLot_Area(int p);
    	char *getType();
    	void setType(char *s);
    	char *getStatus();
    	void setStatus(char *s);
    	void print();
    
    
    protected:
    	char address[256];
    	char owner_name[256];
    	char owner_address[256];
    	int price;
    	char date_purchased[256];
    	int lot_area; // size of lot i'm guessing
    	char type[256]; //residential or commercial
    	char status[256];
    };
    the child:
    Code:
    class SingleFamilyHome : public Property {
    public:
    	SingleFamilyHome();
    	int getDwelling_Area();
    	void setDwelling_Area(int i);
    	int getNumber_Of_Bed_Rooms();
    	void setNumber_Of_Bed_Rooms(int i);
    	int getNumber_Of_Baths();
    	void setNumber_Of_Baths(int i);
    	void print();
    protected:
    	int dwelling_area;
    	int number_of_bed_rooms;
    	int number_of_baths;
    };
    in my main I've got this:
    Code:
    	SingleFamilyHome h;
    
    	h.setAddress("address");
    	h.setDate_Purchased("date");
    	h.setLot_Area(104);
    	h.setOwner_Address("owneraddy");
    	h.setOwner_Name("ownername");
    	h.setPrice(100);
    	h.setStatus("status");
    	h.setType("type");
    	h.setDwelling_Area(1040);
    	h.setNumber_Of_Bed_Rooms(105);
    	h.setNumber_Of_Baths(10);
    	h.print();
    // need to print data from Property as well
    I need to print the data from the base class Property. How would I access that function. H has access to thsoe functions, but it defaults to the child print function first.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I don't know for certain, but shouldn't something like h.Property::print() work?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    When you define the derived class's function you can do something like this:
    Code:
    void SingleFamilyHome::print()
    {  Property::print();
        // do other stuff now
        ......
    }
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    h.Propertry::print() will call the base class...h.print() will call the derive class? .....i think
    Last edited by nextus; 03-11-2003 at 01:13 AM.
    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM