A problem in inheritance in C++

This is a discussion on A problem in inheritance in C++ within the C++ Programming forums, part of the General Programming Boards category; I have a little stupid question, hope some will help okay, let's say like this. I have a template class, ...

  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
    21,662
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,555
    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, 05:24 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 08:27 AM
  4. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 10:14 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

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