Passing a class?

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;
}
This sounds like what you want to do:
http://www.cplusplus.com/doc/tutorial/inheritance/
I don't see your class declaration though could you post your securityPrice header as well?
Last edited on
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;

public:
Security();
void setPrice( const int Type,
double Price,
const double Discount,
const double Cashflow,
const int Expiry);
void Security::virtual Price() = 0;
double getPrice() const;

};
Security::
public: virtual Price();

class Bond: public Security
{public:
void Price();
};



#endif

You wouldn't declare the if statement as the friend. First off lets fix a few things in your header file that will stop this from compiling:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef SECURITY_H
#define SECURITY_H
class Security
{protected:
double p;
double c;
double d;
int e;
int t;

public:
Security();
void setPrice( const int Type,
double Price,
const double Discount,
const double Cashflow,
const int Expiry);
virtual void 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.
Last edited on
Ok. so are you saying that it is better to define a member function outside of the header?
Also does it matter if Bond has no constructor?


by the way here is securityPrice.cpp

#include <cmath>
#include <iostream>
#include <string>
#include "securityPrice.h"
using namespace std;

Security::Security()
{p=0;c=0;d=0;e=0;t=0;
};

void Security::setPrice(int Type,
double Price,
double Discount,
double Cashflow,
int Expiry)
{ t=Type;
p=Price;
c=Cashflow;
d=Discount;
e=Expiry;
};



double Security::getPrice() const
{double v1,n,prob,rate;
prob = 0;
rate = 0;
n = 1 + d/100;
n = pow(n,e);
v1 = e*c/n + p/n;
return v1;
};

void Bond::Price()
{int prob,rate;
cout << "\nwhat is the chance of default?\n";
cin >> prob;
prob /= 100;
cout << "\nwhat is the expected recovery rate?\n";
cin >> rate;
prob /= 100;
p = p * (1-prob) + prob * rate * p;
};
Thats exactly what I was getting at. How to you pass a class to another function?
The same way you pass anyother data type:
1
2
3
4
5
6
7
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:
1
2
3
4
5
6
7
myclass myfunction(); /*Declare it*/
//...
//...
myclass myfunction() /*Define it*/
{//...
 //...
 //...} 


I hope I understood that right.

EDIT: Whoops I defined the function wrong in the first example.
Last edited on
Is there any way to from a function return the pointer for an entire class? i.e.
1
2
3
4
5
6
7
8
main(){
if (string = "string1")
          {Class1 myclass;}
else {Class2 myclass;}

myclass.function1();
myclass.function2();
////..... 
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 {
//...
//...
virtual double 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.
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.