Questions on OOP (Object Oriented Programming) - page 3

 
tara:

Add.

I added ))
 
EverAlex:

For a circle it is calculated one way, for a square another. But in either case - by calling Figura.GetSquare(),

I for example find it hard to learn the theory, demonstrate an example, and describe how the function defines a circle, square, trapezoid, or triangle ???
 
And silence ...
 
VOLDEMAR:
And silence ...

Well, it was a 2x2=?

Not interested in answering.

=================

Maybe this is closer.

There is a standard set of virtual methods in a class: Open(), Close(), Read(), Write() etc.

Such class can handle files, mapping, channels, internet, etc.

Only the contents (description) of these methods will differ. But the class interface will be identical.

 

Don't you think it's suspicious that it's already the 3rd page on the subject and no one cited a single code point as an example?

Besides, it's not interesting to answer, it's elementary, it's so simple that it's too lazy to code...

If someone knew something, I think they would have answered...

 

VOLDEMAR, if you take your first post, why is there a class there at all? Writing classes is a consequence of program complexity, when individual elements have many external links, then a class is created to unite all these elements and create a simple interface. In my opinion, a necessary attribute of a class is data (int's, double's ...) - material with which functions work, the class connects all in one package (data and functions).I think this is the basic idea of OOP. For example, two examples, first without OOP, second with it:

void sum(int &i1, int &i2, int val)
{
    i1 += val;
    i2 += val;
}
int get_number(int &i1, int &i2)
{
    return i1 + i2;
}
void main()  // главная функция программы
{
    int i1, i2;
    sum(i1, i2, 56);
    any_fn(get_number(i1, i2));   // какое-то действие с нашей суммой.
}
class QQ
{
    int i1;
    int i2;
public:
    void sum(int val)
    {
        this->i1 += val;
        this->i2 += val;
    }
    int get_number()
    {
        return this->i1 + this->i2;
    }
};

void main()  // главная функция программы
{
    QQ q;
    q.sum(56);
    any_fn(q.get_number());   // какое-то действие с нашей суммой.
}

In the second example, we don't have to worry about data that functions will work with, I think this is the main purpose of OOP.

 
Why does the second method throw up a bunch of errors and warnings ???
 

One more comment about your code:

...
class vr_trade
  {
   ...
                    ~vr_trade(){}
  };
...

I have a rule: never write a destructor empty. The absence of a destructor is an indicator of class simplicity. If a destructor is written, you may have to write a copy constructor and the = operator or forbid them. Example:

struct S {};
class QQ
{
    S *p;
public:
    QQ() {this->p = new S;}
    ~QQ() {delete this->p;}
};
void main()
{
    QQ q;
    QQ q2 = q;
}

This will cause delete to be called twice for the same pointer. The correct way is this

class QQ
{
    S *p;
public:
    QQ() {this->p = new S;}
    ~QQ() {delete this->p;}
    QQ(QQ &);            // Только объявляем, определения нигде нет.
    QQ &operator=(QQ &); // Только объявляем, определения нигде нет.
};

//или
class QQ
{
    S *p;
public:
    QQ() {this->p = new S;}
    ~QQ() {delete this->p;}
    QQ(QQ &q)               {this->p = new S(*q.p);}
    QQ &operator=(QQ &q)    {*this->p = *q.p;}
};

So, if you have to write a destructor, it's a good reason to think twice - what to do with opera = and copy constructor? Delete it, write it...? I don't think you should write destructor empty, its absence is an indicator that you don't need to redo the above.

 
VOLDEMAR:
Why does the second method throw a bunch of errors and warnings ???

Put a dot after this instead of ->. mql chip.
 

Please explain the actions

struct complex
  {
   double            re; // действительная часть
   double            im; // мнимая часть
   //--- конструкторы
                     complex():re(0.0),im(0.0) {  }
                     complex(const double r):re(r),im(0.0) {  }
                     complex(const double r,const double i):re(r),im(i) {  }
                     complex(const complex &o):re(o.re),im(o.im) { }
   //--- арифметические операции
   complex           Add(const complex &l,const complex &r) const;  // сложение
   complex           Sub(const complex &l,const complex &r) const;  // вычитание
   complex           Mul(const complex &l,const complex &r) const;  // умножение
   complex           Div(const complex &l,const complex &r) const;  // деление
  };

namely

                     complex():re(0.0),im(0.0) {  }
                     complex(const double r):re(r),im(0.0) {  }
                     complex(const double r,const double i):re(r),im(i) {  }
                     complex(const complex &o):re(o.re),im(o.im) { }

why the sign (:) and what do we get with it?

Reason: