using constuctors and destructors

I have to label a pc and im just trying to get the basic down. later I have to put in a disturctor and have come down to one error. any advice on this i have been at this program for hours!







#include <iostream>
using namespace std;
class fraction {
public:
fraction ( char pc, char amd) {make = pc; model = amd;}
void print(){ cout << "pc" << '/' << "amd";}
private:
char make, model;
};

char main (){
fraction make (pc);
cout << "x ="; make.print ();
cout << endl;
system ("pause");
return 0;
}

Your main() is returning a char...it needs to return an int. Also, post the error next time and use [code][/code] tags.
1) Please use code tags when posting code. See the '#' button next to the textbox.

2) your 'print' function actually prints the strings "pc" and "amd". That is, your output would be "pc/amd". You probably meant to output make and model. For that: cout << make << '/' << model;. Notice there's no quotes around make and model. If you put quotes around them you get a literal string (ie: the actual word "make")

3) main needs to return an int, not a char.

fraction make (pc); <--- regarding this line:

4) 'pc' is not a variable name.

5) the 'fraction' ctor takes 2 parameters... you're only giving it one.
thanks for the help so far here is what I came up with it is still wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
class computer {
public:
	computer ( int pc, int amd) {make = pc; processor = amd;} 
	void print(){ cout << pc << '/' << amd;}
private:
	char make, model;
};

int main (){ 
	computer x(pc), y(amd)  ;
	cout << "make ="; x.print ();
	cout << "processor ="; y.print ();
	cout << endl;
	system ("pause");
	return 0;
}


you guys are a big help to me. I am actually trying to print pc and amd the program is supposeto say here is your make of the computer = pc, processor = amd.

I really dont understand how to do this line? fraction make (pc) more explanation would help and here is my list of errors
1
2
3
4
.\question two.cpp(5) : error C2065: 'processor' : undeclared identifier
.\question two.cpp(6) : error C2065: 'pc' : undeclared identifier
.\question two.cpp(6) : error C2065: 'amd' : undeclared identifier
 


if you guys could just tell me how to fix it instead of writing the program so it is my work I would appreciate it.
Thanks!
Last edited on
Line 5: Think carefully when giving names. The correct member is computer::model, not computer::processor.
Line 6: pc and amd are variables local to the constructor of computer. You can only use them there. You assigned their values to make and model, didn't you? Use them.
Lines 12: There are two undeclared variables in use on this line. See if you can find them yourself.
yea the undeclaired are pc and amd how would I delclair it in a way so it prints pc and amd? because I thought I already declaired it above on line 5 thats whats confusing me.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;
class computer {
public:
	computer ( int pc, int amd) {make = pc; model = amd;} 
	void print(){ cout << make << '/' << model;}
private:
	char make, model;
};

int main (){ 
	computer x(pc), y(amd);
	cout << "make ="; x.print ();
	cout << "processor ="; y.print ();
	cout << endl;
	system ("pause");
	return 0;
}
EDIT -- line numbers in this post are referring to the line numbers in your second post in the thread, not your most recent one.

EDIT again: if you want to use std::string, you need to #include <string>. Also because you're using namespace std; you can just use string instead of typing out std::string.

1
2
3
4
5
6
// different variable types hold different types of variables
//  example
int anInteger;  //  for holding numbers (integers only, no floating point), ie:  1,2,-5, but not 1.5
float aFloat;  //  for holding floating point numbers.  ie:  1.5, 1.7, etc
char aCharacter;  // for holding an INDIVIDUAL character.  ie:  'c', 'a', 'B'
std::string aString;  // for holding full strings.  ie:  "foo", "bar" 


If you want your Computer class to retain information about the computer, think about what kind of variables you want to use.

Also if you want to use a string literal (ie, you want it to actually SAY "pc" or you want to assign the string "pc" to something) you need to encase the string in quotes.

1
2
cout << blah;  // prints the contents of a variable named blah
cout << "blah";  // prints the actual word blah 


Double quotes " go around strings, and single quotes ' go around chars.



I really dont understand how to do this line? fraction make (pc) more explanation would help


This is a constructor (aka, 'ctor'). A constructor is just like any other function, only it is called when an object is created. In your code, your ctor is on line 5. It takes two parameters, pc and amd. If you want to call that constructor, you need to supply values for each of those.

On line 12 you're making two Computer objects, and giving 1 parameter to each of their ctors (not 2!). You're basically creating two computers, one with make=pc, and another with make=amd (but the model for each is never specified)

To call the ctor properly, you need to give it two parameters:

 
computer mycomp( /*put desired make here*/ , /*put desired model here*/ );


That will create a 'mycomp' computer object and will call the computer ctor, which will set up your make and model.


Last edited on
Topic archived. No new replies allowed.