Thread: A problem in inheritance in C++

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    A problem in inheritance in C++

    I have a little stupid question, hope some will help

    okay, let's say like this. I have a template class, eg. class Human, therefore it can not be modified.

    Ok, now I write a class, eg. class Female, inherit from class Human.

    Now the problem is, if i have a function return a Human value, and I want to assign the value to Female, this is typically impossible.

    For example, like this

    Code:
    class Human
    {
    	public Human getHuman()
           {
    		return aDummyHuman;
    	}
    }
    
    class Female : Human{
    
    	private Item handBag;  	
    
    }
    And if I have a code like this

    Code:
    objFemale1 = functionReturnHumanValue("Blah Blah Blah");
    Probably I will get an error like
    error C2440: '=' : cannot convert from 'Human' to 'Female'

    So what should I do in this stage, to get objFemale1 get all the value of Human properties, plus for handBag, eg. = NULL?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TnTonly
    Ok, now I write a class, eg. class Female, inherit from class Human.
    Note that you are using private inheritance. You probably want to use public inheritance here, and also provide Human with a virtual destructor.

    Quote Originally Posted by TnTonly
    Now the problem is, if i have a function return a Human value, and I want to assign the value to Female, this is typically impossible.
    It looks like Human should be an abstract base class, so you would not be returning a Human object anyway.

    Quote Originally Posted by TnTonly
    So what should I do in this stage, to get objFemale1 get all the value of Human properties, plus for handBag, eg. = NULL?
    What does it mean to convert a generic human to a female? It sounds like you have something fundamentally wrong with your class design.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by TnTonly View Post
    I have a template class, eg. class Human, therefore it can not be modified.
    The Human class you posted is not a template, it's just a regular class.
    Also, why can't it be modified?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Polymorphism works the other way around - derived classes are also base classes. Also note that you still can't assign a Female to a Human. When using polymorphism, you must work with either pointers or references. It's fine to assign a Female* to a Human*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance problem
    By logicwonder in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2006, 10:14 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 09:27 AM
  4. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM