14 Replies - 636 Views - Last Post: 23 November 2009 - 08:32 PM Rate Topic: -----

#1 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Using constructors in classes C++

Posted 23 November 2009 - 02:59 PM

I am having problems using constructors in my program. I don't really know a lot about constructors. I searched multiple places and tried to figure out how to use them myself, but had no success. I was hoping someone here could help me. Examples, explanations, visuals, links, anything would be helpful. There are three separate code files for my program. They are sequence.cpp, sequenceFunctions.cpp, and sequence.h. Here's my code:

sequence.cpp
#include "sequence.h"

int main()
{
	Sequence go;   
	int size;
	int element = 0;
	vector<int> x;
	vector<int> y;

	go.read(x, y, size);		 
	go.print(x, y);
	go.add(x, y);
	go.subtract(x, y);
	go.multiply(x, y, element);
	go.equal(x, y);
}



sequenceFunctions.cpp
#include "sequence.h"

void Sequence::read(vector<int> &x, vector<int> &y, int size)
{
	int tempInt;

	cout << "Please enter the number of integers you want for vector x: ";
	cin >> size;
	cout << "Please enter your integers: ";
	for (int a = 0; a < size; a++)
	{
		cin >> tempInt;
		x.push_back(tempInt);
	}

	cout << "Please enter the number of integers you want for vector y: ";
	cin >> size;
	cout << "Please enter your integers: ";
	for (int a = 0; a < size; a++)
	{
		cin >> tempInt;
		y.push_back(tempInt);
	}
}

void Sequence::print(vector<int> x, vector<int> y)
{
	cout << "This is the sequence for vector x: (";
	for (int a = 0; a < (x.size() - 1); a++)
		cout << x[a] << ", ";
	cout << x[x.size() - 1] << ")" << endl;

	cout << "This is the sequence for vector y: (";
	for (int b = 0; b < (y.size() - 1); b++)
		cout << y[b] << ", ";
	cout << y[y.size() - 1] << ")" << endl;
}

void Sequence::putelement(vector<int> &y, int element)
{
	y.insert(y.begin(), element);
}

void Sequence::add(vector<int> x, vector<int> y)
{
	if (x.size() > y.size())
		y.resize(x.size());
	else if (y.size() > x.size())
		x.resize(y.size());

	cout << "Vector x + vector y = (";
	for (int a = 0; a < (x.size() - 1); a++)
		cout << x[a] + y[a] << ", ";
	cout << x[x.size() - 1] + y[x.size() - 1] << ")." << endl;
}  

void Sequence::subtract(vector<int> x, vector<int> y)
{
	if (x.size() > y.size())
		y.resize(x.size());
	else if (y.size() > x.size())
		x.resize(y.size());

	cout << "Vector x - vector y = (";
	for (int a = 0; a < (x.size() - 1); a++)
		cout << x[a] - y[a] << ", ";
	cout << x[x.size() - 1] - y[x.size() - 1] << ")." << endl;
}

void Sequence::multiply(vector<int> x, vector<int> y, int element)
{
	Sequence go;
	char answer;
	cout << "Would you like to multiply by a constant? (y/n): ";
	cin >> answer;				
	while (answer == 'y')
	{				  
		char vectorChoice;   
		int integerChoice;
		cout << "Which vector would you like to use? (x/y): ";
		cin >> vectorChoice;
		cout << "Please choose an integer: ";	 
		cin >> integerChoice;
		if (vectorChoice == 'x')
		{
			cout << "Vector x * " << integerChoice << " = (";
			for (int a = 0; a < (x.size() - 1); a++)
				cout << x[a] * integerChoice << ", ";
			cout << x[x.size() - 1] * integerChoice << ")." << endl;
			cout << "Would you like to multiply by a constant again? (y/n): ";
			cin >> answer;   
		}
		else
		{
			cout << "Vector y * " << integerChoice << " = (";
			for (int b = 0; b < (y.size() - 1); b++)
				cout << y[b] * integerChoice << ", ";
			cout << y[y.size() - 1] * integerChoice << ")." << endl;
			cout << "Would you like to multiply by a constant again? (y/n): ";
			cin >> answer;
		}
	}
	cout << "Would you like to multiply vectors x and y together? (y/n): ";
	cin >> answer;			   
	if (answer == 'y')	 
	{
		vector<int> temp1;			
		vector<int> temp2;				  
		vector<int> temp3;		  
		for (int a = 0; a < x.size(); a++)
		{
			temp1.clear();
			for (int b = 0; b < y.size(); b++)	   
				temp1.push_back(x[a] * y[b]);
			temp3.resize(temp1.size());
			temp2 = temp3; 
			temp3.clear();	   
			for (int c = 0; c < temp1.size(); c++)
				temp3.push_back(temp1[c] + temp2[c]);
			go.putelement(y, element);
		}
		cout << "Vector x * vector y = (";
		for (int d = 0; d < (temp3.size() - 1); d++)		  
			cout << temp3[d] << ", ";
		cout << temp3[temp3.size() - 1] << ")." << endl;
	}
}

