Thread: help in inheritance classes

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    help in inheritance classes

    Hi i am creating a project that will be inherited a class point, squere and cube all together i did all this and it's in the zip file but i wonted to know what i am putting wrong in main and why when i run the project it gives me address of the memory

    I really need help in the main
    C++ Is cool

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    File is corrupted.

    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    here is again the file
    C++ Is cool

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Ok, so what do you need help on? It compiles and runs for me. What is working correctly?

    gg

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    Red face

    I know that after you run this program it compiles and it works but my question is why i am getting this in the ouptut

    Squere s(via * pointPtr):0x0012FF64// Her i don't know why iam getting the address of memory

    and aslo its sayis in the output
    perimeter :6.98771e039 // what's wrong with this

    //and i wonted to make sure if this is corret that what's also my question

    thanks
    C++ Is cool

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    A pointer is being printed because that is exactly what you are doing, for example:
    Code:
    int n = 5;
    int *p = &n;
    ...
    cout << p << endl;   //prints the address of n
    cout << *p << endl;  //prints "5"
    Your other problem is that you are trying to cast up inheritance that wasn't previously cast down, for example:
    Code:
    class base {};
    class child : public base {};
    ...
    child c;
    base *pb = static_cast<base*>(&c);    //cast down, ok
    child *pc = static_cast<child*>(pb);  //cast back up, ok since pb points to type child
    ...
    base b;
    pb = &b; //pb now points to type base
    pc = static_cast<child*>(pb); //cast up, but pb currently points to type base - this is garbage, results undefined
    Once you undestand that, what is wrong with this?
    Code:
    pointPtr = &p;
    squarePtr = static_cast<Square *>(pointPtr); //cast up!!!!
    gg

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    Thanks a lot Codeplug
    for explaining to me what was going on that code i am not using pointer at all now and it's working perfect
    You are the Best Codeplug
    C++ Is cool

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Codeplug

    Code:
    child *pc = static_cast<child*>(pb);  //cast back up, ok since pb points to type child
    when casting up you should use dynamic_cast not static_cast

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining classes following user input. Inheritance.
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2009, 09:21 AM
  2. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01:45 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. Inheritance Classes w/ same function name
    By Diamonds in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2003, 01:11 AM
  5. Classes & Inheritance
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2002, 06:25 PM