Thread: Need help coming up with variables, functions

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Need help coming up with variables, functions

    Im not sure if Im doing this right but i need some help with this problem. I just have some of the basic stuff right now. Not sure what variables to use for calculating an investment. Thanks if anyone could help a bit.

    Problem:
    Create an Investment class that includes the appropriate data members for various types of investments. Write appropriate get and set functions for each data member.
    Also include a data member that holds the investment’s total value. Use a parameterized constructor to receive the name of each investment in a string* pointer. Be sure to include a destructor to clean up the heap when each object is destroyed. Also include a static data member and a static member function that calculates the total worth of an investment portfolio by adding together the total value of data members for each instantiated object. Add the Investment class to a console application, and then instantiate an Investment object and initialize its data. Create two additional Investment objects by copying the original object. You will need to write a copy constructor for when you duplicate objects. Retrieve and print the data members for each object. Also print the contents of the static data member using a statement similar to “Your investments are worth a total of $100,000.”

    Header File:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Investment
    {
    public:
    	Investment();
    	void Investment(string*);
    	~Investment();
            string* getName();
    	int getValue();
            void setValue();
    	void setName(string*);
    	double calcInvestment(double);
                    
    	
    private:
    	static long totalInvestment;
        int iNumInvestments;
    };
    Main:
    Code:
    #include <iostream>
    #include "Investment.h"
    using namespace std;
    
    int main()
    {
    	
    	cout << "Your investment is worth a total of " << totalInvestment;
    	
    };
    Implementation:
    Code:
    Investment::Investment()
    {
    	pName = new string;
    }
    
    void Investment::Investment(string* pName)
    {
    	pName = new string;
    	*pName = 
    }
    
    Investment::~Investment()
    {
         delete pName;
         cout << "Previous Investment deleted" << endl;
    }
    
    string* Investment::getName(void) const
    {
    	return *pName;
    }
    
    void Investment::setName()
    {
    	return pName;
    }
    
    void Investment::setValue()
    {
    	return sharesValue;
    }
    
    int Investment::getValue()
    {
    	return sharesValue;
    }

    Here is what the output is supposed to be:
    Code:
    Your investment in Cisco
    based on 100 shares at $67.5
    is worth $6750
    
    Your investment in Janus Fund
    based on 200 shares at $98.5
    is worth $19700
    
    Your investments are worth a total of $26450
    Your investment in California Municipal Bonds
    based on 500 shares at $23.5
    is worth $11750
    
    Your investments are worth a total of $38200
    Last edited by Dokkan; 04-20-2010 at 03:36 PM.

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    	Investment(void);
    	void Investment(string*);
    	void ~Investment();
    	int getInvestment();
    	void setInvestement();
    Constructors and destructors don't have a return type (not even void).

    getInvestment() and setInvestment() are bad names. Investment is the class name; that's not what you're getting and setting. You get and set the members of the class.
    For example, you might have the methods:
    Code:
    void Investment::setName(string* name)
    string* Investment::getName()
    int Investment::getValue()
    ...
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Quote Originally Posted by NeonBlack View Post
    Code:
    	Investment(void);
    	void Investment(string*);
    	void ~Investment();
    	int getInvestment();
    	void setInvestement();
    Constructors and destructors don't have a return type (not even void).

    getInvestment() and setInvestment() are bad names. Investment is the class name; that's not what you're getting and setting. You get and set the members of the class.
    For example, you might have the methods:
    Code:
    void Investment::setName(string* name)
    string* Investment::getName()
    int Investment::getValue()
    ...
    Okay I changed it. Now I need help coming up with the right variables to use.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Well it looks like you still need a member to hold the name, and one to hold the value. What variables do you think would be appropriate to store the... NAME and the, *ahem*, VALUE?

    Try to do as much of this on your own, even if it completely sucks. When you're done, or if you get stuck, bring it back here and someone will help you fix it.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    I added what the output is supposed to be above. I also updated the code a bit. So I am going to need a variable to hold the shares and money per share. Then output them multiplied and add all the totals up at the end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Global variables and functions.
    By earth_angel in forum C Programming
    Replies: 6
    Last Post: 07-25-2005, 12:38 PM
  3. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM