messing around with classes: how to use arrays?

This is a discussion on messing around with classes: how to use arrays? within the C++ Programming forums, part of the General Programming Boards category; this is my simple test class, showing 3 points: 1, 2, 3. These are 3 separate fields. I want to ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    messing around with classes: how to use arrays?

    this is my simple test class, showing 3 points: 1, 2, 3.
    These are 3 separate fields.
    I want to try to use arrays like p[3]={0,1,2}.
    But i fail in getting that to work.

    Maybe someone can help me?

    Code:
    #include <iostream>
    #include <string>		// C++ string
    
    using namespace std;
    
    class point {
    public:
    	point();				// default constructor
    	point( int initp1, int initp2, int initp3);		// constructor
    	int getp1();			// accessor 
    	int getp2();
    	int getp3();
    	void setpoint( int newp1, int newp2, int newp3 );	// mutator 
    	void setp1( int newp1 );		// mutator p1
    	void setp2( int newp2 );		// mutator p2
    	void setp3( int newp3 );		// mutator p3
    private:
    	int p1;
    	int p2;
    	int p3;
    };
    
    point::point()					// default constructor
    {	
    	p1 = 0;
    	p2 = 0;
    	p3 = 0;
    }
    
    point::point( int initp1, int initp2, int initp3)		// constructor
    {	
    	p1 = initp1;
    	p2 = initp2;
    	p3 = initp3;
    }
    
    int point::getp1()				// accessor 
    {	return p1;
    }
    
    int point::getp2()				// accessor
    {	return p2;
    }
    
    int point::getp3()				// accessor 
    {	return p3;
    }
    
    void point::setpoint( int newp1, int newp2, int newp3 )	// mutator 
    {	
    	p1 = newp1;
    	p2 = newp2;
    	p3 = newp3;
    }
    
    void point::setp1( int newp1 )			// mutator p1
    {	p1 = newp1;
    }
    
    void point::setp2( int newp2 )			// mutator p2
    {	p2 = newp2;
    }
    
    void point::setp3( int newp3 )			// mutator p3
    {	p3 = newp3;
    }
    
    void main()
    {	
    	point p(1,2,3);
    
    	cout << "points: " << p.getp1()  << " , " <<  p.getp2()  << " , " <<  p.getp3() << endl;
    
    }
    output:
    Code:
    points: 1 , 2 , 3
    i have tried the following:
    Code:
    ...
    //keeping everything from above until main()
    void main()
    {	
    	point p[3]={(1,2,3)};
    
    	cout << "points: " << p[1].getp1   << endl;
    
    }
    or even rewriting the whole class with arrays in it but there are like 33 errors that are gibberish

    Code:
    #include <iostream>
    #include <string>		// C++ string
    
    using namespace std;
    
    class point {
    public:
    	point();				// default constructor
    	point( int initp1, int initp2, int initp3);		// constructor
    	int getp[3]={};			// accessor 
    	void setpoint( int newp1, int newp2, int newp3 );	// mutator 
    	void setp[3]={int newp1, int newp2, int newp3};		// mutator 
    private:
    	int p[3];
    };
    
    point::point()					// default constructor
    {	
    	p[3] = {0,0,0};
    }
    
    point::point( int initp1, int initp2, int initp3)		// constructor
    {	
    	p[3] = {initp1, initp2, initp3};
    }
    
    int point::getp[0]()				// accessor 
    {	return p[0];
    }
    
    int point::getp[1]()				// accessor
    {	return p[1];
    }
    
    int point::getp[2]()				// accessor 
    {	return p[2];
    }
    
    void point::setpoint( int newp1, int newp2, int newp3 )	// mutator 
    {	
    	p[3] = {newp1, newp2, newp3};
    }
    
    void point::setp[0]( int newp1 )			// mutator 
    {	p[0] = newp1;
    }
    
    void point::setp[1]( int newp2 )			// mutator 
    {	p[1] = newp2;
    }
    
    void point::setp[2]( int newp3 )			// mutator 
    {	p[2] = newp3;
    }
    
    void main()
    {	
    	point p[3]={(1,2,3)};
    
    	cout << "points: " << p[1].getp1   << endl;
    }
    could someone help me please ? i am struggling with this for quite a long time

    thanks!

    //edit

    ok i found some code snippet through google and by understanding that code, i have adjusted my code to this:
    Code:
    #include <iostream>
    #include <string>		// C++ string
    
    using namespace std;
    
    class point {
    public:
    	point();				// default constructor
    	point( int initp1, int initp2, int initp3);		// constructor
    	int getp1();			// accessor 
    	int getp2();
    	int getp3();
    	void setpoint( int newp1, int newp2, int newp3 );	// mutator 
    	void setp1( int newp1 );		// mutator p1
    	void setp2( int newp2 );		// mutator p2
    	void setp3( int newp3 );		// mutator p3
    private:
    	int p1;
    	int p2;
    	int p3;
    };
    
    point::point()					// default constructor
    {	
    	p1 = 0;
    	p2 = 0;
    	p3 = 0;
    }
    
    point::point( int initp1, int initp2, int initp3)		// constructor
    {	
    	p1 = initp1;
    	p2 = initp2;
    	p3 = initp3;
    }
    
    int point::getp1()				// accessor 
    {	return p1;
    }
    
    int point::getp2()				// accessor
    {	return p2;
    }
    
    int point::getp3()				// accessor 
    {	return p3;
    }
    
    void point::setpoint( int newp1, int newp2, int newp3 )	// mutator 
    {	
    	p1 = newp1;
    	p2 = newp2;
    	p3 = newp3;
    }
    
    void point::setp1( int newp1 )			// mutator p1
    {	p1 = newp1;
    }
    
    void point::setp2( int newp2 )			// mutator p2
    {	p2 = newp2;
    }
    
    void point::setp3( int newp3 )			// mutator p3
    {	p3 = newp3;
    }
    
    void main()
    {	
    	//point p[3]={1,2,3}; //error
    	point p[3];
    
    	cout << "points: " << p[1].getp1()   << endl;
    
    }
    but if i want to put values in the array p[3],
    it still doesnt allow me.
    Last edited by eastmus; 04-21-2009 at 05:39 PM. Reason: reason for editing: im halfway there, i found something on google

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    i found out i can use
    Code:
    void main()
    {	
    	point p[3];
    	p[1].setp1(1);
    
    	cout << "points: " << p[1].getp1()  << endl;
    
    }
    but its still not what i exactly want.
    I'd like something like
    Code:
    void main()
    {	
    	point p[3];
    	p[1]={1};
    
    	cout << "points: " << p[1].getp1()  << endl;
    
    }
    but that doesnt work T_T
    because i intend to add more values in that array

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    Creating an array involves the default constructor usually, so you would have to pick a mutator to change the elements.

    C++ makes it a little easier with vector:

    Code:
    #include <vector>
    
    // Declare point...
    
    int main() {
       std::vector<point> parray;
       parray.push_back( point() );
       parray.push_back( point( 1, 0, -1 ) );
       parray.push_back( point( -2, 1, 2 ) );
    
       // Just to prove it works:
       for( std::vector<point>::size_type i = 0; i < parray.size(); i++ ) {
          std::cout << parray[i].getp1() << std::endl;
       }
    
    }
    Notice what happens with point's other constructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic allocation of 2 dim. arrays in classes
    By circuitbreaker in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2008, 11:13 AM
  2. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM
  3. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 05:49 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21