Hyluss

Dziedziczenie

May 4th, 2016
512
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. // DZiedziczeniemaceirze.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <malloc.h>
  6. #include <conio.h>
  7.  
  8.  
  9.  
  10. class Macierz
  11. {
  12. protected:
  13.     int wiersze, kolumny;
  14.     float **nxm;
  15.  
  16. public:
  17.     Macierz(const int k, const int w)
  18.     {
  19.         if ( (w<0) || (k<0) || (w>100) || (k>100) )
  20.         {
  21.                
  22.         }
  23.         else
  24.         {
  25.             wiersze = w;
  26.             kolumny = k;
  27.             nxm = (float**) malloc (sizeof (float*) * kolumny);
  28.             for (int i=0;i<kolumny;i++)
  29.             {
  30.                 nxm[i] = (float*) malloc (sizeof (float) * wiersze);
  31.             }
  32.  
  33.             for (int i=0; i<kolumny;i++)
  34.             {
  35.                 for (int j=0;j<wiersze;j++)
  36.                 {
  37.                     nxm[i][j] = 0;
  38.                 }
  39.             }
  40.  
  41.         }
  42.     }
  43.  
  44.     const int Wiersze () const
  45.     {
  46.         return wiersze;
  47.     }
  48.    
  49.     const int Kolumny () const
  50.     {
  51.         return kolumny;
  52.     }
  53.  
  54.     void Ustaw (const int k,const int w,const float value)
  55.     {
  56.         nxm[k][w] = value;
  57.     }
  58.  
  59.     const float Pobierz (const int k,const int w) const
  60.     {
  61.         return nxm[k][w];
  62.     }
  63.  
  64. };
  65.  
  66.  
  67.  
  68. class MacierzWewy : public Macierz
  69. {
  70. public:
  71.     MacierzWewy (const int k, const int w) : Macierz (k, w)
  72.     {
  73.     }
  74.    
  75.     void Wprowadz()
  76.     {
  77.         float temp;
  78.         for (int i=0;i<kolumny;i++)
  79.         {
  80.             for (int j=0;j<wiersze;j++)
  81.             {
  82.                 printf ("tab[%d][%d] - ",i,j);
  83.                 scanf ("%f", & nxm[i][j]);
  84.             }
  85.         }
  86.     }
  87.  
  88.     const void Wyswietl() const
  89.     {
  90.             for (int i=0;i<kolumny;i++)
  91.             {
  92.                 for (int j=0;j<wiersze;j++)
  93.                 {
  94.                     printf ("\ntab[%d][%d] - %.2f",i,j, nxm[i][j]);
  95.                 }
  96.             }
  97.     }
  98.  
  99. };
  100.  
  101.  
  102. class Macierz2x2 : public MacierzWewy
  103. {
  104. public:
  105.     Macierz2x2 (): MacierzWewy (2, 2)
  106.     {
  107.     }
  108.  
  109.     const float Wyznacznik() const
  110.     {
  111.         return ( ( nxm[0][0] * nxm[1][1] ) - ( nxm [0][1] * nxm [1][0] ) );
  112.     }
  113.  
  114.     const void Wyswietl ()
  115.     {
  116.         MacierzWewy::Wyswietl();
  117.         printf ("\nWyznacznik - %.2f", Wyznacznik() );
  118.     }
  119.  
  120. };
  121.  
  122. int _tmain(int argc, _TCHAR* argv[])
  123. {
  124.     Macierz mac(6,6);
  125.     mac.Wiersze();
  126.     mac.Kolumny();
  127.     mac.Ustaw(2,3,55.5);
  128.    
  129.     MacierzWewy wew (4,4);
  130.     wew.Ustaw(3,3,15.67);
  131.     wew.Wprowadz();
  132.     wew.Wyswietl();
  133.  
  134.     Macierz2x2 mala;
  135.     mala.Ustaw(1,1,1);
  136.     mala.Ustaw(0,1,2);
  137.     mala.Ustaw(0,0,3);
  138.     mala.Ustaw(1,0,4);
  139.     printf ("\nWyznacznik - %.10f\n", mala.Wyznacznik());
  140.     mala.Wyswietl();
  141.  
  142.     _getch();
  143.     return 0;
  144. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment