C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2006, 09:42 PM   #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.
krazykrisi is offline   Reply With Quote
Old 11-21-2006, 11:30 PM   #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
Kurisu33 is offline   Reply With Quote
Old 11-22-2006, 09:02 AM   #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>        ]
krazykrisi is offline   Reply With Quote
Old 11-22-2006, 09:08 AM   #4
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,220
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?
__________________
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.



Last edited by Mario F.; 11-22-2006 at 09:20 AM.
Mario F. is online now   Reply With Quote
Old 11-22-2006, 09:25 AM   #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.
krazykrisi is offline   Reply With Quote
Old 11-22-2006, 09:34 AM   #6
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,220
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.


Mario F. is online now   Reply With Quote
Old 11-22-2006, 09:35 AM   #7
Ethernal Noob
 
Join Date: Nov 2001
Posts: 1,888
you have two default constructors that have the same signature.
indigo0086 is offline   Reply With Quote
Old 11-22-2006, 09:49 AM   #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?
krazykrisi is offline   Reply With Quote
Old 11-22-2006, 10:39 AM   #9
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,220
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.


Mario F. is online now   Reply With Quote
Old 11-22-2006, 10:41 AM   #10
Ethernal Noob
 
Join Date: Nov 2001
Posts: 1,888
hint: *bzzzt* "Ow!"
indigo0086 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:21 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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