Virtual functions

W

Wilson

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");
}
 
O

osmium

Wilson said:
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();
}
 
H

hijkl

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
 
J

Jim Langston

Wilson said:
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.
 
J

Jim Langston

Jim Langston said:
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) {};
// ...
};
 
W

Wilson

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");
}
 
O

osmium

Wilson said:
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".
 
S

SasQ

Dnia Sun, 18 Mar 2007 05:34:52 -0700, Wilson napisa³(a):
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);

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

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 ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top