Encapsulation

Hello People,

I have a class, let's say:

1
2
3
4
5
6
7
8
class MyClass
 {
public:
	MyClass();					// constructor that initializes MyClass

private:
	std::array<double, aNumber>	 myVar;	// array to be encapsulated
}; // end class  MyClass 


Now, let's say I create an array of MyClass objects:

std::array<MyClass,10> MyObject;

I want the constructor to initialize myVar, for every object, from a specific file.
That is, there is a specific file for every object.
Can I do this without compromising myVar encapsulation?
I mean, without using a setter?

Thanks
Last edited on
Can I do this without compromising myVar encapsulation?


I don't understand what you mean by this.

Even though myVar is private, the constructor is part of the class and can access it.


For the initialization I have:

std::array<std::array<double, aNumber>, 10> myArray

How can the constructor initialize every object in MyObject array individually with every array in myArray?

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class A
{
    public:

        A() = default ; // defaulted default constructor
                        // initialises to all zeroes (member initialiser)

        explicit A( std::string file_name ) // reads numbers from the file into the array
        {
            std::ifstream file(file_name) ;
            for( double& d : arr ) file >> d ;
            // not that on input failure, the values remain zero
        }

    private:

        static constexpr std::size_t N = 10 ;

        // in-class member initialiser initialises to all zeroes
        std::array< double, N > arr {} ;
};
Thanks JLBorges,

Please excuse me if I missed something, or didn't explain myself well.

If I create an array of 10 objects of the A class, let's say:

std::array<A, 10> a;

How can I construct every object a of A with a different file.

That is:

initialize object a[0] with file0,
object a[1] with file1, …
object a[9] with file9.

Is that possible?

I can do that with a setter, but I don't want to ruin the array encapsulation.

Thanks
Last edited on
> How can I construct every object a of A with a different file

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
#include <iostream>
#include <array>
#include <string>
#include <fstream>

class A // unchanged, the same as in the earlier post
{
    public:

        A() = default ; // defaulted default constructor
                        // initialises to all zeroes (member initialiser)

        explicit A( std::string file_name ) // reads numbers from the file into the array
        {
            std::ifstream file(file_name) ;
            for( double& d : arr ) file >> d ;
            // not that on input failure, the values remain zero
        }

    private:

        static constexpr std::size_t N = 10 ;

        // in-class member initialiser initialises to all zeroes
        std::array< double, N > arr {} ;
};

int main()
{
    // initialise these objects from the specified files
    std::array< A, 4 > my_objects { A{"file0"}, A{"file1"}, A{"file2"}, A{"file3"} } ;

    // update my_objects[2] using the contents of file "new_file2"
    my_objects[2] = A{ "new_file2" } ;
}
Great!

Thank you very much JLBorges!
Topic archived. No new replies allowed.