Creating objects of a class and print them

can't understand the errors i got !!

Page 1 of 1

14 Replies - 2541 Views - Last Post: 15 February 2009 - 11:25 AM Rate Topic: -----

#1 dreamygirl   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Creating objects of a class and print them

Posted 15 February 2009 - 07:49 AM

Hey guys ,

I'm facing some diffculty understanding the errors i got ,although i did it the way i think its right !

anyway this is the code with the question :

#include <iostream>
#include <string>

using namespace std;

class Student{
private:
	int studentID;
	string name;
	string major;
public:
	void printStudent();
	Student(int, string, string);

};

Student::Student(int ID, string n, string m){

//implement the constuctor	

}

//implement the printStudent member function

int main(){
	//create two objects of type students and assign them different values
	//print the values of these two objects

}




and this is my code :



#include <iostream>
#include <string>

using namespace std;

class Student{
private:
	int studentID;
	string name;
	string major;
public:
	void printStudent();
	Student(int, string, string);

};

Student::Student(int ID, string n, string m){

	
	//implement the constructor

}

void Student::printStudent(int ID,string name, string major )
{
	cout<<name.Student;
	cout<<ID.Student;
	cout<major.Student;
}

   

int main(){

 Student x={006001653,MichaelBens,IST};
 Student y:{006001949,CatherineJones,BUS};

	cout<<x.printStudent()<<endl;
	cout<<y.printStudent<<endl;


return 0;

	

}



Hmm , how could I implement the constructor ? can any one suggest any values ? since it takes an integer and 2 strings


waiting :)

Is This A Good Question/Topic? 0
  • +

Replies To: Creating objects of a class and print them

#2 bszmyd   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: Creating objects of a class and print them

Posted 15 February 2009 - 08:27 AM

Just use initializers like so:
Student::Student(int ID, string n, string m) :
   studentID( ID ),
   name( n ),
   major( m )
{
}

Another thing to note is that you might want to put labels where you're printing the student out and if not newlines, then spaces so all the data doesn't run together. Missing a '<' when printing major as well.

Edit:

I also see that you're putting leading zeros on your IDs. Probably not what you want. When you put a leading zero in C/C++ it means it's in base 16 or Octal. If you need the leading zeros when you print the ID out, you'll need to make the ID a string or use a formater with the IOStream.

This post has been edited by bszmyd: 15 February 2009 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 dreamygirl   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: Creating objects of a class and print them

Posted 15 February 2009 - 08:34 AM

View Postbszmyd, on 15 Feb, 2009 - 07:27 AM, said:

Just use initializers like so:
Student::Student(int ID, string n, string m) :
   studentID( ID ),
   name( n ),
   major( m )
{
}

Another thing to note is that you might want to put labels where you're printing the student out and if not newlines, then spaces so all the data doesn't run together. Missing a '<' when printing major as well.

Edit:

I also see that you're putting leading zeros on your IDs. Probably not what you want. When you put a leading zero in C/C++ it means it's in base 16 or Octal. If you need the leading zeros when you print the ID out, you'll need to make the ID a string or use a formater with the IOStream.


ok i did it ,but I'm still facing the same problem :(

any suggestions ?
Was This Post Helpful? 0
  • +
  • -

#4 bszmyd   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: Creating objects of a class and print them

Posted 15 February 2009 - 08:43 AM

You never said what the problem was. You asked how to implement the constructor and that's what I gave you.
Was This Post Helpful? 0
  • +
  • -

#5 dreamygirl   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: Creating objects of a class and print them

Posted 15 February 2009 - 08:54 AM

I want to specify the constructor i guess this is behind the problem i have

but i dunno how.. for example i want to initialize the ID,name and major to the following " 000000,Student name ,major "
can any one tell me how can i do it ?

Student::Student(int ID, string n, string m){

	//implement the constructor
}

