The class will contain: - A string data field named \"symbol\" for the stock\'s
ID: 3545918 • Letter: T
Question
The class will contain:
- A string data field named "symbol" for the stock's symbol
- A string data field named "name" for the stock's name
- A double data field name "previousClosingPrice" that stores the stock price for the previous day
- A double data field named "currentPrice" that stores the stock price for the current time
- A constructor that creates a stock with specificed symbol name
- The access function for all data fields (such as "return_name"...)
- The readPrice function for "previousClosingPrice" and "currentPrice"
- A function named "changePercent()" that returns the percentage change from "previousClosingPrice" to "currentPrice"
Implement the class. Write a test program that creates a Stock class object with the stock "symbol" GOOG, the name Google, and the previous closing price of 450. Set a new current price to 539 and display the price-change percentage.
Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
class stock
{
char symbol[10];
char name[10];
double previousClosingPrice,currentPrice;
public:
stock()
{
previousClosingPrice=0.0;
currentPrice=0.0;
}
stock(char *sym,char *nam, double pcp=0.0, double cp=0.0) //default arguments are used here
{
strcpy(symbol,sym);
strcpy(name,nam) ;
previousClosingPrice=pcp;
currentPrice=cp;
}
void setSymbol(char *nam)
{
strcpy(symbol,nam);
}
void setName(char *nam)
{
strcpy(name,nam);
}
void setPCPrice(double value) //to set previousClosingPrice
{
previousClosingPrice=value;
}
void setCPrice(double value) //to set currentPrice
{
currentPrice=value;
}
char* return_symbol()
{
return symbol;
}
char* return_name()
{
return name;
}
double return_previousClosingPrice()
{
return previousClosingPrice;
}
double return_currentPrice()
{
return currentPrice;
}
void readPrice()
{
cout<<" Previous Closing Price: "<<previousClosingPrice<<" Current Price: "<<currentPrice;
}
double changePercent()
{
return (currentPrice-previousClosingPrice)/previousClosingPrice*100.0;
}
};
//Test Program
int main()
{
stock g("goog","Google",450);
g.setCPrice(539);
g.readPrice();
cout<<" Change Percent:"<<g.changePercent()<<"%";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.