Announcement

Collapse
No announcement yet.

Array Class + operators overloading () and =

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • zurkan
    replied
    nice post...................

    Leave a comment:


  • xgiovio
    started a topic Array Class + operators overloading () and =

    Array Class + operators overloading () and =

    PHP Code:

    #include <iostream>
    #include <string>
    #include <exception>

    using namespace std;

    class 
    x_array {
    public:
        
    //////////////////////////// constructor for int
        
    x_array (int n 10)
        :
    current_address(this)
        {
            
            try{
            if (
    n<= 0)
                throw 
    string ("Impossibile allocare byte richiesti");
            }
            catch (
    string error){
                
    cout << error << endl;
            }
            
            
            try{
            
    pointer = new int [n];
            }
            catch (
    bad_alloc error) {
                
    cout << error.what() << endl;
            }
            
            
    size n;
        
        }
        
    ///////////////////////////////// constructor with copy
        
    x_array x_array source)
        :
    current_address(this)
        {
            
        
            
    size source.get_size();
            
            try{
                
    pointer = new int [size];
            }
            catch (
    bad_alloc error) {
                
    cout << error.what() << endl;
            }
            
            for (
    int i0;isize;i++){
            
                *(
    pointer+i) = *(source.get_pointer() + i);
            
            }

            
        }
        
        
    ///////////////////////////////// deconstructor
        
    ~x_array (){
        
            
    delete[] pointer;
        }
        
        
        
        
    ///////////////////////////// other functions
        
        ////////////////////// size of array
        
    int get_size(){
            return 
    size;
        }
        
        
    ////////////////////// get address of the object
        
    const x_array get_address(){
            return 
    current_address;
        }
        
        
    ////////////////////// get address of the allocated array
        
    const int get_pointer(){
        
            return 
    pointer;
        
        }
        
        
    //////////////////////// fill array
        
    void fill_array (){
        
            
    int i;
            
    cout << "Enter " << size << " values" << endl;
            for (
    i=0;i<size;i++){
            
                
    cin >> *(pointer +i);
            
            }
        
        }
        
        
    ////////////////////// print array
        
    void print_array(){
        
            
    int i;
            
    cout << "Size = " << get_size() << endl;
            
            for ( 
    size i++ ) {
                
                
    cout << *(pointer +i) << endl;
                
            }
        
        
        }
        
        
        
    ///////////////////////////// overload operators
        
        ///////////////overload = with truncate
        
        
    x_array operator= (x_array source){
            
            
    int i0;
            
            if (
    this->size == source.get_size()) {
            
            
                for (
    i0;isize;i++){
                
                    *(
    pointer+i) = *(source.get_pointer() + i);
                
                }
                
            } else {
            
                
    int common;
                if (
    this->size source.get_size()){
                    
    common source.get_size();
                    
                    for (
    i0;icommon;i++){
                        
                        *(
    pointer+i) = *(source.get_pointer() + i);
                        
                    }
                    
                    for (
    icommon i<size ;i++){
                        
                        *(
    pointer+i) = 0;
                        
                    }
                    
                }else{
                    
    common this->size;
                    
                    for (
    i0;icommon;i++){
                        
                        *(
    pointer+i) = *(source.get_pointer() + i);
                        
                    }
                    
                }
            
            }
            
            return *
    this;
        }
        
        
    //////////////////////// overload () = print integers between positions specified
        
        
    void operator()  (int beginint end){
            
    int total = ( end begin) +1;
            
            for (
    int i0;i<total;i++){
                
    cout << (*(pointer+(begin i))) << endl;
            
            }

        }
        
    private:
        
    int size;
        
    int pointer;
        const 
    x_array * const current_address;


    };


    ///////////////////////////////// non-member operators overload



    int main (){
        
        
        
        
    x_array obj1(5); // create an object with 5 elements of type int
        
    obj1.fill_array(); // fill the object array with user input
        
    obj1(1,3); // print int between the 1 and 3 (array indexes)
        
        
    x_array obj2(3);
        
    obj2.fill_array();
        
    obj1=obj2// copy ints from onj2 to obj1. obj1 is bigger so latest entries will be 0
        
    obj1.print_array(); // print onj1 after the copy
        

        
        
        
    return 0;



Working...
X

Google Profile


My name is Giovanni Di Grezia, but people call me xgiovio.

Here is my homepage:.

I'm a VFX Artist and Software Developer.

Giovanni Di Grezia