C++ Composition Not Working?

The code keeps returning the values from the default constructor, instead of returning the values that were inputted by the user.

So say the user inputs ABCD and 99 for the symbol and shareprice. I'm able to show the value with aStock.displaystock();. But when i do it through the class StockPurchase, it keeps returning the value that is in the default constructor.

I've looked through my textbook and online and i can't find anything on how to fix it.
This is where the problem happens.


1
2
3
4
5
6
void StockPurchase::displayStockPurchase()
{

	object.displayStock();
	cout << "The number of shares you want to purchase is " << sharestopurchase << endl;
	cout << "The total cost of your purchase is " << getCost() << endl;


So for object.displayStock();

It is able to return the cout messages from displayStock();

but it does not return the values of symbol and shareprice. Instead it shows the values put in the default constructor. Which is

Stock::Stock()
{
symbol;
shareprice = 0.0;

}


Here is part of the code.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100


class Stock
{

private:
	string symbol;
	double shareprice;
public:
	Stock();
	Stock(string, double);
	void setStock(string, double);
	string getSymbol();
	double getSharePrice();
	void displayStock();
};

Stock::Stock()
{
	symbol;
	shareprice = 0.0;

}
Stock::Stock(string sym, double price)
{
	
	symbol = sym;
	shareprice = price;
}
void Stock::setStock(string sym , double price)
{
	symbol = sym;
	shareprice = price;
	
}
string Stock::getSymbol()
{
	return symbol;
}
double Stock::getSharePrice()
{
	return shareprice;

}
void Stock::displayStock()
{
	
	cout << "The Stock symbol is " << getSymbol() << endl;
	cout << "The share price of the stock you entered is  " << getSharePrice()<< endl;

}

class StockPurchase
{
private:
	int sharestopurchase;
	double totalcost;
	Stock object;
public:
	StockPurchase();
	double getCost();
	StockPurchase(int, double, string, double);
	void setSharestoPurchase(int);

	void displayStockPurchase();

};
StockPurchase::StockPurchase()
{
	sharestopurchase = 0;
	totalcost = 0;

}
StockPurchase::StockPurchase(int num, double total, string symbol, double price) : object(symbol, price)
{
	totalcost = total;
	sharestopurchase = num;
	
}
void StockPurchase::setSharestoPurchase(int sharesto)
{
	sharestopurchase = sharesto;
	
}
double StockPurchase::getCost()
{
	
	 totalcost = sharestopurchase*object.getSharePrice();

	 return totalcost;
}
void StockPurchase::displayStockPurchase()
{

	object.displayStock();
	cout << "The number of shares you want to purchase is " << sharestopurchase << endl;
	cout << "The total cost of your purchase is " << getCost() << endl;

}
Last edited on
Post the code that uses the StockPurchase class.
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
int main()
{
	Stock aStock;
	string symbo;
	double pric;
	StockPurchase aPurchase;
	int amt;

	cout << "Please enter the symbol of your stock." << endl;
	cin >> symbo;
	cout << "Please enter the share price of your stock." << endl;
	cin >> pric;
	aStock.setStock(symbo, pric);
	cout << "Please enter the amount of shares you want to purchase." << endl;
	cin >> amt;
	aPurchase.setSharestoPurchase(amt);



	aPurchase.displayStockPurchase();

	


	cout << "Please press enter to continue." << endl;
	cin.ignore(2);
	return 0;
}


Its just in the main(). I have the user input values and then i call the display functions.
The object named aStock in main() and the member StockPurchace::object of the StockPurchace instance named aPurchase in main() are unrelated. When you call aStock.setStock(symbo, pric) you're only modifying the Stock instance in main(), not the class member. StockPurchase only modifies its Stock member when parameters are passed to its constructors, but you default-construct it on line 6. You'll either have to wait until line 16 to create the StockPurchase and then call the constructor that takes four parameters, or you'll have to add a function to StockPurchase that calls Stock::setStock().
Thank you.

Figured it out.

This is what i ended up doing.

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
int main()
{

	string symbo;
	double pric;
	int amt;
	
	cout << "Please enter the symbol of your stock." << endl;
	cin >> symbo;
	cout << "Please enter the share price of your stock." << endl;
	cin >> pric;
	cout << "Please enter the amount of shares you want to purchase." << endl;
	cin >> amt;
	
	StockPurchase aPurchase(amt, symbo, pric);
	aPurchase.displayStockPurchase();

	

	


	cout << "Please press enter to continue." << endl;
	cin.ignore(2);
	return 0;
}


I am able to call the display function and it shows the user inputs now, instead of the default constructor values.
Topic archived. No new replies allowed.