By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Virtual functions

P: n/a
hi, i am trying to understand virtual functions, both how to create
them and how or when to use them - how they can be used to improve a
program. Below is a very simple program which i quickly made using
information gathered from a book on the subject, however this simply
returns a blank space. please help both on this program and anything
else related to virtual functions.

wilson

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class account
{
public:
int balance, number;
int function() { cout << balance; }
};
class checking : public account
{
public:
int balancechecking;
};
class savings : public account
{
public:
int balancesavings;
};

void display(account& x)
{
x.function();
}

int main()
{
savings a;
checking b;
b.balance = 10;
a.balance = 20;
display(a);
cout << endl;
display(b);
system("PAUSE");
}

Mar 17 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Wilson" writes:
hi, i am trying to understand virtual functions, both how to create
them and how or when to use them - how they can be used to improve a
program. Below is a very simple program which i quickly made using
information gathered from a book on the subject, however this simply
returns a blank space. please help both on this program and anything
else related to virtual functions.

wilson

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class account
{
public:
int balance, number;
int function() { cout << balance; }
};
class checking : public account
{
public:
int balancechecking;
You are off to a bad start. The balance should be a member of the base
function.
<snip>

Take a look at this. I am sure you can see how things such as service
charge and interest rate could be made a part of the class, for simplicity I
coded these constants in the functions. The order of doing things in main
was dictated by the order of testing,, which is not representative of an end
user application.

#include <iostream>

using namespace std;

class Acct
{
public:
virtual void add_int() = 0;
void show() { cout << balance << endl;}
protected:
double balance;
};
//----------------
class Checking : public Acct
{
public:
Checking(double bal) {balance = bal;}
void add_int() {balance -= 15;} // monthy "service" fee
private:
};
//--------------------
class Savings : public Acct
{
public:
Savings(double bal) {balance = bal;}
void add_int() {balance *= 1.01;}
private:
};
//=====================
int main()
{
Acct* arr[2];
arr[0] = &Checking(256);
arr[0] -add_int();
arr[0] -show();

arr[1] = &Savings(1024);
arr[1] -add_int();
arr[1] -show();

cin.get();
cin.get();
}
Mar 17 '07 #2

P: n/a
hey
i ran your program. it worked just fine.

one change but not relevant to your problem :)
class account
{
public:
int balance, number;
int function() {
cout << balance;
return balance;
}
};

i have question?? dont you need to use virtual keyword?

thanks
sanjay

On Mar 17, 11:32 am, "Wilson" <tpw...@googlemail.comwrote:
hi, i am trying to understand virtual functions, both how to create
them and how or when to use them - how they can be used to improve a
program. Below is a very simple program which i quickly made using
information gathered from a book on the subject, however this simply
returns a blank space. please help both on this program and anything
else related to virtual functions.

wilson

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class account
{
public:
int balance, number;
int function() { cout << balance; }};

class checking : public account
{
public:
int balancechecking;};

class savings : public account
{
public:
int balancesavings;

};

void display(account& x)
{
x.function();

}

int main()
{
savings a;
checking b;
b.balance = 10;
a.balance = 20;
display(a);
cout << endl;
display(b);
system("PAUSE");

}- Hide quoted text -

- Show quoted text -

Mar 17 '07 #3

P: n/a
"Wilson" <tp****@googlemail.comwrote in message
news:11*********************@p15g2000hsd.googlegro ups.com...
hi, i am trying to understand virtual functions, both how to create
them and how or when to use them - how they can be used to improve a
program. Below is a very simple program which i quickly made using
information gathered from a book on the subject, however this simply
returns a blank space. please help both on this program and anything
else related to virtual functions.

wilson

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class account
{
public:
int balance, number;
int function() { cout << balance; }
};
class checking : public account
{
public:
int balancechecking;
};
class savings : public account
{
public:
int balancesavings;
};

void display(account& x)
{
x.function();
}

int main()
{
savings a;
checking b;
b.balance = 10;
a.balance = 20;
display(a);
cout << endl;
display(b);
system("PAUSE");
}
Your code isn't using any virtual functions. It would be more like this
(unchecked code and a bit non realworld):

