This is a basic c++ question Design and write code for a class named Stock that
ID: 3701533 • Letter: T
Question
This is a basic c++ question
Design and write code for a class named Stock that contains:
============
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 named previosuClosingPrice 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 a specified symbol and name.
The getter function members for all fields.
Setter functions for previousClosingPrice and currentPrice
A function named changePercent() that returns the percentage changed from previosClosingPrice to currentPrice.
==========
Write a test program that creates a stock object with the stock symbol MSFT and the name Microsoft Corporatio nand the previous closing price of 17.5.
Set a new current price to 17.6 and display the price change percentage
Explanation / Answer
#include <iostream>
using namespace std;
class Stock {
private:
string symbol, name;
double previousClosingPrice ,currentPrice ;
public:
Stock(string s, string n) {
name = n;
symbol = s;
}
string getName() {
return name;
}
string getSymbol() {
return symbol;
}
double getPreviousClosingPrice () {
return previousClosingPrice ;
}
double getCurrentPrice() {
return currentPrice;
}
void setCurrentPrice(double c) {
currentPrice = c;
}
void setPreviousClosingPrice(double d) {
previousClosingPrice = d;
}
double changePercent() {
return (currentPrice - previousClosingPrice)*100/previousClosingPrice;
}
};
int main()
{
Stock s("MSFT", "Microsoft Corporatio");
s.setPreviousClosingPrice(17.5);
s.setCurrentPrice(17.6);
cout<<"Current price: "<<s.changePercent()<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.