2 Replies - 426 Views - Last Post: 20 March 2010 - 02:23 PM Rate Topic: -----

#1 Ironsheik1089  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-June 09

how to use virtual member functions in C++

Posted 20 March 2010 - 10:13 AM

I am currently writing a C++ program that has a base class and 2 derived classes. In my base class I need a public virtual member function that will later be changed by my 2 derived classes. I am confused on how to do this.

Do I declare the prototype in my header file and cpp file virtual?

Can a virtual function take parameters like other functions?

some code written so far: Package is my base class, TwoDayPackage is 1 derived class
#ifndef PACKAGE_H
#define PACKAGE_H

#include <iostream>
#include <string>
using namespace std;

class Package
{
public:
    // constructor receives package information and validates
    Package(string, string, string, string, int, string, string, string, string, int,
            int, double = 0.0); 
    // prints the sender's and recipient's addresses on screen as a shipping label
    void printLabel(string, string, string, string, int, string, string, string, string, int); 
    // virtual function, will be defined by derived classes
    virtual double calculateCost();
     

protected:
    string senderName;
    string receiverName;
    string senderAddress;
    string receiverAddress;
    string senderCity;
    string receiverCity;
    string senderState;
    string receiverState;
    int senderZipcode;
    int receiverZipcode;
    int packageWeight;
    double ounceCost;
}; // end class
#endif

#ifndef TWODAYPACKAGE_H
#define TWODAYPACKAGE_H

#include <iostream>
#include <string>
#include "Package.h" // Package class declaration
using namespace std;

class TwoDayPackage : public Package // will inherit functionallity of base class Package
{ 
public:
    // constructor
    TwoDayPackage(string, string, string, string, int, string, string, string, string, int,
                  int, double = 0.0, double = 0.0); 
    // calculate the cost of two day shipping
    double calculateCost();  
private:
    double flatFee; // flat rate for two day delivery service
}; // end class
#endif 



Is This A Good Question/Topic? 0
  • +

Replies To: how to use virtual member functions in C++

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: how to use virtual member functions in C++

Posted 20 March 2010 - 02:02 PM

Your code is correct, and yes virtual functions can take parameters. Just remember that the parameters and return value in the derived classes have to match exactly what is specified in the base class.
Was This Post Helpful? 0
  • +
  • -

#3 mustermeister  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 11
  • Joined: 20-March 10

Re: how to use virtual member functions in C++

Posted 20 March 2010 - 02:23 PM

Yes, your code is correct. But it sounds like the base class isn't going to do anything in the virtual function. If so, declare the virtual function pure:

virtual double calculateCost() = 0;

That makes the base class abstract and you can't instantiate it. If the function is not pure you must define it in the base class.

The only thing special about virtual functions is the way they bind: they have run time binding to the correct descendent class function. If x is a descendent object the function of that class will be called even if you declare x as a base object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1