Create a class named Stock . An object of this class contains information about
ID: 3567936 • Letter: C
Question
Create a class named Stock. An object of this class contains information about a company's stock.
Create another class named StockPurchase that uses an object of the Stock class.
Test your program by creating at least three StockPurchase objects, provide values for their attributes, and confirm the total cost of the stock purchase.
Note: The user should be able to enter the symbol, share price, and amount of shares to purchase into local variables in the main - then those variables will become the arguments in the setStock and setSharestoPurchase functions.
Explanation / Answer
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Stock
{
char symbol[4];
float shareprice;
public:
Stock()
{
strcpy(symbol , "");
shareprice = 0;
}
Stock(char *sym, float price)
{
strcpy(symbol, sym);
shareprice = price;
}
void setStock(char *sym, float price)
{
strcpy(symbol, sym);
shareprice = price;
}
char* getSymbol()
{
return symbol;
}
float getSharePrice()
{
return shareprice;
}
};
class StockPurchase
{
Stock stock;
int shareToPurchase;
public:
StockPurchase()
{
stock = Stock();
shareToPurchase = 0;
}
StockPurchase(Stock s, int p)
{
stock = Stock(s.getSymbol(), s.getSharePrice());
shareToPurchase = p;
}
void setSharesToPurchase(int p)
{
shareToPurchase = p;
}
float getCost()
{
return (stock.getSharePrice() * shareToPurchase);
}
};
int main()
{
char s[4];
float sp, pur;
Stock s1 = Stock();
Stock s2 = Stock();
Stock s3 = Stock();
cout<<"Enter 1st stock: ";
cout<<"Enter symbol: ";
cin>>s;
cout<<" Enter price: ";
cin>>sp;
s1.setStock(s, sp);
cout<<" Enter shares to purchase: ";
cin>>pur;
StockPurchase Sp1 = StockPurchase(s1, pur);
cout<<" 1st stock purchase of "<<pur<<" of "<<s<<" costs: $"<<Sp1.getCost()<<" ";
cout<<"Enter 2nd stock: ";
cout<<"Enter symbol: ";
cin>>s;
cout<<" Enter price: ";
cin>>sp;
s2.setStock(s, sp);
cout<<" Enter shares to purchase: ";
cin>>pur;
StockPurchase Sp2 = StockPurchase(s2, pur);
cout<<" 1st stock purchase of "<<pur<<" of "<<s<<" costs: $"<<Sp2.getCost()<<" ";
cout<<"Enter 3rd stock: ";
cout<<"Enter symbol: ";
cin>>s;
cout<<" Enter price: ";
cin>>sp;
s3.setStock(s, sp);
cout<<" Enter shares to purchase: ";
cin>>pur;
StockPurchase Sp3 = StockPurchase(s3, pur);
cout<<" 1st stock purchase of "<<pur<<" of "<<s<<" costs: $"<<Sp3.getCost()<<" ";
cout<<" ";
return 0;
}
-------------------------
output
Enter 1st stock:
Enter symbol: dis
Enter price: 23.3
Enter shares to purchase: 2
1st stock purchase of 2 of dis costs: $46.6
Enter 2nd stock:
Enter symbol: appl
Enter price: 23
Enter shares to purchase: 44
1st stock purchase of 44 of appl costs: $1012
Enter 3rd stock:
Enter symbol: dis
Enter price: 33.3
Enter shares to purchase: 45
1st stock purchase of 45 of dis costs: $1498.5
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.