Thread: i need help understanding classes

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    1

    i need help understanding classes

    I really need help with this class I'm in. I understand the basic principles behind the classes, but I'm unsure how to translate what I want to do into code. On one particular project I need to create a user defined matrix, and then give the user the option to add, subtract, multiply, or print the matrix. We are supposed to use classes, but I don't know how to create the classes. I don't know if they go in the header file or the cpp file. I haven't used C++ in 4 years and I'm totally lost. I'm including what I have so far, so if anyone can give me any advice, i would be thankful.

    code:

    header file
    __________________________________________________
    Code:
    #ifndef MATRIXTYPE_H_
    #define MATRIXTYPE_H_
    	#include <fstream>
    	#include <iostream>
    	const int MAX_ROWS = 10;
    	const int MAX_COLS = 10;
    	class MatrixType
    	{
    	public:
    		MatrixType();
    		void MakeEmpty();
    		void SetSize(int rowSize, int colSize);
    		void StoreItem(int item, int row, int col);
    		void Add(const MatrixType& otherOperand, MatrixType& result);
     		void Sub(const MatrixType& otherOperand, MatrixType& result);
     		void Mult(const MatrixType& otherOperand, MatrixType& result);
    		void Print(std::ofstream& outfile);//Also echo print to console
    		bool AddSubCompatible(const MatrixType& otherOperand);
     		bool MultCompatible(const MatrixType& otherOperand);
    	private:
    		int values[MAX_ROWS][MAX_COLS];
    		int numRows;
    		int numCols;
    	};
    #endif
    __________________________________________________ __

    .cpp file
    __________________________________________________ __
    Code:
    #include “MatrixType.h"
    int main()
    {
    	bool done=false;
    	MatrixType TheMatrix[10];	
    	//Initialize the program as needed
    	while(!done)
    	{
    		//Print menu choices
    
    		//InitMatrix(index), sets number of rows and columns
    		//AddMatrices(indexleft, indexright, indexresult)
    		//SubMatrices(indexleft, indexright, indexresult)
    		//MultiplyMatrices(indexleft, indexright, indexresult)
    		//PrintMatrix(index)
    		//Quit
    		//Get input
    		//Perform task
    	}
    	return 0;
    }
    __________________________________________________ _

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Good start. Now you can define your functions (probably in a cpp file), as follows:

    Code:
    return_type class_name::function_name(args ...)
    {
    }
    
    // For example:
    void MatrixType::makeEmpty()
    {
    ...
    }
    
    ... or ... 
    
    void MatrixType::Add(const MatrixType& otherOperand, MatrixType& result)
    {
    ...
    }
    You may want to look into operator overloading as well, since you would be able to add to matrices (a and b), by simply using 'a + b' for example.

    Also, in your arithmetic functions, you might want to return the result, rather than making the user pass it to the function.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes partial understanding
    By Boomba in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2004, 07:44 AM
  2. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 12:44 PM
  3. I'm having a really hard time understanding classes.
    By imortal in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2003, 01:02 PM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM
  5. Fully understanding classes ...
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 08-14-2002, 11:48 AM