In: Computer Science
Programming assignment 4 : C++
Write a program to do the following:
1.Define a structure to store a date, which includes day(int), month(int), and year(int).
2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string).
3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double). And it also has public member functions: constructors(at least one default and one non-default), Accessors and Mutators for each member variable, and so on.(write definitions of at least one member function outside the class).
4.Test each member function of the class in main function. Each piece information of a student is inputted by user, not initialized by you (programmer)except for default constructor.5.In main function, choose any one student you defined,and then check whether the student lives in the zip code of 11235 (close to school)or not,and check whether the student’s birthday is in November or not.
CODE:
#include <iostream>
#include <string>
using namespace std;
struct date //structure to store a date
{
int day;
int month;
int year;
};
struct address //structure to store an address
{
string house_street;
string city;
string state;
string zip_code;
};
class Student //class student to store student details
{
string name;
int id;
struct date dob;
struct address address;
int totalCreditEarned;
double GPA;
public:
Student(string n, int i = -1) //constructor
{
name = n;
id = i;
}
//all getter setter methods prototypes
void setName(string n);
void setId(int i);
void setDate(int d, int m, int y);
void setAddress(string h, string c, string s, string z);
void setTotalCreditEarned(int t);
void setGPA(double g);
string getName();
int getId();
struct date getDate();
struct address getAddress();
int getTotalCreditEarned();
double getGPA();
};
//all the function definitions declared in the class Student
void Student::setName(string n)
{
name = n;
}
void Student::setId(int i)
{
id = i;
}
void Student::setDate(int d, int m, int y)
{
dob.day = d;
dob.month = m;
dob.year = y;
}
void Student::setAddress(string h, string c, string s, string z)
{
address.house_street = h;
address.city = c;
address.state = s;
address.zip_code = z;
}
void Student::setTotalCreditEarned(int t)
{
totalCreditEarned = t;
}
void Student::setGPA(double g)
{
GPA = g;
}
string Student::getName()
{
return name;
}
int Student::getId()
{
return id;
}
struct date Student::getDate()
{
return dob;
}
struct address Student::getAddress()
{
return address;
}
int Student::getTotalCreditEarned()
{
return totalCreditEarned;
}
double Student::getGPA()
{
return GPA;
}
int main()
{
Student student("Raven"); //object with the name Raven
string h, c, s, z;
int d, m, y;
int id;
int credit;
double gpa;
char temp;
cout << "\nEnter the ID: "; //entering ID
cin >> id;
cout << "\nEnter the Birthday: "; //Entering birthday
cout << "\n\tDay: ";
cin >> d;
cout << "\tMonth: ";
cin >> m;
cout << "\tYear: ";
cin >> y;
getchar(); //since next value input is a char, it will also take in the char value of the enter key from the previous input
cout << "\nEnter the Address: ";
cout << "\n\tHouse & Street: ";
getline(cin, h); //reading string as home and steeet
cout << "\tCity: ";
getline(cin, c); //reading string for city
cout << "\tState: ";
getline(cin, s); //reading string for state
cout << "\tZip Code: "; //reading string for zip
getline(cin, z);
cout << "\nEnter the Total Credits Earned: "; //getting total credits earned
cin >> credit;
cout << "\nEnter the GPA: "; //entering gpa
cin >> gpa;
//we will set all the values here to the object
student.setId(id);
student.setDate(d, m, y);
student.setAddress(h, c, s, z);
student.setTotalCreditEarned(credit);
student.setGPA(gpa);
cout << "\n------------------------------------------------------------\n";
if (student.getAddress().zip_code == "11235") //checking if the struct address's zip code is 11235
{
cout << "\nYes! The student " << student.getName() << "'s ZIP CODE IS 11235";
}
else
{
cout << "\nNo! The student " << student.getName() << "'s ZIP CODE IS NOT 11235";
}
if (student.getDate().month == 11) //checking if the struct date's month value is 11
{
cout << "\nYes! The student " << student.getName() << "'s BIRTH MONTH IS NOVEMBER";
}
else
{
cout << "\nNo! The student " << student.getName() << "'s BIRTH MONTH IS NOT NOVEMBER";
}
cout << endl
<< endl;
}
OUTPUT: