I have the following situation. I have 2 classes; call them ClassA and ClassB.
I have another class; call it Class1. Class1 has several methods that use members from either ClassA or ClassB. However, it has a particular method that needs to know what kind of class is being passed into it (for instance, if I pass an object of ClassA into it, then it does something, but if I pass an object of ClassB into it, then it does something else). Currently, Class1 is blind to the type of class that is being passed into its methods. (I achieved this by having ClassA and ClassB being children classes of a single parent class, and passing in a reference to the parent class inside the methods in Class1.) I need to keep Class1 blind to the type of class that is being passed into its methods, yet I also need to determine the class that is being passed into it for one of its methods. Is this possible to do? If so, how do I go about doing this?
I was thinking that I might need to use this-> or templates, but I'm not sure. Thanks!
Good deal. usingnamespace std; took care of that. One more question. How do I call a function (from main.cpp) that has a pointer as an argument?
For instance, my function (in my Class1 cpp file) is:
1 2 3 4 5 6 7 8 9 10 11
void Class1::function(ParentClass *ParentClass_pointer)
{
if (ParentClass_pointer->class_type == "ClassA")
{
// do stuff given that we have an object of ClassA
}
else
{
// do stuff given that we have an object of ClassB
}
}
In main.cpp, do I call it as such?:
Class1_object.function(&ChildClass_object); // send in an object of ClassA or ClassB
Both storing a type-identifying value and dynamic_cast/typid using RTTI are signs of poor design.
From what you said, function overloading should do the trick--although, you later mentioned that Class1 had to remain "blind" so that may not fit your requirements.
You could also approach this using template specialization.
If you provide concrete examples, we can probably identify more options involving some redesign.
If you have to know the type of an object for anything other than serialization, that's most likely a design flaw. As stated above, we need a fully explained and demonstrated example to further help you.
#ifndef BINOMIAL_TREE_H_
#define BINOMIAL_TREE_H_
#include "option.h"
#include "Stock.h"
// creates a binomial tree for any type of option passed to it
class binomial_tree
{
private:
double option_price;
public:
// constructor:
binomial_tree(int N_bucket);
// data members:
int N; // the number of periods in the tree
double dt; // the size of a time period, given N and T
double u; // the up factor
double d; // the down factor
double disc; // the constant discount factor across each time period
double p; // the probability of an up move in the tree
// methods:
void parameters_calculator(option &option_ref, Stock &Stock_ref); // calculate the parameters
void price_calculator(option &option_ref, Stock &Stock_ref, option *option_pointer); // calculate the price of the option
void set_price(double); // mutator
double get_price(); // accessor
};
#endif
#include "binomial_tree.h"
#include <math.h> // used for exp, sqrt, and pow
#include "option.h"
#include "Stock.h"
#include <map.h>
// constructor:
binomial_tree::binomial_tree(int N_bucket) : N(N_bucket)
{
// set the data members in case the methods are not called explicitly
dt = 0;
u = 0;
d = 0;
disc = 0;
p = 0;
}
//methods:
// calculate the necessary parameters:
void binomial_tree::parameters_calculator(option &option_ref, Stock &Stock_ref)
{
dt = option_ref.T/N; // the size of a time period
u = exp(Stock_ref.sigma*sqrt(dt)); // the up factor
d = 1/u; // the down factor
disc = exp(-option_ref.r*dt); // the discount factor
p = (1/disc - d) / (u - d); // the risk-neutral probability of an up move in stock price
}
void binomial_tree::set_price(double option_price_temp)
{
option_price = option_price_temp;
}
double binomial_tree::get_price()
{
return option_price;
}
// calculate the price of the option at time 0 using a binomial tree model:
void binomial_tree::price_calculator(option &option_ref, Stock &Stock_ref, option *option_pointer)
{
// I have adapted a code for pricing a call option that I found on http://www.ma.utexas.edu/users/nringer/pricing.htmldouble S[N + 1]; // array for the stock price at time T
S[0] = Stock_ref.getValue()*pow(d,N); // let the first element be the lowest possible stock price
// build the rest of the array for the stock price at time T
for(int i = 1; i <= N; i++)
{
S[i] = S[i - 1]*u/d; // substitute a d for a u as we go up the array
}
// compute the value of the option for all end state values of the stock price (at time T)
// V[i][j] is the option price for j up moves of the stock price at time i
map<int, double> V[N + 1];
for(int j = 0; j <= N; j++)
{
if ((option_pointer->class_type == "AC") || (option_pointer->class_type == "EC"))
{
// a call has payoff max(S - K, 0)
double a = S[j] - option_ref.K;
if (a > 0)
{
V[N][j] = a;
}
else
{
V[N][j] = 0;
}
}
else
{
// a put has payoff max(K - S, 0)
double b = option_ref.K - S[j];
if (b > 0)
{
V[N][j] = b;
}
else
{
V[N][j] = 0;
}
}
}
// discount the option values at each state for a given time down to the previous time
for (int i = N - 1; i >= 0; i--)
{
for (int j = 0; j <= i; j++)
{
// the up stock price is at position j + 1; the down move is at position j
V[i][j] = disc*(p*V[i + 1][j + 1] + (1.0 - p)*V[i + 1][j]);
}
}
set_price(V[0][0]); // set the option price
}
#ifndef OPTION_H_
#define OPTION_H_
#include "Asset.h"
#include <string>
usingnamespace std;
class option : public Asset
{
public:
// constructor (calling format is: t, K, T, r):
option(double t_dummy, double K_dummy, double T_dummy, double r_dummy);
// data members:
double t; // present time
double K; // option strike
double T; // maturity date
double r; // constant interest rate (continuously compounded, per year)
// methods:
double cum_dist_normal(double); // compute the cumulative distribution of Z (same for call and put)
virtualdouble getPayout(double, double) = 0; // return the payout, given a spot price and a time
virtual string class_type() = 0; // set the class type
};
#endif
An example child class of option parent class (there are 4 child classes in all):
AmericanCall.h
// create a European Call:
EuropeanCall ECb_object(0, 105, 1.5, 0.09);
// create a Stock and set its price:
Stock Stockb_object(0.35, 0);
Stockb_object.setValue(105); // this makes the ECb_object ATM
// calculate the European Call's price via Black-Scholes:
ECb_object.BlackScholesValue(Stockb_object);
cout << "Black-Scholes: C = $" << ECb_object.getValue() << endl;
// determine the European Call's price via the binomial tree:
binomial_tree b_treeb(1500);
b_treeb.parameters_calculator(ECb_object, Stockb_object);
b_treeb.price_calculator(ECb_object, Stockb_object, &ECb_object);
cout << "Binomial Tree: C = $" << b_treeb.get_price() << endl;
I have the following situation. I have 2 classes; call them ClassA and ClassB.
I have another class; call it Class1. Class1 has several methods that use members from either ClassA or ClassB. However, it has a particular method that needs to know what kind of class is being passed into it (for instance, if I pass an object of ClassA into it, then it does something, but if I pass an object of ClassB into it, then it does something else). Currently, Class1 is blind to the type of class that is being passed into its methods. (I achieved this by having ClassA and ClassB being children classes of a single parent class, and passing in a reference to the parent class inside the methods in Class1.) I need to keep Class1 blind to the type of class that is being passed into its methods, yet I also need to determine the class that is being passed into it for one of its methods. Is this possible to do? If so, how do I go about doing this?
What do you need to have happen? You should 'tell' the class that you get to do their thing and, because THEY know what they are, they can do different things. An outside view need not know what something is.