Encapsulation involving multiple classes

Hi all,

I'm having trouble with encapsulating my C++ code. So far, I've created 7 files: person.h, person.cpp, city.h, city.cpp, country.h, country.cpp and a main.cpp. I understand that each class would go into its own .h file and I believe I did that much correctly. However, I am unable to determine what would go into the implementation files (the country.cpp, person.cpp, city.cpp files) as well as what would go into the main.cpp file. Would anything in the
int main() go into the implementation files or would they be included in the main.cpp file only? Thanks in advance!

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
#include<iostream>
#include<string>
using namespace std;
  
 // define your classes here...
class Person {
string name;

public:
	void setName (string name) {
		this ->name = name;
	}
	string getName() {
		return name;
	}
};
class City {
string name;
Person mayor;
public:

	void setName (string name) {
		this ->name = name;
	}
	string getName() {
		return name;
	}
	void setMayor (Person mayor) {
		this ->mayor = mayor;
	}
	Person getMayor() {
		return mayor;
	}
};
class Country {
string name;
Person leader;
City city;

public:
	void setName (string name) {
		this ->name = name;
	}
	void setLeader(Person leader) {
		this ->leader = leader;
	}
	void setCity(City city) {
		this ->city = city;
	}
	string getName() {
	return name;
	}
	City getCity() {
	return city;
	}
	Person getLeader() {
	return leader;
	}
};

int main()
{
        Person person1, person2;
        person1.setName("Barack Obama");
        person2.setName("Bill De Blasio");

        Country country1;

        City city1;
               
        city1.setName("New York City");

        city1.setMayor(person2);   

        country1.setName("United States of America");

        country1.setLeader(person1);

        country1.setCity(city1);
       
        // this should print Barack Obama
        cout << country1.getLeader().getName() << endl;

        // this should print Bill De Blasio
        cout << country1.getCity().getMayor().getName() << endl;
       

return 0;
}
Last edited on
It seems like you need to include all the header files into the main program.

Like..

#include "city.h" at the top. Same for the other two.

And for the implementation files, you need to also include the correct header file for them. Just the ones that relate to it. Like in city.cpp, you would
#include "city.h"
at the top.
Last edited on
Hi sethman410, thanks for your reply! I have that done so far. I was just wondering as to which parts of the original code would go into the city.cpp/country.cpp/person.cpp file. Basically, which parts of the original code (in my original post) would go into the implementation files, which parts wouldn't, etc. Also, what would the main.cpp file consist of.

Would anything in the int main () that is associated with the city class, including the city1 variable, have to go into the city.cpp file? Thanks again.
For big and complicated classes, the convention is to have the class definition in the .h file:

1
2
3
4
5
6
7
8
9
#include <string>

class Person {
  std::string name;

public:
  void setName (const std::string& name);
  std::string getName();
};

and for the class implementation to be in the .cpp file:

1
2
3
4
5
6
7
8
9
10
11
#include "person.h"

void Person::setName (const std::string& name)
{
  this->name = name;
}

std::string Person::getName()
{
  return name;
}

Last edited on
Thanks Shodan Ho! Your answer was very helpful. Just one question though -- where would the variables Person person1 go? Would it be located in the implementation file or the main.cpp? Thanks again.
In main.cpp or wherever it is used.

So there are two distinct things:
(1) The _definition_ of the class which defines the code and variables associated. This is generally in the .h and .cpp as discussed earlier.

This defines the size, interface, code, behavior of all members of this class. But generally no instances are defined.

(2) The _instantiation_ and use of variables of this class.

An example is:

Person person1, person2;

which defines two instances of Person. Each has the same variables:
1
2
person1.name
person2.name

and the same methods:
1
2
3
4
5
person1.setName(..);
person1.getName();

person2.setName(..);
person2.getName();

which were define earlier in (1).

As you get more familiar with coding, exceptions to these conventions will occur, and modifications will occur.
Last edited on
Don't using namespace std; in headers, everything that includes it gets polluted.

Regarding your design, don't feel that your class is encapsulated just because the members are private http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
Also check out `Demeter's Law' and `Tell Don't Ask'
yes, edited.
Last edited on
Thank you ShodanHo! I found your reply very helpful. Also, thank you ne555 for your reply! I'll make sure not to include that in my .h files.
Topic archived. No new replies allowed.