( Help please! :) )I'm having trouble utilizing a class object within another class
Feb 23, 2016 at 8:46pm UTC
I am trying to incorporate an object of a date class that I have created into a student class that I have created, but I am unsure how. Here are both of my classes thus far.
DATE>H
______
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
using namespace std;
class Date{
private :
//data members
int mm,dd,yyyy;
public :
//constructors
Date();
Date(int mm,int dd,int yyyy);
//methods
void display()const ;
void input();
};
DATE.CPP
________
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
#include "date.h"
//function definitions
Date::Date(){
cout << "default constructor" ;
mm = 0;
dd = 0;
yyyy = 0;
}
Date::Date(int mm,int dd,int yyyy){
Date::mm = mm;
Date::dd = dd;
Date::yyyy = yyyy;
}
void Date::input(){
char x = '/' ;
cout << "please input the month, day and year (mm/dd/yyyy)" ;
cout << endl;
cin >> mm >> x >> dd >> x >> yyyy;
cout << endl;
}
void Date::display() const {
cout << mm << "/" << dd << "/" << yyyy;
}
STUDENT.H
_________
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
#include <iostream>
#include "date.h"
using namespace std;
class Student{
private :
Date dob;
string studentid;
char gender;
public :
//Default Constructors
Student();
Student(Date dob, char gender, string studentid);
//Mutator Methods
//set methods
//perhaps set dob somehow??
void setGender();
void setIDNumb();
//input and display methods
void input();
void display();
//Accessor Methods
char getGender();
string getIDNumb();
};
STUDENT.CPP
__________
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
#include "student.h"
//function definitions
void Student::setDob(//?){
//?
}
void Student::setGender(){
cout << "Please input your gender (M/F): " ; cin >> gender;
}
void Student::setIDNumb(){
cout << "Please input your ID number: " ;
cin >> studentid;
}
char Student::getGender(){
return gender;
}
string Student::getIDNumb(){
return studentid;
}
void Student::input(){
cout << endl;
setGender();
setIDNumb();
}
void Student::display(){
cout << endl;
cout << "Student's gender: " ;
cout << getGender();
cout << endl;
cout << "Student's ID number: " ;
cout << getIDNumb();
cout << endl;
}
Ultimately, I am attempting to output a hypothetical student's date of birth using my date class, followed by a student's gender and i.d.
Any help would be much appreciated.
Thank you!!! :)
Last edited on Feb 23, 2016 at 8:50pm UTC
Feb 23, 2016 at 9:15pm UTC
You've already written Date::Display(). All you need to do is call it.
In student.cpp, after line 43:
44 45
cout << "Student's DOB" << endl;
dob.Display ();
Feb 23, 2016 at 9:22pm UTC
but it needs to be an object in my student class' constructor rather than simply calling it from "student's" driver
Feb 23, 2016 at 9:57pm UTC
I don't understand what you mean by that statement.
I don't see an implementation for either your Student default constructor or Student's parameterized constructor (declared in your header at lines 16-17).
Regarding setDob():
1 2 3
void Student::setDob (const Date & Dob)
{ dob = Dob;
}
That should give you a clue what you have to do in your constructor.
Note: It is a poor practice to name your arguments the same as your member variables.
Topic archived. No new replies allowed.