arrey cons in c++ oop

# include <iostream>
using namespace std;
class a{
int b;
public:
a(int c){b=c;}
void set()
{
cin>>b;
}
};
void main()
{
a ob[100000]
for(int i=1;i<=100000;i++)
ob[i].set();
}
if i set this topic thats error
num array is 100000 ..
how i can doing array to this Qas ...
Note: i cant used defult constrector .
Last edited on
Note: Explain what you mean, we must guess what are you thinking to give you an answer. Don't you think.
Also, void main isn't valid C or C++.
i edit my topic
C++ allows some functional constructs, but it is not a functional language.

What you call "cons" in a functional language is the same as "insert at head of list".

Either use a standard container (like a vector or a deque or a list) and use the push_front() or insert() method.

Or you must copy elements in the array one item to the right and write the new element value in the first position.

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
#include <iostream>
using namespace std;

int cons( int x, int* xs, int len )
  {
  for (int n = len - 1; n > 0; n--)
    xs[ n ] = xs[ n - 1 ];
  xs[ 0 ] = x;
  return len + 1;
  }

int main()
  {
  int as[ 10 ];
  int alen = 0;

  alen = cons( 3, as, alen );
  alen = cons( 2, as, alen );
  alen = cons( 1, as, alen );

  for (int n = 0; n < alen; n++)
    cout << as[ n ] << " ";
  cout << endl;

  return 0;
  }

Example of a functional approach:

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
#include <iostream>
#include <list>
using namespace std;

list <int> cons( int x, const list <int> & xs )
  {
  list <int> result = xs;
  result.push_front( x );  // The push_front() function works much like cons
  return result;
  }

int main()
  {
  list <int> xs;

  xs = cons( 3, xs );
  xs = cons( 2, xs );
  xs = cons( 1, xs );

  for (list <int> ::iterator xi = xs.begin(); xi != xs.end(); ++xi)
    cout << *xi << " ";
  cout << endl;

  return 0;
  }

Hope this helps.
thx all ..
thx Duoas ....i hope your topic ...
but if i use this . i have a big problem
problem is in main the logicall is not good if i have example : 100000
because i need write in main 100000( alen = cons( 3, as, alen );)
in my program ..
my Qus ? i can solution that ..thx and sorry to Bother
No offense, I'm really having a hard time understanding what you want. My entire response was based upon the title: "cons" and "array".

In my example for the array, alen is not the upper bound of the array, but the number of elements used in the array.

Are you trying to attach a new element to the front of the list? (That is what cons does.)
Or are you trying to initialize the list when you create it in main()?

1
2
3
4
5
int main()
  {
  // A list of primes. The first 11 elements are initialized. The remaining elements are set to zero.
  int primes[1000] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 0 };
  }
[code] Your code goes here [/code]
1
2
3
a ob[100000]; //no matching function for call to `a::a()'
for(int i=1;i<=100000;i++)
ob[i].set();


I think the problem is the lack of default constructor
Use std::vector with reserve and push_back
1
2
3
4
std::vector<a> ob;
ob.reserve(100000);
for(int K=0; K<10000; K++)
 ob.push_back( a(42) );


(you could also use an array of pointers, and create the objects dynamically, but that has secondary effects)
if i have a class in c++, the class contains parametrized constructor
ex: class A
{private:
int a,b;

public:
A(int x, int y){ a=x; b=y;}}
how could i define array of objects and pass the parameters of the constructor for each object cell
int main()
{ A ob[10]; // how could i pass the two parameters of each object constructor (ob[0], ob[1],...)


return 0;}
my question correctly now ..
happyjoker >how could i define array of objects and pass the parameters of the constructor for each object


In fact, you can not.
It is YOUR responsibility to create each object explicitly, cause` arrays are making use of default constructors.
So, you have to allocate memory fist, then place generated objects.

You also can allocate an array of pointers (if you really have to utilize arrays) and for each pointer allocate a new object.
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
#include <iostream>
using namespace std;

struct point
  {
  int x, y;
  point( int x, int y ): x( x ), y( y ) { }
  };

ostream& operator << ( ostream& outs, const point& pt )
  {
  return outs << "(" << pt.x << ", " << pt.y << ")";
  }

template <typename T, size_t N>
size_t sizeof_array( const T (& a) [ N ] )
  {
  return N;
  }

int main()
  {
  point pts[] =
    {
    point( 1,  2 ),
    point( 2,  3 ),
    point( 3,  5 ),
    point( 4,  7 ),
    point( 5, 11 ),
    point( 6, 13 )
    };

  for (unsigned n = 0; n < sizeof_array( pts ); n++)
    cout << pts[ n ] << endl;

  return 0;
  }
You can not defer the initialization of the array elements to some later point in order to use custom initialization algorithm. There are some workarounds in poor programming style, to give you the idea:

Suppose your class was:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class A
{
private:
    int a,b;

public:
    A(int x, int y)
    {
        a=x; b=y;
        std::cout << "Initializing with x = " << x
                << " and y = " << y << std::endl;
    }

    ~A()
    {
        std::cout << "Destroying with a = " << a
                << " and b = " << b << std::endl;
    }
};


One solution that should work is this:
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

int main()
{
    int N = 10;
    char array_[sizeof(A [N])];

    struct TryFinally
    {
        A* array_ptr;
        int construction_idx;

        TryFinally(char array_[], int N)
        {
            array_ptr = (A*)array_;

            for (construction_idx = 0; construction_idx < N; ++construction_idx)
            {
                new (array_ptr + construction_idx) A(construction_idx, construction_idx);
            }
        }

        ~TryFinally()
        {
            for (--construction_idx; construction_idx >= 0; --construction_idx)
            {
                array_ptr[construction_idx].~A();
            }
        }
    } tf(array_, N);

    // You function code follows here ...
}


There is no finally clause for a catch block in C++, but if there was, the code would look more like this:
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
int main()
{
    int N = 10;
    char array_[sizeof(A [N])];
    A* array_ptr = (A*)array_;

    struct Finally {};

    int construction_idx = 0;

    try
    {
        for (/*construction_idx is 0*/; construction_idx < N; ++construction_idx)
        {
            new (array_ptr + construction_idx) A(construction_idx, construction_idx);
        }

        // You function code follows here ...

        throw Finally();

    }
    catch (...)
    {
        for (--construction_idx; construction_idx >= 0; --construction_idx)
        {
            array_ptr[construction_idx].~A();
        }

        try { throw; } catch (Finally&) {}
    }
}

The second technique impairs the performance of your program and will certainly disqualify you at a job interview. It is also a great tease for C++ programmers to see the finally approach emulated vs using the RAII idiom. The first technique is a low level hack that I believe is standard conformant, but is indication that you either refrain from using STL containers for a purpose or you don't know STL very well (which happens to be my case).

The solutions in STL use dynamically allocated memory a lot, which you may want to avoid. But dynamically allocated objects are central aspect of the C++ language, so stack based and static storage optimizations are not given much ado.

Regards
Last edited on
What is this for?
try { throw; } catch (Finally&) {}

And why do you simply throw an exception at line 20?
It doesn't provide a try...finally construct. This attempts to give it one. (It is mis-formed, though).
Throwing at the end of the try block allows me to execute the catch code in all cases. After all, this is what you would expect from a finally block. The inner re-throw will propagate the exceptions, except Finally, which will be silenced by the following catch. This is a bit of teasing code. C++ does not always bring out the best in men :)

Regards
Topic archived. No new replies allowed.