Which is the better programming style?

I am looking for some opinions on programming style.

The following two example programs both run as intended and are similar.
The Row class is composed of an array of samples.
The Row class will be in a library.
The size of the sample array is a constant value determined by the main program.

Which example is the better programming style?

1) sample[] array is created in the main program and passed to the Row constructor:
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
#include <iostream>

class Row
{
	private:
		int* samples;		//array of samples
		int N;			//number of samples
	public:
		Row(int* s, int n): samples(s), N(n) {}
		void scan()
		{
			for (int i=0; i<N; i++)
			{
				samples[i] = i;
				std::cout << samples[i];
			}
		}
};

const int N=2;
int samplesArray[N];
Row row(samplesArray, N);

int main()
{
	row.scan();
}


2) sample[] array is created in the Row class and uses a template to set array size.
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>

template< unsigned N >			//number of samples
class Row{
	private:
		int samples[ N ];	//array of samples
	public:
		void scan()
		{
			for (int i=0; i<N; i++)
			{
				samples[i] = i;
				std::cout << samples[i];
			}
		}
};

const int N=2;
Row< N > row;

int main()
{
	row.scan();
}

Output:
01

Is there a third way that is better?

Thank you.
Last edited on
Of course 2. It has less lines of code and does not require two pointers to the same object.
> Which example is the better programming style?

If the number of samples is a constant known at compile time, 2. is simple and direct.
1. is more flexible; the number of samples can be determined at runtime.


> Is there a third way that is better?

If the number of samples is a constant known at compile time (better version of 2.)
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
#include <iostream>
#include <array>
#include <numeric>

template < std::size_t N > class Row
{
    // http://www.stroustrup.com/C++11FAQ.html#std-array
    private: std::array<int,N> samples ;

    public:

        void scan()
        {
            // http://en.cppreference.com/w/cpp/algorithm/iota
            std::iota( std::begin(samples), std::end(samples), 0 ) ;

            // http://www.stroustrup.com/C++11FAQ.html#for
            for( int v : samples ) std::cout << v << ' ' ;
            std::cout << '\n' ;
        }

        // ...
};

int main()
{
    Row<5> row ;
    row.scan() ;
}


Otherwise (better version of 1.)
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
#include <iostream>
#include <vector>
#include <numeric>

class Row
{
    // http://www.mochima.com/tutorials/vectors.html
    private: std::vector<int> samples ;

    public:
        Row( std::size_t n ) : samples(n) {}
        Row( const int* s, std::size_t n ) : samples( s, s+n ) {}
        // ...

        void scan()
        {
            // http://en.cppreference.com/w/cpp/algorithm/iota
            std::iota( std::begin(samples), std::end(samples), 0 ) ;

            // http://www.stroustrup.com/C++11FAQ.html#for
            for( int v : samples ) std::cout << v << ' ' ;
            std::cout << '\n' ;
        }

        // ...
};

int main()
{
    Row row(5) ;
    row.scan() ;
}
Thank you coder777 and JLBorges.
The number of samples is a constant known at compile time.
Topic archived. No new replies allowed.