class account
{
public:
virtual void Deposit( double Amount ) { Balance_ += Amount; }
virtual double Withdraw( double Amount ) { if ( Balance_ - Withdraw 0 )
{ Balance -= Withdraw; return Withdraw } else { return 0.0 };
virtual double Balance() { return Balance_; };
private:
double Balance_;
};

class savings: public account
{
// On savings we love you and give you and extra 0.50 on deposits
void Deposit( double Amount ) { Balance_ += Amount + 0.50; }
};

class checking: public account
{
// On Checking we charge you 0.50 to check your balance
double Balance() { Balance -= 0.50; return Balance; };
};

Play around with that.
Mar 17 '07 #4

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:uY***************@newsfe02.lga...
"Wilson" <tp****@googlemail.comwrote in message
news:11*********************@p15g2000hsd.googlegro ups.com...
>hi, i am trying to understand virtual functions, both how to create
them and how or when to use them - how they can be used to improve a
program. Below is a very simple program which i quickly made using
information gathered from a book on the subject, however this simply
returns a blank space. please help both on this program and anything
else related to virtual functions.

wilson

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class account
{
public:
int balance, number;
int function() { cout << balance; }
};
class checking : public account
{
public:
int balancechecking;
};
class savings : public account
{
public:
int balancesavings;
};

void display(account& x)
{
x.function();
}

int main()
{
savings a;
checking b;
b.balance = 10;
a.balance = 20;
display(a);
cout << endl;
display(b);
system("PAUSE");
}

Your code isn't using any virtual functions. It would be more like this
(unchecked code and a bit non realworld):

class account
{
public:
virtual void Deposit( double Amount ) { Balance_ += Amount; }
virtual double Withdraw( double Amount ) { if ( Balance_ - Withdraw >
0 ) { Balance -= Withdraw; return Withdraw } else { return 0.0 };
virtual double Balance() { return Balance_; };
private:
double Balance_;
};

class savings: public account
{
// On savings we love you and give you and extra 0.50 on deposits
void Deposit( double Amount ) { Balance_ += Amount + 0.50; }
};

class checking: public account
{
// On Checking we charge you 0.50 to check your balance
double Balance() { Balance -= 0.50; return Balance; };
};

Play around with that.
Oooh, bad code, forgot to initialize balance. account needs a constructor.

class account
{
public:
account(): Balance_(0) {};
// ...
};
Mar 17 '07 #5

P: n/a
On Mar 17, 10:28 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Wilson" <tpw...@googlemail.comwrote in message

news:11*********************@p15g2000hsd.googlegro ups.com...


hi, i am trying to understand virtual functions, both how to create
them and how or when to use them - how they can be used to improve a
program. Below is a very simple program which i quickly made using
information gathered from a book on the subject, however this simply
returns a blank space. please help both on this program and anything
else related to virtual functions.
wilson
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
class account
{
public:
int balance, number;
int function() { cout << balance; }
};
class checking : public account
{
public:
int balancechecking;
};
class savings : public account
{
public:
int balancesavings;
};
void display(account& x)
{
x.function();
}
int main()
{
savings a;
checking b;
b.balance = 10;
a.balance = 20;
display(a);
cout << endl;
display(b);
system("PAUSE");
}

Your code isn't using any virtual functions. It would be more like this
(unchecked code and a bit non realworld):

class account
{
public:
virtual void Deposit( double Amount ) { Balance_ += Amount; }
virtual double Withdraw( double Amount ) { if ( Balance_ - Withdraw 0 )
{ Balance -= Withdraw; return Withdraw } else { return 0.0 };
virtual double Balance() { return Balance_; };
private:
double Balance_;

};

class savings: public account
{
// On savings we love you and give you and extra 0.50 on deposits
void Deposit( double Amount ) { Balance_ += Amount + 0.50; }

};

class checking: public account
{
// On Checking we charge you 0.50 to check your balance
double Balance() { Balance -= 0.50; return Balance; };

};

Play around with that.- Hide quoted text -

- Show quoted text -
hi, sorry but im still confused as to how this should be written, i
aletered my origional slightly to, i thought, sort the problem however
the same problem has ocurred. Please help

Wilson

#include <iostream>
using namespace std;

class account
{
public:
virtual float returnbalance() { return balance; cout <<
balance << endl; }
float balance;
};

class checking : public account
{
public:
checking::checking()
{
checking::balance = 10;
cout << endl;
}
int a;
};

class savings : public account
{
public:
savings::savings()
{
savings::balance = 20;
cout << endl;
}
int b;
};

int main()
{
checking one;
savings two;
one.returnbalance();
two.returnbalance();
system("PAUSE");
}

Mar 18 '07 #6

P: n/a
"Wilson" wrote:
hi, sorry but im still confused as to how this should be written, i
aletered my origional slightly to, i thought, sort the problem however
the same problem has ocurred. Please help

Wilson

#include <iostream>
using namespace std;

class account
{
public:
virtual float returnbalance() { return balance; cout <<
balance << endl; }
float balance;
};

class checking : public account
{
public:
checking::checking()
{
checking::balance = 10;
cout << endl;
}
int a;
};

class savings : public account
{
public:
savings::savings()
{
savings::balance = 20;
cout << endl;
}
int b;
};

int main()
{
checking one;
savings two;
one.returnbalance();
two.returnbalance();
system("PAUSE");
}
Look at this link and read it carefully. Hint: note that in your code,
account is *not* "furthest".
Mar 18 '07 #7

P: n/a
Dnia Sun, 18 Mar 2007 05:34:52 -0700, Wilson napisał(a):
>>hi, i am trying to understand virtual functions, both
how to create them and how or when to use them - how they
can be used to improve a program.

sorry but im still confused as to how this should be written
Maybe try this example:

#include <iostream>

class Instrument
{
public:
//The great and famous virtual function :)
virtual void MakeSound()
{
std::cout << "Some strange noise" << std::cout;
}
//In a real-world solution, this should be declared
//as pure virtual to make this class abstract type
//[not concrete instrument].
};

//Concrete example of an instrument.
class Piano : public Instrument
{
public:
//Overriding virtual function.
void MakeSound()
{
std::cout << "Plink plank plonk" << std::endl;
}
}

class Drum : public Instrument
{
public:
void MakeSound()
{
std::cout << "Crash! Bang!" << std::endl;
}
};

class Trumpet : public Instrument
{
public:
void MakeSound()
{
std::cout << "Tra ta taa!" << std::endl;
}
}

class Musician
{
public:
void Play(Instrument* ins)
{
ins->MakeSound(); //Calling virtual function.
//This will call the overrided version
//of virtual function from the derived class.
}
}

//Let's play some funky music! :)
int main()
{
//Let we create some instruments from the free store.
//Pointers to instruments will be stored in an array.
Instrument* instruments[] = {
new Trumpet(),
new Trumpet(),
new Piano(),
new Drums()
};

Musician virtuose;

//He can play every instrument we give him:
for (int i=0; i<4; ++i) virtuose->Play(instruments[i]);

//Show's over, so let's break those instruments! :P
for (int i=0; i<4; ++i) delete instruments[i];
}

Check out the output from this program.
You should see that all instruments played by musician will
make their special beautiful sounds, and not the strange noise
from the base class ;)

--
SasQ
Mar 18 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.