[/code/

[quote name='bszmyd' post='543216' date='15 Feb, 2009 - 07:43 AM']
You never said what the problem was. You asked how to implement the constructor and that's what I gave you.
[/quote]

hmm yeah , well could u plz try to run the the C++ n see the errors ?

coz i'm really confused .. there're  7 errors now 

this is my final code


[code]
#include <iostream>
#include <string>

using namespace std;

class Student{
private:
	int studentID;
	string name;
	string major;
public:
	void printStudent();
	Student(int, string, string);

};

Student::Student(int ID, string n, string m){

	
}

void Student::printStudent(int ID,string name, string major )
{
	cout<<name.Student;
	cout<<ID.Student;
	cout<<major.Student;
}

   
//implement the printStudent member function

int main(){

	Student x={6001653,MichaelBens,IST};
	Student y={6001949,CatherineJones,BUS};
cout<<x.printStudent<<endl;
cout<<y.printStudent<<endl;


return 0;

	//create two objects of type students and assign them different values
	//print the values of these two objects

}


Was This Post Helpful? 0
  • +
  • -

#6 bszmyd   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: Creating objects of a class and print them

Posted 15 February 2009 - 08:58 AM

Are you wanting default values in the constructor? Declare your constructor like this then:
Student ( int id = 0, n = string("Name"), m = string("Major") );
And your latest code doesn't have the constructor implementation I gave you earlier.

This post has been edited by bszmyd: 15 February 2009 - 09:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 dreamygirl   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: Creating objects of a class and print them

Posted 15 February 2009 - 09:01 AM

View Postbszmyd, on 15 Feb, 2009 - 07:58 AM, said:

Are you wanting default values in the constructor? Declare your constructor like this then:
Student ( int id = 0, n = string("Name"), m = string("Major") );


hmm like this ?

Student::Student(int ID, string n, string m){

Student ( int id = 0, n = string("Name"), m = string("Major") );
	
}


yeah coz it didnt work XD

This post has been edited by dreamygirl: 15 February 2009 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#8 bszmyd   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: Creating objects of a class and print them

Posted 15 February 2009 - 09:23 AM

OK here are the errors I get with the code you posted:
student.cpp:22: error: prototype for ‘void Student::printStudent(int, std::string, std::string)’ does not match any in class ‘Student’
student.cpp:12: error: candidate is: void Student::printStudent()
student.cpp: In function ‘int main()’:
student.cpp:34: error: ‘MichaelBens’ was not declared in this scope
student.cpp:34: error: ‘IST’ was not declared in this scope
student.cpp:34: error: braces around initializer for non-aggregate type ‘Student’
student.cpp:35: error: ‘CatherineJones’ was not declared in this scope
student.cpp:35: error: ‘BUS’ was not declared in this scope
student.cpp:35: error: braces around initializer for non-aggregate type ‘Student’


* Error on line 12 is because you declared printStudent to take no arguments, but the definition takes 3.
* The errors on lines 34 and 35 about not being declared are because you forgot to put quotes around your strings.
* The errors on line 34 and 35 regarding the initializer for non-aggregate type is because you used curly-braces instead of paranthesis.
* You are not calling printStudent but trying to pass it to an ostream.
* Student is not a member of string or int. I don't know what you were trying to do with code like:
cout << ID.Student;

Fixing these errors gives this code which works:
#include <iostream>
#include <string>

using namespace std;

class Student{
   private:
	  int studentID;
	  string name;
	  string major;
   public:
	  void printStudent();
	  Student(int, string, string);

};

Student::Student(int ID, string n, string m) :
   studentID( ID ),
   name( n ),
   major( m )
{


}

void Student::printStudent()
{
   cout<<name;
   cout<<studentID;
   cout<<major;
}


//implement the printStudent member function

int main(){

   Student x(6001653,"MichaelBens","IST");
   Student y(6001949,"CatherineJones","BUS");
   x.printStudent();
   y.printStudent();


   return 0;

   //create two objects of type students and assign them different values
   //print the values of these two objects

}

You'll notice that it all runs together. I suggest putting some spaces in and a newline at the end of each student.
Was This Post Helpful? 0
  • +
  • -

#9 crazyjugglerdrummer   User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Creating objects of a class and print them

Posted 15 February 2009 - 09:39 AM

Yep. I got pretty much the same thing.
#include <iostream>
#include <string>

using namespace std;

class Student{
private:
	int studentID;
	string name;
	string major;
	
public:
	void printStudent();
	Student(int, string, string);
	
};

Student::Student(int ID, string n, string m){
	studentID=ID;   // a constructor sets the classes variables to the args passed to it.
	name=n;
	major=m;
	
	
}

void Student::printStudent()   //this doesn't take any args as they're all in the class as instance variables
{
	cout<< name << endl;		//name is a member of Student, not the other way around
	cout<< studentID << endl;  //and since these are members of student, the member function knows what the are without explicitly telling it
	cout<< major << endl;
}


//implement the printStudent member function

int main(){
	
	Student x(6001653,"MichaelBens","IST");	   //you are doing this as if you were filling an array, see below. and use "" for strings
	Student y(6001949,"CatherineJones","BUS");
	x.printStudent()	;   //print student actually does the couting for you, you don't have to use the << with it, just call it
return 0;
}


some brief explanations: member functions of a class already have access to all of a class's instance variables, so you just refer to them with out . specifiers. Also, when you did that you had name.Student, which suggests that Student is a member of class name. Just backwards, thats all.

Constructors for classes are called : classname nameOfNewObject( args to constructor);
Your way would have been correct for initializing an array to a set of values.

When you call printStudent, that has all of the cout's already in it. You can do it your way if you did something like : void operator << printStudent (osstream& os); but I like the member function idea better.

Happy coding!!!
Was This Post Helpful? 0
  • +
  • -

#10 dreamygirl   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: Creating objects of a class and print them

Posted 15 February 2009 - 09:51 AM

ooh ok .. thanks alot that was really helpfull =)


I need to go through classes and read more about this ..

