I am trying to declare a derived class conditional on user input and am not sure how to implement it. how would I pass the fact that the user has chosen the "bond" class and still take advantage of the virtual function Price()? when I create an if statement, the compiler wont pass my declared bond to the main function.
thanks.
#include <iostream>
#include <string>
#include "securityPrice.h"
using namespace std;
int main()
{double price, discount, cashflow,value,tt;
int expiry;
string type1;
cout << "\nWhat type of security is it?\n";
getline(cin,type1);
cout << "\nWhat is the "<< type1 << "'s price?\n";
cin >> price;
cout << "\nWhat is your discount rate?\n";
cin >> discount;
cout << "\nWhat are the associated cashflows?\n";
cin >> cashflow;
cout << "\nIn how many periods will the "<< type1<< " be sold?\n";
cin >> expiry;
Security s1;
s1.setPrice(1,price,discount,cashflow,expiry);
if (type1 == "bond")
{Bond s1;
}
s1.Price();
value = s1.getPrice();
return value
cout << "\nThe discounted price of your " <<type1<< " is:\n";
cout << value;
cin >> tt;
return 0;
}
Can you really declare an if statement as a friend? to clarify what I want to do:
-user chooses security
-a security type is chosen creating an inherited object (in this case a bond)
- that object calculates its price
- returns its price to the user.
what I am having trouble with is conditionally creating an inherited class and then passing it to the main function.
much thanks for the help.
#ifndef SECURITY_H
#define SECURITY_H
class Security
{protected:
double p;
double c;
double d;
int e;
int t;
#ifndef SECURITY_H
#define SECURITY_H
class Security
{protected:
double p;
double c;
double d;
int e;
int t;
public:
Security();
void setPrice( constint Type,
double Price,
constdouble Discount,
constdouble Cashflow,
constint Expiry);
virtualvoid Price();
double getPrice() const;
};
Security::Security() /*This is where you define the constructor that was declared on line 12 */
{}
class Bond: public Security /*This all looks good so far but you may not need to redefine Price()*/
{public:
void Price();
};
void Security::Price()/*This is where you would define your Price function*/
{}
/*The format to define a member function is datatype class::nameoffunction(argument)*/
/*You also need to define SetPrice, GetPrice and bond.*/
#endif
EDIT: To answer you question though I would rewrite that part of your Main function to something like this:
1 2 3 4 5
/*Instead of declaring s1 before the if statement do this*/
if (type1 == "bond")
{Bond s1;}
else
{Security s1;}
This way you don't have a variable to conflict until you see what it should be.
EDIT: Sorry you would then pass it to a function to do the rest.
int myfunction(myclass); /*Declare the function with an argument data type matching your class */
//...
//...
int myfunction(myclass myvariable) /*Define your function with an argument data type matching your class */
{//...
//...
//...}
If you mean how to get that function to RETURN a variable of your class do this:
Ok I think I get it now. In your sample code here "myclass" is the reference you're refering to otherwise when you declare the member functions don't declare them as void like you did with Price(...) but declare them with a type like this:
1 2 3 4 5
class Security {
//...
//...
virtualdouble Price();
//...
AND
1 2 3
class Bond: public Security: {
//...
double Price();
EDIT: That way you have a data type that you can assign to a local variable inside of Main(..). I don't suggest doing it this way however as it breaks up the class from it's data members.
ohh.....ok that makes perfect sense now. thanks alot. so what do you suggest that I do given that I am trying to find an "elegant" way of formulating this code?(i.e. as little duplication as possible with the most flexibility. this really forces one to learn!!!)
You're on the right track I think. I'm not too familiar with Securities and Exchanges so I don't know how closley you can treat each data type. But from what we have here I would have each Price() function modify a data member of their respective class and use that to work with inside of the main function. This ensures that each instance of each variable can have independent values.