Help with C++ code using classes and pointers.?
-
I have an assignment a C++ assignment and here is my code so far; #include <iostream.h> #include <stdlib.h> #include <cstring> #include <cctype> class Student { private: char Name[80]; long SSN ; public: Student (char, long); void setSSN (int SSN); void setName (int Name); int getSSN (long); int getName (char); }; Student::Student (char Name, long SSN) { char Name [80] = "unassigned"; long SSN = 999999999; } int Student::getName() { return Name; } int Student::getSSN() { return SSN; } void Student::setName(int name) { int name = "John Doe" } void Student::setSSN(int ssn) { int ssn = 123456789 } void printName(Student *nameprint); void updateName(Student *nameprint); void printSSN(Student *ssnprint); void updateSSN(Student *ssnprint); int main() { Student myStudent; Student mySSN; printName(&myStudent); updateName(&myStudent); printName(&myStudent); printSSN(&mySSN); updateSSN(&mySSN); printSSN(&mySSN); return 0; } void printName(Student *dName) { cout << dName->getName( ) << endl; } void printSSN(Student *dSsn) { cout << dSsn->getSSN( ) << endl; } void updateName(Student *nameprint) { namePrint->setName(int name); } void updateSSN(Student *ssnprint) { namePrint->setSSN(int ssn); } I need it to do this: Create a class named Student. The class should consist of the following private member variables: social security number and name (last, first or first, last?). The social security number (SSN) should be a long integer. The name variable should be a character array of 80 characters. (This means use a C-style string only. You may not use a string/String class anywhere.) Create the following class member functions: setSSN, getSSN, setName, getName. Each of the functions should be public. The setSSN function should accept 1 argument and update the the social security number member variable. Do not allow the the social security number to be set to zero or less than zero. The getSSN should return the class SSN. The setName member function should accept one string argument. Use the argument to update the name class member variable. Do not update the class variable for name if the argument has a length of 0. (This indicates the name in the argument is "empty".) The getName method should return the class value for name. Create a default constructor. (This constructor will accept no arguments.) Use the default constructor to initialize the social security number to 999999999 and the name to "unassigned". Make sure all your methods are defined in the implementation section of the class. Do not use any inline member functions. Do not print from the Student class. Instead retrieve the data in the main() function and print from main. Create a main function. In the main method create two Student objects. Use the appropriate get functions to print all the values of all the member variables for the first Student object. For the second object, use the set methods to change the student name to John Doe and the social security number to 123456789. Use the appropriate get functions to print all the values of all the member variables for the second Student object. I am having MANY issues, what am I doing wrong??
-
Answer:
You need to take this step by step, one function at a time. Your instructor has already laid out your header file for you. You just need to work on getting each requirement filled one at a time. 1. Your constructor is not a default constructor. A default constructor takes no arguments, yours has two. 2. Your GetName is returning an int, but your string is an array of char. What you need to do is allocate memory for a temporary string, and copy the data in the name field over to it, and return the address as a pointer like so: char * Student::GetName() { char *Tmp = new char[80]; int i = 0; do { Tmp[i] = Name[i++]; }while (Name[i-1]!=0); return Tmp; }; 3. SetName, same Problem, similar solution. Your function is expecting an int when you are supposed to be passing a string (char *). Also in your set name, your are setting it to the same string value every time, instead of setting it to the passed string. 4. SetSSN, You need to implement a check for values less or equal to 0. Also, same situation as your Set Name, you are setting each and every instance of students' SSN to 123456789 with every call to SetSSN 5. Since all of your non-class functions are only one line each anyway, just put them into the main function any way like so and omit the function declarations for the print and update functions: int main() { Student S1,S2; cout<<S1.GetName()<<endl; cout<<S1.GetSSN()<<endl; S2.SetName("John Doe"); S2.SetSSN(123456789); cout<<S2.GetName()<<endl; cout<<S2.GetSSN()<<endl; } This should get you up and running.
Yahoo! Answers Visit the source
Other answers
I wish i could help you..i am taking C++ too and having alot of trouble understanding it myself... but then again my teacher isn't very good at teaching the material.. He likes to give alot of work without teaching the knowledge necessary to do the work...
Related Q & A:
- How you format your code using phone?Best solution by Meta Stack Overflow
- What is the problem with pointers in c?Best solution by Stack Overflow
- How to convert Matlab .m file to C code?Best solution by mathworks.com
- Help with c++ grading program.Best solution by Yahoo! Answers
- Can anyone help with a code for a ford fiesta radio?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.