hmm i'm wondering , why did you put 2 braces at the end of this part of the definition:

Student::Student(int ID, string n, string m) :
   studentID( ID ),
   name( n ),
   major( m )
{


}



and to to what values do the (ID) ,(n) and (m) intialize the objects ?
Was This Post Helpful? 0
  • +
  • -

#11 bszmyd   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: Creating objects of a class and print them

Posted 15 February 2009 - 09:55 AM

I put two braces there because even though the function doesn't do anything other than initialize member variables, it still needs a definition though empty.

They initialize the objects to the values you passed in in your main function when you instantiated the object.
Was This Post Helpful? 0
  • +
  • -

#12 dreamygirl   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: Creating objects of a class and print them

Posted 15 February 2009 - 09:59 AM

View Postcrazyjugglerdrummer, on 15 Feb, 2009 - 08:39 AM, said:

Yep. I got pretty much the same thing.
#include <iostream>
#include <string>

using namespace std;

class Student{
private:
	int studentID;
	string name;
	string major;
	
public:
	void printStudent();
	Student(int, string, string);
	
};

Student::Student(int ID, string n, string m){
	studentID=ID;   // a constructor sets the classes variables to the args passed to it.
	name=n;
	major=m;
	
	
}

void Student::printStudent()   //this doesn't take any args as they're all in the class as instance variables
{
	cout<< name << endl;		//name is a member of Student, not the other way around
	cout<< studentID << endl;  //and since these are members of student, the member function knows what the are without explicitly telling it
	cout<< major << endl;
}


//implement the printStudent member function

int main(){
	
	Student x(6001653,"MichaelBens","IST");	   //you are doing this as if you were filling an array, see below. and use "" for strings
	Student y(6001949,"CatherineJones","BUS");
	x.printStudent();   //print student actually does the couting for you, you don't have to use the << with it, just call it
return 0;
}


some brief explanations: member functions of a class already have access to all of a class's instance variables, so you just refer to them with out . specifiers. Also, when you did that you had name.Student, which suggests that Student is a member of class name. Just backwards, thats all.

Constructors for classes are called : classname nameOfNewObject( args to constructor);
Your way would have been correct for initializing an array to a set of values.

When you call printStudent, that has all of the cout's already in it. You can do it your way if you did something like : void operator << printStudent (osstream& os); but I like the member function idea better.

Happy coding!!!



ooh ok now i got it .. thanks for your explanation it helped me alot
so when i include the cout's in the called function, i dont need to add a cout<< to print the objects i created ..


thanks for your time guys
Was This Post Helpful? 0
  • +
  • -

#13 dreamygirl   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: Creating objects of a class and print them

Posted 15 February 2009 - 10:07 AM

View Postbszmyd, on 15 Feb, 2009 - 08:55 AM, said:

I put two braces there because even though the function doesn't do anything other than initialize member variables, it still needs a definition though empty.

They initialize the objects to the values you passed in in your main function when you instantiated the object.


I see , well i should write down notes about all this .

thanks alot for your help =)
Was This Post Helpful? 0
  • +
  • -

#14 crazyjugglerdrummer   User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Creating objects of a class and print them

Posted 15 February 2009 - 10:25 AM

 Student::Student(int ID, string n, string m) :
   studentID( ID ),
   name( n ),
   major( m )
{


}



Its called an initializer list, and it tripped me up a great deal when I was learning. When an object has references to or members that are other objects, it has to set them to something when it starts.

What is the difference between using the : colon syntax and itializers and just using equals stuff? Would you use = for ints and primitive data and that syntax for strings and objects? Do they do different things? Could I initialize a number the same way?

This post has been edited by crazyjugglerdrummer: 15 February 2009 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#15 bszmyd   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: Creating objects of a class and print them

Posted 15 February 2009 - 11:25 AM

Quote

it has to set them to something when it starts.

Incorrect. The only thing that by default gets initialized are non-base types. Base types are left uninitialized if you don't explicitly do it yourself. If you don't explicitly initialize anything else like a string, then the default constructor is called. Unless you meant to say "you should set them to something..."

View Postcrazyjugglerdrummer, on 15 Feb, 2009 - 09:25 AM, said:

What is the difference between using the : colon syntax and itializers and just using equals stuff? Would you use = for ints and primitive data and that syntax for strings and objects? Do they do different things? Could I initialize a number the same way?

The difference is that when you use the assignment operator, for things that are non-base types, they would have already had their constructor called. In this instance both strings would be default constructed then their assignment operator is called to set their value. This is just inefficient when you can call the constructor with the string to begin with. This is similar to doing something like:
std::string name;
name = "Brian";
// Instead of:
std::string age("26");

Your compiler may or may not optimize the std::string::operator= call out. Best to just use the initializer list.

For base types where there is no default constructor then doing the initializer or assignment in the constructor function is no different. Hope that clears things up.

This post has been edited by bszmyd: 15 February 2009 - 11:51 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1