bool Sequence::equal(vector<int> x,vector<int> y)
{
	if (x == y)				   
		cout << "Vector x equals vector y." << endl;
	else			   
		cout << "Vector x does not equal vector y." << endl;
}



sequence.h
#ifndef SEQUENCE_H_
#define SEQUENCE_H_

#include <iostream>
#include <vector>

using namespace std;

class Sequence
{
	public:
		void read(vector<int> &x, vector<int> &y, int size);
		void print(vector<int> x, vector<int> y);
		void putelement(vector<int> &y, int element);
		void add(vector<int> x, vector<int> y);
		void subtract(vector<int> x, vector<int> y);
		void multiply(vector<int> x, vector<int> y, int element);
		bool equal(vector<int> x, vector<int> y);
};

#endif



Thanks in advance for the help guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Using constructors in classes C++

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Using constructors in classes C++

Posted 23 November 2009 - 03:07 PM

Forgive me if this sounds assholish, but I have a hard time believing you wrote all that code and don't know what a constructor is.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Using constructors in classes C++

Posted 23 November 2009 - 03:08 PM

What's the problem? You've not defined an constructor for your class, so the compiler is generating one to use as a default.

A constructor shouldn't do anything more than to initialize the class into a known, default state. Where your class doesn't even seem to have any member variables, you don't even NEED a constructor.
Was This Post Helpful? 0
  • +
  • -

#4 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: Using constructors in classes C++

Posted 23 November 2009 - 03:22 PM

View PostKYA, on 23 Nov, 2009 - 02:07 PM, said:

Forgive me if this sounds assholish, but I have a hard time believing you wrote all that code and don't know what a constructor is.


The only thing he doesn't understand is constructors. Come on, it's not like you have to use constructors to code vectors, not at all.
Was This Post Helpful? 0
  • +
  • -

#5 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Re: Using constructors in classes C++

Posted 23 November 2009 - 03:29 PM

View PostKYA, on 23 Nov, 2009 - 02:07 PM, said:

Forgive me if this sounds assholish, but I have a hard time believing you wrote all that code and don't know what a constructor is.


That gave me a good laugh, but I completely understand you thinking that. I do know what a constructor is and what it's supposed to do, but I wasn't sure if I was using one or even needed to use one. It's alright if you don't believe me, but I did really write the code.

View PostJackOfAllTrades, on 23 Nov, 2009 - 02:08 PM, said:

What's the problem? You've not defined an constructor for your class, so the compiler is generating one to use as a default.

A constructor shouldn't do anything more than to initialize the class into a known, default state. Where your class doesn't even seem to have any member variables, you don't even NEED a constructor.


The only reason I was asking about this is because of what my lab instructor said. I know the code runs and compiles. I fine tuned it and it works great (took me a good 4 hours, but I did it while watching the Puppet Master I and II :) ). I'm just going to assume I heard him wrong (he does have a bad accent) because I know you guys know what you're talking about. But really quick, if I was setting up a default constructor, I would set it up doing something like this, right?
Sequence::Sequence()
{
	size = 0;
	element = 0;
}


And I could include vectors if I wanted as well. Then, to use the default constructor, I would just call like this: Sequence start; right?


And, I was going to do a separate posting for this, but I'll just ask now. I need to overload operators '+', '-', '==', '<<', '>>', and '*'. How would I do this? Also, I was told that the '*' operator couldn't be overloaded...is this true?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Using constructors in classes C++

Posted 23 November 2009 - 03:36 PM

Like KYA and ccubed sort of implied, one would usually know how to write a constructor well before venturing into vectors and the Standard Template Library.

