One class using another class

okay so I have two classes and I want one class to use the other class but I dont want it to be a base and derived class situation. This is my code so far, Im not sure how to connect the two classes. Please help!

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  // Date.h
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
#ifndef DATE_H
#define DATE_H

class Date
{
private:
	int _month;
	int _day;
	int _year;
public:
	Date();
	Date(int month, int day, int year);
	~Date();
	void setDate(int month, int day, int year);
	void displayDate()const;
};
#endif
// ===================================================
// Date.cpp
#include"Date.h"
Date::Date()
{
	_month = 0;
	_day = 0;
	_year = 0;
}
Date::	Date(int month, int day, int year)
{
	_month = month;
	_day = day;
	_year = year;
}
Date::	~Date(){}
void Date::setDate(int month, int day, int year)
{
	_month = month;
	_day = day;
	_year = year;
}
void Date:: displayDate()const
{
	cout << "Birthdate: " << _month << "/" << _day << "/" << _year << endl;
}
// ===========================================
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
#ifndef FAMOUS_PEOPLE_H
#define FAMOUS_PEOPLE_H
class FamousPeople
{
private:
	string _name;
	string _quote;
	int _birthdate;
public:
	FamousPeople();
	~FamousPeople();
	void setName(string name);
	void setQuotes(string quote);
	void setDate(int month, int day, int year);
	void displayPerson();
	void displayDate();
};
#endif
// ======================================================
// FamousPeople.cpp
#include"FamousPeople.h"
FamousPeople::FamousPeople()
{
	_name = "";
	_quote = "";
	_birthdate = 0;

}
FamousPeople::~FamousPeople(){}
void FamousPeople::setName(string name)
{
	_name = name;
}
void FamousPeople::setQuotes(string quote)
{
	_quote = quote;
}
void FamousPeople::setDate(int month, int day, int year) 
{
	_birthdate.setDate(month, day, year);
}
void FamousPeople::displayPerson()
{
	
}
void FamousPeople::displayDate()
{
	_birthdate.displayDate();
}
// =============================================
#include"Date.h"
#include"FamousPeople.h"

int main ()
{
	FamousPeople person1;
	FamousPeople person2;
	FamousPeople person3;

	string name;
	string quote;
	int month;
	int day;
	int year;

	cout << "Enter the first famous person:";
	cin >> name;
	person1.setName(name);
	cout << "\nEnter the quotation: ";
	getline(cin,quote);
	person1.setQuotes(quote);
	cout << "Enter the birthdate: ";
	cin >> month;
	cin >> day;
	cin >> year;
	person1.setDate(month,day,year);
	

	cout << "Enter the second famous person:";
	cin >> name;
	person2.setName(name);
	cout << "\nEnter the quotation: ";
	getline(cin,quote);
	person2.setQuotes(quote);
	cout << "Enter the birthdate: ";
	cin >> month;
	cin >> day;
	cin >> year;
	person2.setDate(month,day,year);
	
	cout << "Enter the third famous person:";
	cin >> name;
	person3.setName(name);
	cout << "\nEnter the quotation: ";
	getline(cin,quote);
	person3.setQuotes(quote);
	cout << "Enter the birthdate: ";
	cin >> month;
	cin >> day;
	cin >> year;
	person2.setDate(month,day,year);

	return 0;
}


All you need to do is create a object of class Date inside your FamousPeople class, therefore your class would look like this:

FamousPeople.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

#ifndef FAMOUS_PEOPLE_H
#define FAMOUS_PEOPLE_H

#include <iostream>
#include <iomanip>
#include <string>
#include "date.h"   // this line added
using namespace std;

class FamousPeople
{
private:
	string _name;
	string _quote;
	Date _birthdate;   // this line added

public:
	FamousPeople();
	~FamousPeople();
	void setName(string name);
	void setQuotes(string quote);
	void setDate(int month, int day, int year);
	void displayPerson();
	void displayDate();
};
#endif


FamousPeople.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

// ======================================================
// FamousPeople.cpp
#include"FamousPeople.h"

FamousPeople::FamousPeople()
{
	_name = "";
	_quote = "";
	_birthdate.setDate(0, 0, 0);   // this line added
}
FamousPeople::~FamousPeople(){}

void FamousPeople::setName(string name)
{
	_name = name;
}
void FamousPeople::setQuotes(string quote)
{
	_quote = quote;
}
void FamousPeople::setDate(int month, int day, int year)
{
	_birthdate.setDate(month, day, year);
}
void FamousPeople::displayPerson()
{

}
void FamousPeople::displayDate()
{
	_birthdate.displayDate();
}


Hope this helps.
Last edited on

By the way, you could shorten your main function quite a bit by changing the way you declare your 3 objects and using a loop:

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

#include"Date.h"
#include"FamousPeople.h"

int main()
{
	FamousPeople people[3];

	string name;
	string quote;
	int month;
	int day;
	int year;

	// loop for the three famous people
	for (int i = 0; i < 3; i++)
	{
		cout << "Enter famous person No." << i + 1 << ": ";
		cin >> name;
		people[i].setName(name);
		cout << "\nEnter the quotation: ";
		getline(cin, quote);
		people[i].setQuotes(quote);
		cout << "Enter their birthdate: ";
		cin >> month;
		cin >> day;
		cin >> year;
		people[i].setDate(month, day, year);
	}

	return 0;
}
Thank you!!!!!
Your welcome :)
Topic archived. No new replies allowed.