Class functions


Program wants you to create a class Stock, create string data fields for symbol and name of stock, double data fields for the closing and current prices, a constructor with a specified symbol name, access function for the names etc, readprice function for the two prices and a change of percent function for the increase in stock value.

I've gotten the last part to work, but I am stuck on the acces functions....That is, the ones that reads the names....It gives me an error on Stock google (google,Goog)

here's my code so far

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
#include <iostream>
#include <istream>
#include <string>
#include <iomanip>


using namespace std;

class Stock
{
	string name,symbol;
	double closingPrice,currentPrice;
public:
	Stock (double,double);
	Stock(string a,string b);
 string return_name () {return name,symbol;}
	double changePercent () {return ((currentPrice-closingPrice)/currentPrice)*100;}
};

Stock::Stock ( double x,double z)
{
	closingPrice = x;
	currentPrice = z;
}
Stock::Stock (string a  ,string b)
{
	name= a;
	symbol= b;
}


int main ()
{
	Stock google  (450,539);
	Stock google (google,Goog);

	cout<<"The name of the stock is:"<<google.return_name();
	
	cout<<"The name of the stock is: "<<
	cout<<"The stock grew by: "<<setprecision (2)<<google.changePercent()<<"%"<<endl;
	return 0;
}
return_name() should return a single string as defined but you're trying to return two of them. Why don't you concatenate the two? return something like name + ", " + symbol. Also, it's weird to have two constructors like that, why don't you combine them? (btw: google and Goog aren't string, "google" and "Goog" are).
1
2
Stock google  (450,539);
Stock google (google,Goog);


The first line here says 'create a new object of type Stock, called google, and here are some parameters you'll need to make it'
The second line says 'create a new object of type Stock, called google, and here are some parameters you'll need to make it'.

Hold on, you've tried to create two different objects with the same name! You can't create two different objects with the same name.


I thought 2 constructors looked wrong....
Topic archived. No new replies allowed.