Thread: using classes

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    6

    using classes

    I'm trying to make this program to add 3 user inputed populations together. Here is a summary of what it is supposed to do and the code:

    Code:
    /*	Create the programs listed below per the specifications. You may not create any additional member function. Where listed, you must use the 
    	declaration provided.
    
    1.	Write a class called AssemblyLine. This class will be used to track the production of widgets. Each instance of the assembly line will 
    	represent different assembly lines that are owned across the US. Create three instance of the AssemblyLine class to represent the factories 
    	in Rochester, Fargo, and Nelson. Create a fourth instance that will sum up all the other instances. Create the following member functions:
    a.	Two constructors. 
    b.	An input member function
    c.	An output member function
    d.	A function that will take other AssemblyLine instances and add them together. You must use the following declaration:
    	void Addit(AssemblyLine AL1)
    e.	One of the member functions must be externally defined.*/
    #include <iostream>
    using namespace std;
    ///////////////////////////////////////////////////////////////////////////////////
    class AssemblyLine
    {
    private:
    	int widgets, total;
    public:
    	AssemblyLine() : widgets(0)
    	{  }
    	AssemblyLine() : total(0)
    	{  }
    
    	void Input()
    	{
    		cout << "Enter number of widgets: ";
    		cin >> widgets;
    	}
    	void Output()
    	{
    		cout << "You entered: " << widgets;
    	}
    	void Addit(AssemblyLine AL1);
    };
    ////////////////////////////////////////////////////////////////////////////////////
    	void Addit(AssemblyLine AL1)
    	{
    		int pop1, pop1, pop3;
    		cout << "Total population is: " << AL1 = pop1 + pop2 + pop3;
    	}
    ////////////////////////////////////////////////////////////////////////////////////
    int main()
    {
    	AssemblyLine Rochester, Fargo,  Nelson, Total;
    	Rochester.Input();
    	Rochester.Output();
    	Fargo.Input();
    	Fargo.Output();
    	Nelson.Input();
    	Nelson.Output();
    	
    	Addit(Total);
    
    	int x;
    	cin >> x;
    	return 0;
    }
    I know this isn't right but I need a little guideline to get me on the right track to make this right.

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    I think

    Code:
    void Addit(AssemblyLine AL1);
    is meant to be a member function of the class? If that is the case I believe it would work something like below:

    Code:
    int main()
    {
    	AssemblyLine Rochester, Fargo,  Nelson, Total;
    	Rochester.Input();
    	Rochester.Output();
    	Fargo.Input();
    	Fargo.Output();
    	Nelson.Input();
    	Nelson.Output();
    	
    	Total.Addit(Rochester);
            Total.Addit(Fargo);
            Total.Addit(Nelson);
    
            Total.Output();
    
    	int x;
    	cin >> x;
    	return 0;
    }
    Of course I may have misread the requirements

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    Yes Addit is a member function that is defined below the class. I put in your suggestion and I got a few errors...here is what I got:
    Code:
    c:\documents and settings\kriskent1\my documents\c++\chapter 6\worksheet\number1\number1\number1.cpp(23) : error C2535: 'AssemblyLine::AssemblyLine(void)' : member function already defined or declared
    1>        c:\documents and settings\kriskent1\my documents\c++\chapter 6\worksheet\number1\number1\number1.cpp(21) : see declaration of 'AssemblyLine::AssemblyLine'
    1>c:\documents and settings\kriskent1\my documents\c++\chapter 6\worksheet\number1\number1\number1.cpp(40) : error C2086: 'int pop1' : redefinition
    1>        c:\documents and settings\kriskent1\my documents\c++\chapter 6\worksheet\number1\number1\number1.cpp(40) : see declaration of 'pop1'
    1>c:\documents and settings\kriskent1\my documents\c++\chapter 6\worksheet\number1\number1\number1.cpp(41) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'AssemblyLine' (or there is no acceptable conversion)
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(650): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(697): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(735): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(782): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(906): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(913): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(920): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(927): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(168): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(174): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(181): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(188): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(208): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(241): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(261): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(286): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(306): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(326): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(347): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(367): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(388): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(408): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(428): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(448): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\ostream(468): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, AssemblyLine)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The AddIt member function must take three arguments. They should be const references to 3 distinct AssemblyLine objects.

    This is if, believing in the assignment text, you are not expected to sum a variable number of assembly lines.

    I also suggest you do input check on the input member function. If the user enters a non-number or a value higher than the int capacity, your program will not run as expected.

    EDIT: Rereading your initial post I can see you were given an AddIt prototype which is not compatible with the above. I don't see how you are supposed to add all instances with that prototype. But then and again someone else may see past it.

    Code:
    AssemblyLine Rochester, Fargo,  Nelson, Total;
    /* ... */
    
    Total.Addit(Rochester);
    Total.Addit(Fargo);
    Total.Addit(Nelson);
    
    Total.Output();
    Is that it?
    Last edited by Mario F.; 11-22-2006 at 09:20 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    yes I tried that:
    Code:
    /*	Create the programs listed below per the specifications. You may not create any additional member function. Where listed, you must use the 
    	declaration provided.
    
    1.	Write a class called AssemblyLine. This class will be used to track the production of widgets. Each instance of the assembly line will 
    	represent different assembly lines that are owned across the US. Create three instance of the AssemblyLine class to represent the factories 
    	in Rochester, Fargo, and Nelson. Create a fourth instance that will sum up all the other instances. Create the following member functions:
    a.	Two constructors. 
    b.	An input member function
    c.	An output member function
    d.	A function that will take other AssemblyLine instances and add them together. You must use the following declaration:
    	void Addit(AssemblyLine AL1)
    e.	One of the member functions must be externally defined.*/
    #include <iostream>
    using namespace std;
    ///////////////////////////////////////////////////////////////////////////////////
    class AssemblyLine
    {
    private:
    	int widgets, total;
    public:
    	AssemblyLine() : widgets(0)
    	{  }
    	AssemblyLine() : total(0)
    	{  }
    
    	void Input()
    	{
    		cout << "Enter number of widgets: ";
    		cin >> widgets;
    	}
    	void Output()
    	{
    		cout << "You entered: " << widgets;
    	}
    	void Addit(AssemblyLine AL1);
    };
    ////////////////////////////////////////////////////////////////////////////////////
    	void Addit(AssemblyLine AL1, AL2, AL3)
    	{
    		cout << "Total population is: " << total = AL1 + AL2 + AL3;
    	}
    ////////////////////////////////////////////////////////////////////////////////////
    int main()
    {
    
    	AssemblyLine Rochester, Fargo,  Nelson, Total;
    	Rochester.Input();
    	Rochester.Output();
    	Fargo.Input();
    	Fargo.Output();
    	Nelson.Input();
    	Nelson.Output();
    	
    	Total.Addit(Rochester, Fargo, Nelson);
    
        Total.Output();
    
    	int x;
    	cin >> x;
    	return 0;
    }
    i get errors that the constructor does is not defined correctly or something.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I was going to that...

    You are defining two default constructors. You can't do that. If you feel you need total (which you really don't), your constructor should be defined like this:

    Code:
    AssemblyLine() : widgets(0), total(0) {}
    Next,

    The member that will hold the number of widgets on each class is widgets. That is the variable you have to inspect in order to sum the values. You seem to have grasped that concept when outputing its value, but not when you decided to sum them.

    So... Addit should be defined like this:

    Code:
    void Addit(AssemblyLine AL1) {
        widgets = widgets + AL1.widgets; // This object widgets is equal to it's current value plus the widgets of AL1.
    }
    You don't need total.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    you have two default constructors that have the same signature.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    thanks, it works but how do I get it to print out "total of widgets = " at the end instead of you entered?

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'll leave that to you to figure out. Sorry.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    hint: *bzzzt* "Ow!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM