Using an object from Class A in Class B

Hi,
So I have a class Student that contains student first name and last name
And I have 2 other classes named DateOfBirth and Units

What I'm trying to do is using the Student class, create a vector that contains the students name, DOBs and Units

1
2
3
4
5
6
7
8
9
10
  Class Student {
public:
   getFirstName()
   getLastName()

   setFirstName(string lName)
   setLastName(string fName)

private:
   //atributes 


1
2
3
4
5
6
7
8
  Class DOB {
public:
   getDOB()

   setDOB(string DOB)

private:
   //atributes 


1
2
3
4
5
6
7
8
  Class DOB {
public:
   getUnits()

   setUnits(int Unit)

private:
   //atributes 


How do I incorporate the DOB and Unit classes into the student class so that they're not excluded from the vector because currently the vector does not contain the DOB and Unit.

Thanks
Last edited on
You haven't shown us the full declaration for Student, so it's impossible to tell if DOB and Units (which you also did not show) are included in Student.

DOB and Units are attributes of a student they should be members of your Student class.


Currently DOB and Units are not included in Student. The only thing student has are setters and getters for the student first name and last name. DOB and Units also only have their own setters and getters.

The problem I'm having is figuring out how to include DOB and Units into Student. I could put DOB and Units into the Student class but I want to learn to work with different classes.
I could put DOB and Units into the Student class but I want to learn to work with different classes.

By doing so, you are working with classes. Doing so is an important principle called composition. If you can say a Student HAS A DOB and a Student HAS A Units, then you can be pretty sure those belong as members of Student.
In order to include DOB and Units into the student class, you want to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "DOB.h"
#include "Units.h"
#include <string>

class Student
{
public:

    // ...

private:
    std::string firstName;
    std::string lastName;
    DOB dateOfBirth;
    Units units;
};

How does this ensure that when the vector takes in the student objects, it also takes in their DOB and units?
Can someone please give me a small example? I could really use it.
How does this ensure that when the vector takes in the student objects, it also takes in their DOB and units?

If you push a Student instance onto a std::vector, you push all it's member variables including DOB and Units.

Can someone please give me a small example?
doug4 already gave you an example of what your Student class declaration should look like.

What I think you may be missing is a parameterized constructor:
1
2
3
4
5
6
7
8
9
10
11
Student::Student (const string & fname, const string & lname, const DOB & dob, const Units & unit)
{  firstname = fname;
    lastname = lname;
    dateofBirth = dob;
    units = unit
}

...
   Student fred ("Fred", "Flintstone", DOB(1,1,1970), Units(1));
   vector<Student> students;
   students.push_back (fred);


Last edited on
Topic archived. No new replies allowed.