473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Declaring Class Instances

11
hello!! i am writing a program about classes and i have a few problems. my program so far looks like this:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. class Student
  8. {
  9.       private:
  10.               char lastname[40];
  11.               char firstname[40];
  12.               char id[10];
  13.               double gpa;
  14.               int score;
  15.               Student(char [], char [], char [], double , int);
  16.               void showStudent();
  17.               void setName(char [], char []);
  18.               void setID(char []);
  19.               int  setGPA(double );
  20.               int  setScore(int );
  21.               char getDecision();
  22.  
  23. };
  24.  
  25. int main()
  26. {
  27.  
  28.      Person1 = ( " Stanko", " Bree", " Z146926", " 3.3", " 1598");
  29.      Person2 = ( " Hartnett", " Josh", " Z123654", " -2.8", " 156");
  30.  
  31.      showStudent();
  32. // ...
  33.      return 0;
  34.  
in the int main, none of the functions or the person1/2 are declard. i have tried every way to d it and have looked at a 10 examples on google but it still will not compile. i am also confused with set functions. how can i chnage the name of someone??
May 1 '07 #1
14 1838
ilikepython
844 Expert 512MB
hello!! i am writing a program about classes and i have a few problems. my program so far looks like this:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. class Student
  8. {
  9.       private:
  10.               char lastname[40];
  11.               char firstname[40];
  12.               char id[10];
  13.               double gpa;
  14.               int score;
  15.               Student(char [], char [], char [], double , int);
  16.               void showStudent();
  17.               void setName(char [], char []);
  18.               void setID(char []);
  19.               int  setGPA(double );
  20.               int  setScore(int );
  21.               char getDecision();
  22.  
  23. };
  24.  
  25. int main()
  26. {
  27.  
  28.      Person1 = ( " Stanko", " Bree", " Z146926", " 3.3", " 1598");
  29.      Person2 = ( " Hartnett", " Josh", " Z123654", " -2.8", " 156");
  30.  
  31.      showStudent();
  32.      //...
  33.     return 0;
  34.  
There is also a setting called protected:. If you are interested, you could go to www.cplusplus.com's tutorial or you could ask here.
For your second question, could you please explain that a little bit more, I don't quit understand what you are asking.
May 2 '07 #2
me001
11
well i understand the protected thing..isnt that the same thing as private?? but what i was trying to say before ( sorry about that) was about the person1 and person2 and all of the function uses within main (). the errors that are coming up are only about them not being declared. where would i declare them--i though i already did. and below is my updated program. if you notice where the setName function is, i tired a couple ways but they were really long errors. do you know how to do them?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. class Student
  8. {
  9.       private:
  10.               char lastname[40];
  11.               char firstname[40];
  12.               char ID[10];
  13.               double GPA;
  14.               int Score;
  15.       public:
  16.               Student(char [], char [], char [], double , int);
  17.               void showStudent();
  18.               void setName(char [], char []);
  19.               void setID(char []);
  20.               int  setGPA(double );
  21.               int  setScore(int );
  22.               char getDecision();
  23.  
  24. };
  25.  
  26. int main()
  27. {
  28.      char lastname[40];
  29.      char firstname[40];
  30.      char ID[10];
  31.      double GPA;
  32.      int Score;
  33.   //...
  34. }
thank you!!
May 2 '07 #3
ilikepython
844 Expert 512MB
well i understand the protected thing..isnt that the same thing as private?? but what i was trying to say before ( sorry about that) was about the person1 and person2 and all of the function uses within main (). the errors that are coming up are only about them not being declared. where would i declare them--i though i already did. and below is my updated program. if you notice where the setName function is, i tired a couple ways but they were really long errors. do you know how to do them?

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

class Student
{
private:
char lastname[40];
char firstname[40];
char ID[10];
double GPA;
int Score;
public:
Student(char [], char [], char [], double , int);
void showStudent();
void setName(char [], char []);
void setID(char []);
int setGPA(double );
int setScore(int );
char getDecision();

};

int main()
{

return 0;
}


thank you!!
Oohh, I think I got it. You are saying "showStudent();", you need to specify the object like this:
Expand|Select|Wrap|Line Numbers
  1. person1.showStudent();
  2. person2.showStudent();
  3.  
That should work.
Can't believe I missed it though.

Also, try to only post the code from your porgram that is relevant to your question. You don't have to keep posting every single function.
May 2 '07 #4
me001
11
but the person1 and person2 are not part of the class. we were suppose to declare them in int main() only.

here are the directions to the program..i got most of it, but do i have to do the "calling part" two times for each person?

Declare one Student with your name, your Z-ID, and some valid GPA and score.
Declare a second Student with some name, id, and an invalid GPA and invalid score.
For the first (valid) Student:

1. Call showStudent()
2. Call setName() to change just the first name, then call showStudent() again.
3. Call setName() again, changing both names, then call showStudent() again.
4. Call setID() to change the ID, then call showStudent() again.
5. Call setScore() to change the score to a new valid score. Display the value of the return code from the function. Then call showStudent() again.
6. Call setScore() to change the score to a new invalid score. Display the value of the return code from the function. Then call showStudent() again.
7 and 8. Call setGPA() twice, once with a valid new GPA and once with an invalid one. Each time, display the value of the return code from the function and then call showStudent() again.
9. Call and display the result from getDecision().
May 2 '07 #5
AdrianH
1,251 Expert 1GB
Hi me001,

I apologise for erasing most of your code, but if you look at the posting guidelines, it states to post only relevant sections. As you are new to C++, this may be a little difficult, but here are some tips:
  • When you compile, you will be given file names and line numbers where the errors/warnings occur. Post these along with the line where the problem occurred. Actually, don’t just post the one line, but with a bit of context. The amount of context could be up to the beginning of the function with supporting class(es).
  • It is a good idea to break the problem down in to smaller pieces so that you can post something that is compliable, but contains only what is necessary to replicate the error that you are having. In fact, by doing this, you will be more than halfway there to figuring out what the problem is yourself.
  • Keep your functions small to make debugging it easier. The less irrelevant information in the function the better. In fact, a function should contain no irrelevant information, as that adds to the noise, making your function harder to read and debug (what you have done is right on the money there ;)).
  • If it becomes necessary for someone to ask you for the entire code base, please Private Message (PM) it to the party asking for it.
All of these points will actually help you in learning how to debug your code yourself.

As, for the problems you are having, ilikepython is correct about the access modifiers for the class. You need to define the interface that is to be used by all as public. Included in (but not limited to) those are your constructors. (There are times that you will want to make your constructors private, but this is not one of them).

It looks like you have some training in another language, perhaps VB where you do not have to declare a variable before use and are mixing up the syntaxes. I say that because of this:
Expand|Select|Wrap|Line Numbers
  1.      Person1 = ( " Stanko", " Bree", " Z146926", " 3.3", " 1598");
  2.  
There are also no variants in C/C++ (well not without using the C/C++ constructs). So what you need to do is declare the variable with its associated type, the construction parameters go with the variable name, like this:
Expand|Select|Wrap|Line Numbers
  1.      Student Person1( " Stanko", " Bree", " Z146926", " 3.3", " 1598");
  2.  
This same problem occurs in several other parts of your code.

Those two are the biggest problems that I see in your code. If you wish to get further help, fix those errors, recompile and try to work within the posting guidelines. Using the tips I provided will also help.

Good luck. We will be here if you need us.

Ciao,


Adrian
May 2 '07 #6
me001
11
ok--sorry about that. i am actually new to the whole thing of programming--i am an engineering major. but when i do what you said...this is the error i recieve:

no matching function for call to `Student::Student(const char[8], const char[9], const char[8], const char[4], const char[5])'

i really do not undertsand this--why are they const char[8]..when i declared them as [40],[ 40], and [10]?

thank you
May 2 '07 #7
ilikepython
844 Expert 512MB
ok--sorry about that. i am actually new to the whole thing of programming--i am an engineering major. but when i do what you said...this is the error i recieve:

no matching function for call to `Student::Student(const char[8], const char[9], const char[8], const char[4], const char[5])'

i really do not undertsand this--why are they const char[8]..when i declared them as [40],[ 40], and [10]?

thank you
In your class you declared the constructor as:
Expand|Select|Wrap|Line Numbers
  1. Student(char [], char [], char [], double, int);
  2.  
but I believe you called the last two arguements as strings. Try calling them without the quotes and see what happens.
The fact that the compilers says the char [] has less items than 40 just means that you called it with less than 40.
May 2 '07 #8
AdrianH
1,251 Expert 1GB
ok--sorry about that. i am actually new to the whole thing of programming--i am an engineering major. but when i do what you said...this is the error i recieve:

no matching function for call to `Student::Student(const char[8], const char[9], const char[8], const char[4], const char[5])'

i really do not undertsand this--why are they const char[8]..when i declared them as [40],[ 40], and [10]?

thank you
That is because there is no Student constructor with the signature you are using.

Student(char [], char [], char [], double , int); //< This is the signature

Student Person1( " Stanko", " Bree", " Z146926", " 3.3", " 1598"); //< This has 5 char arrays.

The sizes of the arrays are coming from the call. A C-string contains one char per letter plus one char for the terminating null character. Get rid of the quotes around your numbers, and the the call will match the signature.

I may not be in from the rest of the night. Don't worry, there are other people in the group who are capable like ilikepython.

Ciao,


Adrian
May 2 '07 #9
me001
11
When I do that I have to declare every single thing within the parenthesis. The two people should be declared separately from the constructor as I showed within the instructions above. Right now I am just very confused, but thank you for your quick responses. I appreciate it.
May 2 '07 #10
ilikepython
844 Expert 512MB
When I do that I have to declare every single thing within the parenthesis. The two people should be declared separately from the constructor as I showed within the instructions above. Right now I am just very confused, but thank you for your quick responses. I appreciate it.
You declare the "people" in main; they are instances of your Student class. You use the constructor to initialize their varaibles (lastname, firstname etc...). This:
Expand|Select|Wrap|Line Numbers
  1. Student myStudent("john", "smith", "Z1758294, 3.4, 1589);
  2.  
makes an instance of the Student class. (using the constructor).
The paranthesis indicate the arguements passed to the constructor.
I'm sorry if I didn't explain this correctly or if I didn't understand your question, so if you need more help, just post here.
May 2 '07 #11
me001
11
thats what i have had the whole time but this is the error message i recieve:
no matching function for call to `Student::Student(const char[7], const char[8], const char[8], const char[4], const char[5])'

candidates are: Student::Student(const Student&)

Student::Student(char*, char*, char*, double, int)
May 2 '07 #12
ilikepython
844 Expert 512MB
thats what i have had the whole time but this is the error message i recieve:
no matching function for call to `Student::Student(const char[7], const char[8], const char[8], const char[4], const char[5])'

candidates are: Student::Student(const Student&)

Student::Student(char*, char*, char*, double, int)
Oh I'm sorry that's a typo on my part. Try this:
Expand|Select|Wrap|Line Numbers
  1. Student student("john", "smith", "Z17491", 3.4, 1589);
  2.  
I forgot the closing quote on the ID number.
May 2 '07 #13
me001
11
i know i already had it--i am still getting the same error message.
May 2 '07 #14
AdrianH
1,251 Expert 1GB
i know i already had it--i am still getting the same error message.
Ok, can you post that part of main() which creates the instances of Student? I'm pretty sure what we have here is a failure to communicate. ;)


Adrian
May 2 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
6
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I wanted add one more demonstration to the mix. ...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
4
by: Ant | last post by:
Hi I'm quite new to C#. I've been playing around with variables declared outside the scope of a method with & without the static keyword. What difference does declaring variables in either fashion...
2
by: Zardoz | last post by:
I've created a new header file in which one will find : #using <mscorlib.dll> using namespace System; __gc class G { public: int k; int sum(int){return 0;}
3
by: Peter Oliphant | last post by:
OK. I created a managed class. Now I want to create a global pointer to an instance of the class that the entire application can reference. Yet, when I do something like: MyManagedClass* ...
18
by: Nathan | last post by:
If you're wondering why I post so many questions, it's because I want to make an entry in the Guinness Book of World Records. But because I post so many, I try to make them simple. Here is (I...
15
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here...
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
5
by: Michael Bell | last post by:
I have been going through the textbooks, and they all seem to dodge what I see as a necessary question: How do you create multiple instances of the same class? The problem is that these are...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.