Thread: virtual function in base class

  1. #1
    Registered User QueSue's Avatar
    Join Date
    Jan 2003
    Posts
    7

    Unhappy virtual function in base class

    [COLOR=indigo][B]
    Can some one PLEASE post and example for :

    virtual getresults() in the base class

    where to define: results = 3.14 * dRadius * dRadius
    return results;

    and how to call it in derived classes

    Susan


    Education Direct Student Center
    Last edited by QueSue; 01-25-2003 at 11:12 AM.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Code:
    #include <iostream>
    
    using namespace std;
    
    class base {
    protected:
      double dRadius;
    public:
      explicit base(int init)
        : dRadius(init)
      {}
    
      virtual ~base() {};
      virtual double getresults() const = 0;
    };
    
    class derived: public base {
    public:
      explicit derived(int init)
        : base(init)
      {}
    
      double getresults() const;
    };
    
    double derived::getresults() const
    {
      return 3.14 * dRadius * dRadius;
    }
    
    int main()
    {
      derived d(10);
    
      cout<< d.getresults() <<endl;
    }
    *Cela*

  3. #3
    Registered User QueSue's Avatar
    Join Date
    Jan 2003
    Posts
    7
    [b]
    [COLOR=indigo]

    Thanks but that's about what I have here and it's loaded with errors

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Thanks but that's about what I have here and it's loaded with errors
    Then you should show us what the errors are and tell us what compiler you use because it compiles and runs perfectly for me.
    *Cela*

  5. #5
    Registered User QueSue's Avatar
    Join Date
    Jan 2003
    Posts
    7
    [B][COLOR=indigo]
    Thank you for responding to my post regardless if problem is solved


    This is far as I can get with out errors:

    Shape base class:

    #if!defined(SHAPE_H)
    #define SHAPE_H
    class Shape {
    public:
    Shape();
    virtual double getArea() const = 0;

    protected:
    double dArea;
    };
    #endif


    Circle derived class:

    #if!defined(CIRCLE_H)
    #define CIRCLE_H
    #include "Shape.h"
    class Circle: public Shape {
    public:
    Circle();
    double setRadius();
    double getRadius();
    double getArea();
    private:
    double dRadius;
    };
    #endif

    Circle .ccp:

    #include "Circle.h"
    Circle::Circle(){
    }

    double Circle::getArea(){
    return 3.14 * dRadius * dRadius;
    }

    Main Source file:

    #include<iostream.h>
    #include "Circle.h"


    void main(){


    }

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    There are a few problems with that code. That is not like the code cela gave you. Anyways lets see..

    You never define the constructor for Shape class, your constructor doesn't take any parameters so the dArea member will never be set. This also goes for you Circle class, you never have a constructor that can set the radius. Try making some changes and looking at cela's code again.

  7. #7
    Registered User QueSue's Avatar
    Join Date
    Jan 2003
    Posts
    7
    I just need to add on not change. The requirement of this project is to bulid on the data and format given by instuctor. Thanks just the same.

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    try adding another constructor that takes a parameter to set the radius OR add a "mutator" function to access the radius and set it before you try to calculate the area. you cant find the area if the radius is not set to any value.

  9. #9
    Registered User QueSue's Avatar
    Join Date
    Jan 2003
    Posts
    7
    People I get it You don't know how or can't post a sample of

    base class with virtual getresults


    derived class that can call it


    NO PROBLEM

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    People I get it You don't know how or can't post a sample of
    Someone did post it, if you're not smart enough to understand the code you've been given, that's not our problem. Cela's code does exactly what you asked, it has a virtual base function and a derived class calls it. What more do you want?? I mean geez, if you ask for help and then completely ignore the help you're given why even waste our time?

  11. #11
    Registered User QueSue's Avatar
    Join Date
    Jan 2003
    Posts
    7
    Lighten up Crimpy didn't see YOU post anything



    I did say Thanks anyway !!!

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Lighten up Crimpy didn't see YOU post anything
    cuz I wasn't here genius. The only reason I decided to post was I was reading what looked like an interesting thread and then I saw you insult everyone who was nice enough to try and help you.
    I did say Thanks anyway !!!
    No, you said "Oh, despite the fact that everyone posted perfectly knowledgeable, correct, and workable answers, I'll just assume that they don't know how to do it because I don't understand. Thanks anyway!!!" Moron.

  13. #13
    Registered User QueSue's Avatar
    Join Date
    Jan 2003
    Posts
    7
    NO THAT"S WHAT YOU READ !!!!!!!!

    If in fact I did insulted anyone other then crimpy I am truly sorry that wasn’t my intention at all.
    I did count three Thanks and I meant it CELA, MRWIZARD, and PERSPECTIVE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 03:14 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM