could someone please clarify the concept of virtual functions?
I've tried reading textbooks and googling and I just get more confused. I have some idea what it is but not enough to make any sense of it. Basically I have to change all of my display functions in my code to virtual functions. Here is my code:
I just need to know what a virtual function is in layman's terms so that I have some idea what to do.
Thank you in advance for your help! :)
//specification file phone.h
#include<iostream>
using namespace std;
#ifndef PHONE
#define PHONE
class Phone
{
public:
Phone(string /*name*/,int /*extnum*/);
Phone();
void displayInfo() const;
string getName()const {return name;}
int getExt() const{return extnum;}
protected:
string name;
int extnum;
};
class OfficeInfo: public Phone
{
public:
OfficeInfo(string /*name*/,int /*extnum*/,int /*officenum*/);
OfficeInfo();
void displayInfo () const;
int getOfficeNum() const {return officenum;}
private:
int officenum;
};
class Email: public Phone
{
public:
Email(string /*name*/,int /*extnum*/,string /*email*/);
Email();
void displayInfo () const;
string getEmail () const {return email;}
private:
string email;
};
#endif
//implementation file phone.cpp
#include "phone.h"
#include <iostream>
Phone::Phone(string newname, int newextnum)
{
name = newname;
extnum = newextnum;
}
OfficeInfo::OfficeInfo(string newname, int newextnum, int newofficenum)
: Phone(newname, newextnum)
{
officenum = newofficenum;
}
Email::Email(string newname, int newextnum, string newemail)
: Phone(newname, newextnum)
{
email = newemail;
}
void Phone::displayInfo() const
{
cout<<"name"<<name<<endl;
cout<<"extenstion"<<extnum<<endl;
}
void OfficeInfo::displayInfo() const
{
Phone::displayInfo();
cout<<"office number"<<officenum;
}
void Email::displayInfo() const
{
Phone::displayInfo();
cout<<"email"<<email;
}
//client code
#include <iostream>
#include "phone.cpp"
using namespace std;
int main()
{
Phone objPhone("john",111);
OfficeInfo objOffice("mary",222,5551111);
Email objEmail("sharon",333,"blah@something.com");
objPhone.displayInfo();
objOffice.displayInfo();
objEmail.displayInfo();
system("pause");
return 0;
}
I just need to know what a virtual function is in layman's terms so that I have some idea what to do.
Thank you in advance for your help! :)
