Solved

C++ programming using classes - VERY URGENT

Posted on 2006-11-21
16
416 Views
Last Modified: 2013-11-23
This is the point of this program:

Develop a set of classes to represent all of the people that would be of interest to a university or college.  You should model this after your own institution, and you must have at least four classes.  You will have a base class that represents all people, and at least three classes will be derived from this base class, representing students, staff and faculty.  Develop a small program that instantiates and displays objects of all three derived classes.  Remember that the idea behind derived classes is they have their own attributes and methods (specific to them).

I am almost positive that my program is perfect (according to these standards), but I just want it double checked because it is worth a lot of points.
Thank you

Here is my complete code:


#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>

using namespace std;

//base class the other classes are derived from
class Baker
{
//declaring variables
private:
      string yourName;

//calling methods
public:
      void welcomeUser();      
      void setYourName(string yourName);
      void readInputFromUser();
      void displayBakerInfo();
};

//welcome message for user
void Baker::welcomeUser()
{
      cout << endl; //prints empty line
      cout << "Welcome to the program, have a great day." << endl;
}

void Baker::setYourName(string yourName)
{
      Baker::yourName = yourName;
}

//reading input from user
void Baker::readInputFromUser()
{
      string localyourName;
      cout << "Please enter your name: ";
      cin >> localyourName;
      yourName = localyourName;
}

//displaying output
void Baker::displayBakerInfo()
{
      cout << endl; //prints empty line
      cout << "Your name is: " << yourName << endl;
}


//student class derived from base class Baker
class Students: public Baker
{
//declaring variables
private:
      string studentName;
      int studentNumber;
      string studentMajor;

//calling methods
public:
      Students();
      void setStudentName(string studentName);
      void setStudentNumber(int studentNumber);
      void setStudentMajor(string studentMajor);
      void readInputFromUser();
      void displayStudentInfo();
};

//setting value of studentNumber to zero
Students::Students() : Baker()
{
     studentNumber = 0;
}

void Students::setStudentName(string studentName)
{
      Students::studentName = studentName;
}

void Students::setStudentNumber(int studentNumber)
{
      Students::studentNumber = studentNumber;
}

void Students::setStudentMajor(string studentMajor)
{
      Students::studentMajor = studentMajor;
}

//reading input from user
void Students::readInputFromUser()
{
      string localstudentName;
      cout << "Please enter the student's name: ";
      cin >> localstudentName;
      studentName = localstudentName;

      int localstudentNumber;
      cout << "Please enter the student's id number: ";
      cin >> localstudentNumber;
      studentNumber = localstudentNumber;

      string localstudentMajor;
      cout << "Please enter the student's major: ";
      cin >> localstudentMajor;
      studentMajor = localstudentMajor;      
}

//displaying output
void Students::displayStudentInfo()
{
      cout << endl; //prints empty line
      cout << "The student's name is: " << studentName <<endl;
      cout << "The student's id number is: " << studentNumber << endl;
      cout << "The student's major is: " << studentMajor << endl;
}

//staff class derived from base class Baker
class Staff: public Baker
{
      //declaring variables
private:
      string staffName;
      int staffNumber;
      string staffTitle;

public:
      Staff();
      void setStaffName(string staffName);
      void setStaffNumber(int staffNumber);
      void setStaffTitle(string staffTitle);
      void readInputFromUser();
      void displayStaffInfo();
};

//setting value of staffNumber to zero
Staff::Staff() : Baker()
{
     staffNumber = 0;
}

void Staff::setStaffName(string staffName)
{
      Staff::staffName = staffName;
}

void Staff::setStaffNumber(int staffNumber)
{
      Staff::staffNumber = staffNumber;
}

void Staff::setStaffTitle(string staffTitle)
{
      Staff::staffTitle = staffTitle;
}

//reading input from user
void Staff::readInputFromUser()
{
      string localstaffName;
      cout << "Please enter the staff member's name: ";
      cin >> localstaffName;
      staffName = localstaffName;

      int localstaffNumber;
      cout << "Please enter the staff member's id number: ";
      cin >> localstaffNumber;
      staffNumber = localstaffNumber;

      string localstaffTitle;
      cout << "Please enter the title of the staff member: ";
      cin >> localstaffTitle;
      staffTitle = localstaffTitle;      
}

//displaying output
void Staff::displayStaffInfo()
{
      cout << endl; //prints empty line
      cout << "The staff member's name is: " << staffName <<endl;
      cout << "The staff member's id number is: " << staffNumber << endl;
      cout << "The staff member's title is: " << staffTitle << endl;
}

//faculty class derived from base class Baker
class Faculty: public Baker
{
      //declaring variables
private:
      string facultyName;
      int facultyNumber;
      string facultyTitle;

public:
      Faculty();
      void setFacultyName(string facultyName);
      void setFacultyNumber(int facultyNumber);
      void setFacultyTitle(string facultyTitle);
      void readInputFromUser();
      void displayFacultyInfo();
};

//setting value of facultyNumber to zero
Faculty::Faculty() : Baker()
{
     facultyNumber = 0;
}

void Faculty::setFacultyName(string facultyName)
{
      Faculty::facultyName = facultyName;
}

void Faculty::setFacultyNumber(int facultyNumber)
{
      Faculty::facultyNumber = facultyNumber;
}

void Faculty::setFacultyTitle(string facultyTitle)
{
      Faculty::facultyTitle = facultyTitle;
}

//reading input from user
void Faculty::readInputFromUser()
{
      string localfacultyName;
      cout << "Please enter the faculty member's name: ";
      cin >> localfacultyName;
      facultyName = localfacultyName;

      int localfacultyNumber;
      cout << "Please enter the faculty member's id number: ";
      cin >> localfacultyNumber;
      facultyNumber = localfacultyNumber;

      string localfacultyTitle;
      cout << "Please enter the title of the faculty member: ";
      cin >> localfacultyTitle;
      facultyTitle = localfacultyTitle;      
}

//displaying output
void Faculty::displayFacultyInfo()
{
      cout << endl; //prints empty line
      cout << "The faculty member's name is: " << facultyName <<endl;
      cout << "The faculty member's id number is: " << facultyNumber << endl;
      cout << "The faculty member's title is: " << facultyTitle << endl;
}

//calling main program
void main()
{
      //calls class Baker
      Baker bakerInformation;
      bakerInformation.readInputFromUser();
      
      //calls class Students
      Students studentsInformation;
      studentsInformation.readInputFromUser();

      //calls class Staff
      Staff staffInformation;
      staffInformation.readInputFromUser();

      //calls class Faculty
      Faculty facultyInformation;
      facultyInformation.readInputFromUser();

      //display
      bakerInformation.welcomeUser();
      bakerInformation.displayBakerInfo();
      studentsInformation.displayStudentInfo();
      staffInformation.displayStaffInfo();
      facultyInformation.displayFacultyInfo();
}
0
Comment
Question by:alligarbage
  • 8
  • 3
  • 2
  • +3
16 Comments
 

Author Comment

by:alligarbage
ID: 17993730
Can someone please respond soon?
ASAP please.
0
 
LVL 24

Expert Comment

by:fridom
ID: 17993777
Have you tested
void Students::setStudentName(string studentName)
{
     Students::studentName = studentName;
}

If it's just a reference assignment you may run into trouble really soon.

Regards
Friedrich
0
 

Author Comment

by:alligarbage
ID: 17993798
What? I have tested the entire program and it compiles and runs.  There are no error messages, but that's not part of the assignment.  Do you think that I fufilled the assignment description:

Develop a set of classes to represent all of the people that would be of interest to a university or college.  You should model this after your own institution, and you must have at least four classes.  You will have a base class that represents all people, and at least three classes will be derived from this base class, representing students, staff and faculty.  Develop a small program that instantiates and displays objects of all three derived classes.  Remember that the idea behind derived classes is they have their own attributes and methods (specific to them).

Thank you so much for your help.
0
 

Author Comment

by:alligarbage
ID: 17993878
Anyone want to help me out with this?
0
 
LVL 24

Expert Comment

by:fridom
ID: 17993890
No forget it string is not a reference. I got that wrong.

Sorry
Friedrich
0
 

Author Comment

by:alligarbage
ID: 17993896
Does it look good though?
0
 
LVL 24

Expert Comment

by:fridom
ID: 17993934
What about error checking? What happens if someone puts in "foo" while getting asked for a number?

Regards
Friedrich
0
 

Author Comment

by:alligarbage
ID: 17993940
The program does not require error checking.  It just requires what I wrote above.  Does it look like it meets those requirements to you?
0
 

Author Comment

by:alligarbage
ID: 17994015
Anyone?
Can someone just tell me if the program does what the directions say it's supposed to?
0
 
LVL 9

Expert Comment

by:DrAske
ID: 17994253
alliqarbaqe,

1) You don't need to add *Name* string on each derived class (StudentName, FacultyName, .. etc) because it is inherited from the base class, so the derived classes can use the data members provided in the base class (but you have to replace *private* access modifier with *protected* access modifier to allow the derived class to access it).

That's applied to *Number* attribute too!! It's Obvious that each derived class has an attribure number, so you can add a generic attribute *myNumber* to the base class.

As we know that the purpose of using Inheritance is to incorporate an existing functionalities (in the base class) with a new functionalities in the dervied class.

2) you have added a setter functions to each derived classes but you don't use it?? why??

3) why not to choose a proper name for the base class?? "community", "People" or somthing like that??
sorry, english not my native language but I don't think "baker" is the best choice (as a base class) in your case.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 17995061
your design should do something like this:

A) What are the Properties and Methods that are COMMON to all three of the derived classes:  Student, Faculty, Staff:
1) they ALL have a UniversityID number
2) they ALL have a NAME
3) they ALL have a DateOfBirth
4) they ALL have a StartedOnDate (date of first association with the university)

B) Now, what does each class have that is UNIQUE to THAT class, and not shared with the others:

Student)
   a) Yearof Graduation
   b) Cumulative GradePointAverage
   c) Major
   d) FacultyAdvisor

Faculty)
   a) Academic Department
   b) Rank
   
Staff)
    a)StaffPosition
    b) FullTime

This is the analysis part of the assignment - determinig what your application is going to model, and how those elements should be divided among the various classes that the application will be built with.  

The base class (call it Person?) will have the properties

ID_NUMBER
NAME
DateOfBirth
StartDate

class Person
{
//declaring variables
private:
     int ID_Number;
     string yourName;
     date DateOfBirth;
     date StartDate;

//calling methods
public:
     void welcomeUser();    
     void setName(string _Name);
     void setID(int _ID);
     void set_DOB(date _DOB);
     void set_StartDate(date _sDate);
     void readInputFromUser();
     void displayBakerInfo();
};

Now your Student class will Inherit from Person, and add the additional properties that distinguish a Student from the others:

class Student: public Baker
{
//declaring variables
private:
     string Major;
     float cumGPA;
     int gradYear;
     string Advisot;
//calling methods
public:
     Student();
     
     void setMajor(string _Major);
     void setGradYear(int _Grad);
     void setAdvisor(string _advisor);
     void setGPA(float _GPA);
};

Note that because a Student is also a Person, they ALSO have the Person Properties, and Person Methods -0 you only need to add the variables and methods that are UNIQUE to a Student.

Similarly for Faculty and Staff.  You only add the variables and procedures that are UNIQUE to Faculty or UNIQUE to Staff.

AW


0
 

Author Comment

by:alligarbage
ID: 17996175
Thank you so much, I get it now.  :D
0
 

Author Comment

by:alligarbage
ID: 17996258
So, do I write the code for input and output of the personID, personName, etc in the base class code (Person) or in each derived code?  
0
 
LVL 4

Accepted Solution

by:
chhokra_expert earned 500 total points
ID: 17997611
one of the core ideas of using classes is code reuse: think efficient/ lazy
whatever attributes are common to all classes belong  in a base class. Consequently, any accessors of these data make sense in the base class or else you will need to implement these for EVERY sub-class. Imagine a hierarchy like:

class base
{
private:
int m_nID;
string m_strName;

};

class sub1 : public base
{

};

class sub2 : public base
{

};

...

class sub1000 : public base
{

};

if for each of these 1000 classes, if you wanted to input / output id and name, writing these functions for each of the sub-classes is exhorbitantly expensive and difficult to maintain (can you write 2000 functions in a day? what if you had to change some behavior once it is written?). instead if you simply wrote those for the base class, you'd be getting the functionality "for free" in the sub-classes.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 17997688
Yes, you would write the code to get the base values (ID, Name, ...) in the Base class.  

Think of a tree, starting at the root, and then branching, which branches then branching.  So the LOWER on the tree you can do something, it will then be 'delivered' up the tree, to the braches.  If you only want to affect one branch, and not others, then  you apply the change to the branch where you want it to have effect.

AW
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 17997959
>Can someone just tell me if the program does what the directions say it's supposed to?
I think it does but for me personally some part of code like this:

//1
void Staff::setStaffName(string staffName)
{
     Staff::staffName = staffName;
}
//2
...
   cin >> localstaffName;
     staffName = localstaffName;
...

don't look very pretty. I would like to write

class Staff: public Baker
{
     //declaring variables
private:
     string m_StaffName;

....
//1
void Staff::setStaffName(string staffName)
{
   m_StaffName = staffName;
}
//2
   cin >> localstaffName;
  setStaffName(localstaffName);

function
void Baker::welcomeUser()
{

looks a liile bit strange for me, becaouse it is nor related to the base class. I t could be possible to make it  as static or better use only write lines in the main function. Base class name - "Baker" not good for me too.
I would like to use virrtual function displayInfo(), not member functions displayXXInfo()

I use class name as Students only for a set of a students not for the single person.
0

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now