Thread: classes and objects

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    6

    classes and objects

    Hi to all ,

    Ive written a program in c++ using structures not classes.
    Its basically a program that compares and performs diverse operations between 2 or more matrix's.

    Since my program is working I decided to unleash the full power of c++ and use a matrix class.

    As i gathered objects can be created and passed to functions etc.
    So i created object A and object B to be two matrix's of my matrix class.

    My problems are the following:

    1, I obviously need to compare and operate between the two matrix objects but unfortunately i cannot seem to be able to do that using my class methods. eg. i have a method to add matrix A to matrix B but I can only call the method with one object and i dont even know if its possible to call the method with both.

    2. I tried taking my method out of the Matrix class and declaring it as a function but my compiler tells me that once out of the 'main' function it doesnt know what objects A and B are anymore. So I wonder if I need to include the following in each function I use:
    <code>
    Matrix A;
    Matrix B;
    </code>
    Not that i think it really matters for such basic programming but Im using Borland c++ 5.02.

    As you can see I would just like some advice on how to use classes and their objects.
    Any help is appreciated thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would urge you to show some code.
    From what I gather, you have two problems:
    - 1) You cannot sum two matrix classes. Solution: The addition function should take the right-hand object. For example: void Matrix::add(const Matrix& OtherMatrix). Then simply do A.add(B);
    - 2) Scope issue. A and B are defined in main, so they cannot possibly exist in other functions, outside main. Learn to pass objects by argument to other functions so they can operate on the objects.

    You probably want to learn operator overloading, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    Since its all theoretical I didnt feel the need to post code since the problem isnt actually the code its my knowledge of the code.

    I think you actually gave me a solution to my question and you have shown me how.

    I knew there must be a way to compare 2 objects to produce a third.

    I will try your suggestions. thankyou.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It may be theoretical, but seeing as you want to "unleash the full power," I'm sure there are a lot of things that could be improved. Seeing your questions here, I started to wonder how many things you were doing "the right way."
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    You are right of course. I will post my new matrix class and definitions as soon as i have included the advice you have given me. I am sure, as usually happens with such things, that one solved problem will lead to another unsolved problem due to not doing things 'the right way'.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    Here is the messy code im using to test the Matrix class:


    Code:
    ////////////////////////////////////////////////////////////////////////////////
    class Matrix {
    
    	private:
    
    
    
       	int M_R;                // Number of rows
    
       public:
    		//Matrix();					// Constructor
    		~Matrix(){}; 				// Destructor
          int M_C;                // Number of columns is only public for debugging
          int MxN[5][5];          // Cell values (m*n)
          // Methods
    		void Input_MxN(void);   // Values for matrix cells
    		void Size(void);        // Size of matrix
          void Ma_comp(void);
          void add(const Matrix& B,const Matrix& A);
    
    }; //Matrix
    ////////////////////////////////////////////////////////////////////////////////
    void Matrix::Input_MxN(void){
    
    Size();
    if (M_C > 5){
    	cout << "no";
    }else{
    	cout << "M_C:" << M_C;
    }
    
    cout << endl << "MC:" << M_C << endl;
    getch();
    
    } 	// Input MxN
    ////////////////////////////////////////////////////////////////////////////////
    void Matrix::Size(void) {
    
    clrscr();
    cout << "number of columns:";
    cin >> M_C;
    clrscr();
    cout << "number of rows:";
    cin >> M_R;
    
    } 	// Size
    ////////////////////////////////////////////////////////////////////////////////
    void Matrix::Ma_comp(){
    Matrix A;
    A.Input_MxN();
    Matrix B;
    B.Input_MxN();
    clrscr();
    int x = (A.M_C-B.M_C);
    cout << x;
    getch();
    }
    ////////////////////////////////////////////////////////////////////////////////
    void Matrix::add(const Matrix& B,const Matrix& A){
    
    
    
    for (int i=0; i < M_R+1; i++){
    	for (int j=0; j < M_C+1; j++){
       	cout << "Matrix A:" << A.MxN[i][j] << endl;
          cout << "Matrix B:" << B.MxN[i][j] << endl;
    		MxN[i][j]=(A.MxN[i][j]+B.MxN[i][j]);
       	cout << "Matrix C:" << A.MxN[i][j] << endl;
       }
    }
    getch();
    }
    ////////////////////////////////////////////////////////////////////////////////
    int main(void){
    
    Matrix C;
    Matrix A;
    A.MxN[5][5]=(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    A.Input_MxN();
    Matrix B;
    B.MxN[5][5]=(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    B.Input_MxN();
    clrscr();
    cout << "A:" << A.Matrix::M_C << endl;
    cout << "B:" << B.Matrix::M_C << endl;
    getch();
    C.add(B,A);
    
    } // Main
    /
    AS you can see Im adding the values of MxN of objects A and B to MxN of object C.
    This code actually compiles but doesnt produce the desired output.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For one thing, you badly need better indentation. Better fix that now before anything else.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Survey says: matrix initialization failure!

    Code:
    A.MxN[5][5]=(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    Nope not gonna do it. Which you'd be more likely to realize if you turned warnings on/up:

    Code:
    ma.cpp: In function ‘int main()’:
    ma.cpp:80: warning: left-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:80: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: left-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect
    ma.cpp:83: warning: right-hand operand of comma has no effect

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    Firstly this is just my messy test code not the actual program itself.
    The indentation is careless, non the less, when i copied and pasted the code the indentation was ignored and changed when posted. Dont ask me why.
    I only made this small program in 10 minutes this morning because i was asked to post some code.
    I have no intention of wasting time indenting when i will cut, copy, paste, add and delete for different tests before I actually delete the whole thing when I have solved the problem before mentioned.
    The code:
    Code:
    A.MxN[5][5]=(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    is useless its just for the purpose of demonstration. The actual program requires user input for initialization of obj.MxN. If this is the reason the output of my program isn't the desired could someone actually guide me in the right direction rather than trying to teach me etiquette.
    thanks in advance.

    GoldenBoy.

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    So what is your input and what is your output? And what did you expect as output instead?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    Basically I want to create three objects of the matrix class. objects A and B have all array values of 1. object C is the sum of A and B therefore the output of all array values of C should be 2. This is just for example's sake and summarizes the basic problem i have dealing with objects which is... basically... i don't know how!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    GoldenBoy, replace all your tabs and spaces with ONE. Either tabs or spaces. Then you won't have problems pasting code over here. And please post your real code so that we don't have to guess, because there are obviously problems with your demonstration code!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Quote Originally Posted by GoldenBoy View Post
    objects A and B have all array values of 1.
    rags_to_riches already told you, that you fail at initializing your matrices to 1. The line you quoted doesn't do what you think it does. First, make sure your matrices actually have "all array values of 1."
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code to dump a classes objects into an array?
    By Xanderbeard in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2010, 02:46 PM
  2. Classes & Objects [c++]
    By salmansalman in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2008, 08:02 AM
  3. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  4. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 11:01 PM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM