Array of class objects

This is a discussion on Array of class objects within the C++ Programming forums, part of the General Programming Boards category; The question becomes, what you do want to do with them?...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    The question becomes, what you do want to do with them?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I guess i need to do all of the work that was originally done in main through the class functions now?

    Well I need to display all of the items with a number in order to select them. Then work with them like I was doing with the built in items in my original code.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    You could overload functions that returns the private data so you can display it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    That sounds awesome I would love to make all of that information no longer private although I'm not sure that's what I'm suppose to do.

    I need to be able to access the information in the switch statement and others... I guess I do need it to be free data; not sure if that's what my teacher wants though. Oh well, I just want it to work after with your help I was able to make the array of class objects.

    Is overloading ok to do?

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    You don't need to make it public.
    Just make member functions that returns the data you need.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Like this?

    Code:
    #include <string>
    #include <iostream>
    
    class dispenserType
    {
    	public:
    		int getNoOfItems() const;
    		int getCost() const;
    		void makeSale();
    		dispenserType(int setNoOfItems = 50, int setCost = 50);
    		void read(std::ifstream& readfrom)
    		{
    			readfrom >> name;
    			readfrom >> numberOfItems;
    			readfrom >> cost;
    		}
    		void return(std::string& heldname int& heldnoi int& heldcost)
    		{
    			heldname = name;
    			heldnoi = numberOfItems;
    			heldcost = cost;
    		}
    	private:
    		std::string name;
    		int numberOfItems;
    		int cost;
    };
    then would (in main) productlist[0].heldname give me the first held name?! I think I'm getting close!

    or should I make a function to return each different variable and be like

    std::string returnname()
    Code:
    {
        std::string heldname;
        heldname = name
    
       return name;
    }
    can I return a string?

    productlist[0].returnname
    Last edited by GCNDoug; 03-26-2008 at 02:25 PM.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    Quote Originally Posted by GCNDoug View Post
    Like this?
    Maybe...
    But what if you don't want all 3 of them?
    Better create 3 functions, each that returns said part of the data you need.

    then would (in main) productlist[0].heldname give me the first held name?!
    No, it wouldn't (at least if I'm interpreting this correctly). All you're doing is defining a member function that takes an std::string reference named heldname as argument. You need to pass in a variable of type std::string to the function which will then, upon return of the function, have the information you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Code:
    #include <string>
    #include <iostream>
    
    class dispenserType
    {
    	public:
    		int getNoOfItems() const;
    		int getCost() const;
    		void makeSale();
    		dispenserType(int setNoOfItems = 50, int setCost = 50);
    		void read(std::ifstream& readfrom)
    		{
    			readfrom >> name;
    			readfrom >> numberOfItems;
    			readfrom >> cost;
    		}
    		std::string returnname(std::string heldname)		
    		{
    			heldname = name;
    			return heldname;
    		}
    		int returnnoi(int noi)
    		{
    			noi = numberOfItems;
    			return noi;
    		}
    		int returncost (int& heldcost)
    		{
    			heldcost = cost;
    			return cost;
    		}
    	private:
    		std::string name;
    		int numberOfItems;
    		int cost;
    };
    Code:
    while (choice !=99)
    	{
    		switch (choice)
    		{
    		case 1:
    			sellProduct (productlist[0].returnname(name), counter);
    			break;
    		case 2:
    			sellProduct (productlist[1].returnname(name), counter);
    			break;
    		case 3:
    			sellProduct (productlist[2].returnname(name), counter);
    			break;
    		case 4:
    			sellProduct (productlist[3].returnname(name), counter);
    			break;
    		case 5:
    			sellProduct (productlist[4].returnname(name), counter);
    			break;
    		case 6:
    			sellProduct (productlist[5].returnname(name), counter);
    			break;
    		case 7:
    			sellProduct (productlist[6].returnname(name), counter);
    			break;
    		case 8:
    			sellProduct (productlist[7].returnname(name), counter);
    			break;
    		case 9:
    			sellProduct (productlist[8].returnname(name), counter);
    			break;
    		default:
    			cout << "Invalid selection." << endl;
    }
    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\candymachinelab. cpp(66) : error C2664: 'sellProduct' : cannot convert parameter 1 from 'std::string' to 'dispenserType []'
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    lol

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    Come on. This is obvious.
    sellProduct takes a dispenserType array.
    You need to think about your implementation. What do you want and where?

    And as for this:
    Code:
    		std::string returnname(std::string heldname)		
    		{
    			heldname = name;
    			return heldname;
    		}
    		int returnnoi(int noi)
    		{
    			noi = numberOfItems;
    			return noi;
    		}
    		int returncost (int& heldcost)
    		{
    			heldcost = cost;
    			return cost;
    		}
    You are returning the data, so it does not need to take an argument.
    Code:
    		std::string returnname()		
    		{
    			return heldname;
    		}
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I want the name lol! I fixed the class functions though. Will keep working on the other stuff thanks again for ur help!
    void sellProduct(dispenserType product[], cashRegister& pCounter);
    dispenserType productlist[8];

    productlist is a dispenserType

    Code:
    #include <string>
    #include <iostream>
    
    class dispenserType
    {
    	public:
    		int getNoOfItems() const;
    		int getCost() const;
    		void makeSale();
    		dispenserType(int setNoOfItems = 50, int setCost = 50);
    		void read(std::ifstream& readfrom)
    		{
    			readfrom >> name;
    			readfrom >> numberOfItems;
    			readfrom >> cost;
    		}
    		std::string returnname()		
    		{
    			return name;
    		}
    		int returnnoi()
    		{
    			return numberOfItems;
    		}
    		int returncost()
    		{
    			return cost;
    		}
    	private:
    		std::string name;
    		int numberOfItems;
    		int cost;
    };
    Last edited by GCNDoug; 03-26-2008 at 02:49 PM.

  11. #26
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Well after I fixed the class functions it says this for every line of the switch statement.

    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\candymachinelab. cpp(62) : error C2660: 'dispenserType::returnname' : function does not take 1 arguments
    Have to fun to class though thanks for the help I hope i can figure it out later

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    Look,
    if dispenserType::returnname does not take any arguments, then don't pass any arguments to it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    already tried, if I take it out it still gives a compiler error.

    Code:
    		case 1:
    			sellProduct (productlist[0].returnname, counter);
    			break;
    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\candymachinelab. cpp(62) : error C3867: 'dispenserType::returnname': function call missing argument list; use '&dispenserType::returnname' to create a pointer to member

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    As the error says, it's a function, not a variable.
    Functions must have () at the end, otherwise it's illegal. If it were a non-class function, you would have taken its address instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I am pretty sure that I tried it earlier with the () but I'm not sure will post results when I get a chance to compile again

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handling with Array filled with a class
    By MarlonDean in forum C++ Programming
    Replies: 18
    Last Post: 06-27-2008, 10:53 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21