Topic : C++ tutorial for C users
Author : Eric Brasseur
Page : << Previous 8  Next >>
Go to page :


is that inside every derived class definition all methods the sort function needs are correctly defined:


#include <iostream.h>
#include <math.h>

class octopus
{
public:

   virtual double module() = 0;  // = 0 implies function is not
                                 // defined. This makes instances
                                 // of this class cannot be declared.
};

double biggest_module (octopus &a, octopus &b, octopus &c)
{
    double r = a.module();
    if (b.module() > r) r = b.module();
    if (c.module() > r) r = c.module();
    return r;
}

class vector: public octopus
{
public:

   double x;
   double y;

   vector (double a = 0, double b = 0)
   {
      x = a;
      y = b;
   }

   double module()
   {
      return sqrt (x * x + y * y);
   }
};

class number: public octopus
{
public:

   double n;

   number (double a = 0)
   {
      n = a;
   }

   double module()
   {
      if (n >= 0) return n;
      else        return -n;
   }
};

void main ()
{
    vector k (1,2), m (6,7), n (100, 0);
    number p (5),   q (-3),  r (-150);

    cout << biggest_module (k, m, n) << endl;
    cout << biggest_module (p, q, r) << endl;

    cout << biggest_module (p, q, n) << endl;
}



Perhaps you think "okay, that's a good idea to derive classes from the class octopus because that way I can apply to instances of my classes methods and function that were designed a generic way for the octopus class. But what if there exists another base class, named cuttlefish, which has very interesting methods and functions too? Do I have to make my choice between octopus and cuttlefish when I want to derive a class?" No, of course. A derived class can be at the same time derived from octopus and from cuttlefish. That's POLYMORPHISM. The derived class simply has to define the methods necessary for octopus together with the methods necessary for cuttlefish:

class octopus
{
   virtual double module() = 0;
};

class cuttlefish
{
   virtual int test() = 0;
};

class vector: public octopus, public cuttlefish
{
   double x;
   double y;

   double module ()
   {
      return sqrt (x * x + y * y);
   }

   int test ()
   {
      if (x > y) return 1;
      else       return 0;
   }
}



28
Probably you wonder what all those public: keywords mean. They mean the variables or the methods below them may be accessed and used everywhere in the program.

If you want them to be accessible only to methods of the class AND to methods of derived classes then you must put the keyword protected: above them.

If you want variables or methods be accessible ONLY to methods of the class then you must put the keyword private: above them.

The fact variables or methods are declared private or protected means no function external to the class may access or use them. That's ENCAPSULATION. If you want to give to a specific function the right to access those variables and methods then you must include that function's prototype inside the class definition, preceded by the keyword friend.

29
Now let's talk about input/output. In C++ that's a very broad subject.

Here is a program that writes to a file:

#include <iostream.h>
#include <fstream.h>

void main ()
{
   fstream f;

   f.open("c:\\test.txt", ios::out);

   f << "This is a text output to a file." << endl;

   double a = 345;

   f  << "A number: " << a << endl;

   f.close();
}


Here is a program that reads from a file:

#include <iostream.h>
#include <fstream.h>

void main ()
{
   fstream f;
   char c;

   cout << "What's inside the test.txt file from";
   cout << "the C: hard disk root " << endl;
   cout << endl;

   f.open("c:\\test.txt", ios::in);

   while (! f.eof() )
   {
      f.get(c);                          // Or c = f.get()
      cout << c;
   }

   f.close();
}


30
Roughly said, it is possible to do on character arrays the same operations as on files. This is very useful to convert data or manage memory arrays.

Here is a program that writes inside a character array:


#include <iostream.h>
#include <strstrea.h>
#include <string.h>
#include <math.h>

void main ()
{
   char a[1024];
   ostrstream b(a, 1024);

   b.seekp(0);                           // Start from first char.
   b << "2 + 2 = " << 2 + 2 << ends;     // ( ends, not endl )
                                         // ends is simply the
                                         // null character   '\0'
   cout << a << endl;

   double v = 2;

   strcpy (a, "A sinus: ");

   b.seekp(strlen (a));
   b << "sin (" << v << ") = " << sin(v) << ends;

   cout << a << endl;
}


A program that reads from a character string:

#include <iostream.h>
#include <strstrea.h>
#include <string.h>

void main ()
{
   char a[1024];
   istrstream b(a, 1024);

   strcpy (a, "45.656");

   double k, p;

   b.seekg(0);                       // Start from first character.
   b >> k;

   k = k + 1;

   cout << k << endl;

   strcpy (a, "444.23 56.89");

   b.seekg(0);
   b >> k >> p;

   cout << k << ", " << p + 1 << endl;
}



31
This program performs formated output two different ways. Please note the width() and setw() MODIFIERS are only effective on the next item output to the stream. The second next item will not be influenced.


#include <iostream.h>
#include <iomanip.h>

void main ()
{
   int i;

   cout << "A list of numbers:" << endl;
   for (i = 1; i <= 1024; i *= 2)
   {
      cout.width (7);
      cout << i << endl;
   }

   cout << "A table of numbers:" << endl;
   for (i = 0; i <= 4; i++)
   {
      cout << setw(3) << i << setw(5) << i * i * i << endl;
   }
}



You now have a basic knowledge about C++. Inside good books you will learn many more things. The file management system is very powerful, it has many other possibilities than those illustrated here. There is also a lot more to say about classes: template classes, virtual classes...
In order to work correctly with C++ you will need a good reference book, just like you need one for C. You will also need information on how C++ is used in your particular domain of activity. The standards, the global approach, the tricks, the typical problems encountered and their solutions... The best reference is of course the book written by Bjarn Stroustrup himself. Following book contains almost every detail about C and C++ and is constructed a way similar to this text:

Jamsa's C/C++ Programmer's Bible
İright; 1998

Page : << Previous 8  Next >>