I
think I am near finishing this assignment, but am having problems in the default constructor saying "the expression must be a modifiable lvalue" for line 21. I am pretty new to C++ obviously and would appreciate any tips to point me in the right direction for this and any other blaring mistakes. I will include the directions for the assignment as well.
Create a class named Student. The class should consist of the following private data members : social security number and name (last, first or first, last?). The social security number (SSN) should be a long integer. The name variable must be a character array of 80 characters.
Create the following class member functions: setSSN, getSSN, setName, getName. Each of these member 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 for the Student class. 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 class member functions.
Create a main function. In the main function 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.
Do not print from the Student class. Instead you will retrieve the data in the main() function and print from main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
include "stdafx.h"
#include <iostream>
using namespace std;
class Student
{
private:
long ssn;
char name[80];
public:
Student();
Student(char name[80], long ssn);
void setSSN(long);
long getSSN();
void setName(char *);
char getName();
};
Student::Student()
{
name = "unassigned";
ssn = 999999999;
}
Student::Student(char name[80], long ssn)
{
setSSN(ssn);
setName(name);
}
long Student::getSSN()
{
return ssn;
}
char Student::getName()
{
return name[80];
}
void Student::setSSN(long newSSN)
{
while (ssn>0)
ssn = newSSN;
}
void Student::setName(char newName[80])
{
while (strlen(newName) > 0)
name[80] = newName[80];
}
int main()
{
Student stu1;
cout << "Name for student1 is " << stu1.getName() << " and ssn is " << stu1.getSSN() << endl;
Student stu2("John Doe", 123456789);
cout << "Name for student2 is " << stu2.getName() << "and ssn is " << stu2.getSSN() << endl;
return 0;
}
|