Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
class Amatrix
 {
  protected:
                int rows, cols;
                double *elements;
public:
         matrix(int r, int c);
        ˜matrix();
         int get rows();
         int get cols();
         void fill(double value);
         matrix operator+ (const matrix &C)
};
class squarematrix : public Amatrix
 {
   private:
   protected:
   public:
            squarematrix(int r, int c) : matrix(r,c)
 {
   if(r!=c) std::cerr<<"not a square matrix"; exit(1);
 }
  double ()
 {
  double sum(0.0);
  for(int i=0; i <rows ; i++)
  sum += elements[i*cols+i];
 return sum;
 }
   };
class vectormatrix : public matrix
{
}
Posted
Updated 3-Dec-11 6:04am
v4
Comments
Richard MacCutchan 3-Dec-11 12:22pm    
Why do you allow specification of both rows and columns for squarematrix? Just allow a single value and then no need to test for equality.
carlmack 3-Dec-11 12:59pm    
can you explain further i am sure i dont follow
Richard MacCutchan 3-Dec-11 13:10pm    
You have a constructor for squarematrix that accepts an integer value for rows and an integer value for columns. You then check if the values are equal and reject it if they are not. Since it is a square then there is no point allowing the user to enter both values, just use a single integer and use that for the number of rows and for the number of columns.
carlmack 3-Dec-11 14:20pm    
crystal clear Ritchie
thank you
this program is structured to allow for: prompt user to enter information,
this is a provision made in the event the user input elements the result in a not square matrix, hence rather than leaving it to user discretion i have a test to ensure,
Richard MacCutchan 3-Dec-11 15:09pm    

prompt "how many elements per side?"
input number_of_elements
squarematrix sm = new squarematrix(number_of_elements)

Easy!

1 solution

Please see my solution saw this online, seem just like the structure i wanna use in my code except i would wish to expand this to do matrices operation to20*20 with +, -,/, and * i have a base class electrical class ...[^].

The same idea applied to this question. Making a square matrix a class derived from matrix is wrong in principle, because a square matrix is just a special case of a matrix. It does not introduce or override any operations. You only need a special constructor or a static factory method. Please see the above link.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900