I don't know what the purpose of your class is here, so it's hard to say what you would want to do moving forward. If you wanted a constructor like that, you would need to make those variables member variables to the class, obviously.

EDIT: OK, so you're doing operations on two vectors. Those two things (the vectors) are the only things that need to be member variables. I don't know what "element" is being used for, but size is not necessary outside of the initialization process (when the user enters the vector elements), and therefore is not required outside of that function.
Was This Post Helpful? 0
  • +
  • -

#7 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Re: Using constructors in classes C++

Posted 23 November 2009 - 04:00 PM

First off, this is the first assignment we've had that uses constructors. Second, my professor is useless, so I've had to teach myself everything. That's why I'm missing things like this. Since this assignment dealt a lot with vectors, I taught myself how to use them at http://www.cplusplus...nce/stl/vector/.

Now, since the 2 vectors x and y are the only members I need, I'm assuming I would be correct to declare them private. After I do that, would this be correct:
Sequence::Sequence()
{
x.resize(0);
y.resize(0);
}

I tried this before, and when I did it caused a lot of problems. The compiler was saying that x and y weren't declared in sequence.cpp and it was saying the same thing in sequenceFunctions.cpp. Am I missing something?

Oh yeah, when I declare those private, push_back and size won't work. I'll continue to work on it. I'll post newer code if I make some progress.

This post has been edited by mgrande: 23 November 2009 - 04:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Using constructors in classes C++

Posted 23 November 2009 - 04:03 PM

You don't need to size the vector. Just push_back when you have data.

An updated code listing would be helpful.
Was This Post Helpful? 0
  • +
  • -

#9 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Re: Using constructors in classes C++

Posted 23 November 2009 - 04:10 PM

I'll update my code in a minute...I'm trying something that may work.
Was This Post Helpful? 0
  • +
  • -

#10 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Re: Using constructors in classes C++

Posted 23 November 2009 - 04:46 PM

As promised, here's my new code:

sequence.cpp
#include "sequence.h"

int main()
{
	Sequence start;
	Sequence go;
	int size;
	int element = 0;

	read(go, size);
	start.print(go);
	start.add(go);
	start.subtract(go);
	start.multiply(go, element);
	start.equal(go);
}



sequenceFunctions.cpp
#include "sequence.h"

Sequence go;

Sequence::Sequence()
{
	x.resize(0);
	y.resize(0);
}

void read(Sequence &go, int size)
{
	int tempInt;

	cout << "Please enter the number of integers you want for vector x: ";
	cin >> size;
	cout << "Please enter your integers: ";
	for (int a = 0; a < size; a++)
	{
		cin >> tempInt;
		go.x.push_back(tempInt);
	}

	cout << "Please enter the number of integers you want for vector y: ";
	cin >> size;
	cout << "Please enter your integers: ";
	for (int a = 0; a < size; a++)
	{
		cin >> tempInt;
		go.y.push_back(tempInt);
	}
}

void Sequence::print(Sequence go)
{
	cout << "This is the sequence for vector x: (";
	for (int a = 0; a < (go.x.size() - 1); a++)
		cout << go.x[a] << ", ";
	cout << go.x[go.x.size() - 1] << ")" << endl;

	cout << "This is the sequence for vector y: (";
	for (int b = 0; b < (go.y.size() - 1); b++)
		cout << go.y[b] << ", ";
	cout << go.y[go.y.size() - 1] << ")" << endl;
}

void Sequence::putelement(Sequence &go, int element)
{
	go.y.insert(go.y.begin(), element);
}

void Sequence::add(Sequence go)
{		 
	if (go.x.size() > go.y.size())
		go.y.resize(go.x.size());
	else if (go.y.size() > go.x.size())
		go.x.resize(go.y.size());

	cout << "Vector x + vector y = (";
	for (int a = 0; a < (go.x.size() - 1); a++)
		cout << go.x[a] + go.y[a] << ", ";
	cout << go.x[go.x.size() - 1] + go.y[go.x.size() - 1] << ")." << endl;
}			   

void Sequence::subtract(Sequence go)
{
	if (go.x.size() > go.y.size())
		go.y.resize(go.x.size());
	else if (go.y.size() > go.x.size())
		go.x.resize(go.y.size());

	cout << "Vector x - vector y = (";
	for (int a = 0; a < (go.x.size() - 1); a++)
		cout << go.x[a] - go.y[a] << ", ";
	cout << go.x[go.x.size() - 1] - go.y[go.x.size() - 1] << ")." << endl;
}

void Sequence::multiply(Sequence go, int element)
{
	char answer;
	cout << "Would you like to multiply by a constant? (y/n): ";
	cin >> answer;
	while (answer == 'y')		 
	{
		char vectorChoice;
		int integerChoice;	  
		cout << "Which vector would you like to use? (x/y): ";
		cin >> vectorChoice;
		cout << "Please choose an integer: ";
		cin >> integerChoice;	
		if (vectorChoice == 'x')
		{
			cout << "Vector x * " << integerChoice << " = (";
			for (int a = 0; a < (go.x.size() - 1); a++)
				cout << go.x[a] * integerChoice << ", ";
			cout << go.x[go.x.size() - 1] * integerChoice << ")." << endl;
			cout << "Would you like to multiply by a constant again? (y/n): ";
			cin >> answer;
		}
		else
		{
			cout << "Vector y * " << integerChoice << " = (";
			for (int b = 0; b < (go.y.size() - 1); b++)
				cout << go.y[b] * integerChoice << ", ";
			cout << go.y[go.y.size() - 1] * integerChoice << ")." << endl;
			cout << "Would you like to multiply by a constant again? (y/n): ";
			cin >> answer;
		}
	}	 
	cout << "Would you like to multiply vectors x and y together? (y/n): ";
	cin >> answer;
	if (answer == 'y')			
	{
		vector<int> temp1;
		vector<int> temp2;	  
		vector<int> temp3;
		for (int a = 0; a < go.x.size(); a++)
		{
			temp1.clear();	   
			for (int b = 0; b < go.y.size(); b++)
				temp1.push_back(go.x[a] * go.y[b]);
			temp3.resize(temp1.size());
			temp2 = temp3;
			temp3.clear();
			for (int c = 0; c < temp1.size(); c++)
				temp3.push_back(temp1[c] + temp2[c]);
			go.putelement(go, element);
		}
		cout << "Vector x * vector y = (";
		for (int d = 0; d < (temp3.size() - 1); d++)
			cout << temp3[d] << ", ";
		cout << temp3[temp3.size() - 1] << ")." << endl;
	}
}

bool Sequence::equal(Sequence go)
{
	if (go.x == go.y)
		cout << "Vector x equals vector y." << endl;
	else
		cout << "Vector x does not equal vector y." << endl;
}



sequence.h
#ifndef SEQUENCE_H_
#define SEQUENCE_H_

#include <iostream>
#include <vector>

using namespace std;

class Sequence
{
	public:
		Sequence();
		friend void read(Sequence &go, int size);
		void print(Sequence go);
		void putelement(Sequence &go, int element);
		void add(Sequence go);
		void subtract(Sequence go);
		void multiply(Sequence go, int element);
		bool equal(Sequence go);
	private:
		vector<int> x;
		vector<int> y;
};

#endif



I still need help with overloading operators +, -, ==, <<, >>, and * (I was told * can't be overloaded...is this true?). Can anyone help with this?
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Using constructors in classes C++

Posted 23 November 2009 - 05:10 PM

Why do you have an object in the cpp file? You do the operations on the object itself, not on some instance outside of main.
Was This Post Helpful? 0
  • +
  • -

#12 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Re: Using constructors in classes C++

Posted 23 November 2009 - 05:26 PM

I have 2 cpp files, but I think I know what you're talking about and I fixed that. It wasn't causing any errors, but it was a good point to change that. Can you help me with overloading operators at all, or no?
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Using constructors in classes C++

Posted 23 November 2009 - 05:32 PM

I'll do a couple:

ostream& operator <<(ostream&out, const Sequence& seq){
   //display vectors here, iterate or whatever
}


bool operator==(const Sequence&){
	//how are they to be compared?
   //vector items?
   //compare each index and return false if they don't amtch
}




Most of them require you to determine how exactly you want to proceed/display data/input it/etc... I can't read your mind :)
Was This Post Helpful? 0
  • +
  • -

#14 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Re: Using constructors in classes C++

Posted 23 November 2009 - 05:35 PM

Cool, this helps. I'll mess around with it some and post what I get in a little bit. Thanks again KYA.
Was This Post Helpful? 0
  • +
  • -

#15 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Re: Using constructors in classes C++

Posted 23 November 2009 - 08:32 PM

Alright. I finally got it. Thanks for your help guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1