Questions on OOP (Object Oriented Programming) - page 9

 
Integer:

This means you have to create an instance using the new operator.
There's one thing I don't understand. What are the compelling reasons for using the new operator? You cannot do it without it, can you?
 
hoz:
There's one thing I don't understand. What good reasons are there to force the use of the new operator? Is it all right without it?


It is needed if you want to create objects dynamically. If you don't know in advance how many objects and what types of objects will be needed.

 
hoz:
Так я вот одного не пойму. Какие веские причины принуждают к использованию оператора new ? Без него типа никак?
Integer:
It's needed if you need to create objects dynamically. If you don't know beforehand how many objects and what types of objects are required.

You can use dynamic class arrays. In MQL this will work. In C++, there may be problems with class initialization of class members. That's why it's better to put a smart pointer in the array. So you don't have to deal with class initialization and memory clearing.

 
mql5:
class cFather
 {
 public:
 int GetData() {return 3;}
 };
 //+------------------------------------------------------------------+
 //| |
 //+------------------------------------------------------------------+
 class cChild : public cFather
 {
 public:
 int GetData() {return 5;}
 };

 int f(cFather *p) {return p.GetData();}
 //+------------------------------------------------------------------+
 //| |
 //+------------------------------------------------------------------+
 int OnStart()
 {
 cChild obj,*ptr=GetPointer(obj);
 f(ptr); // вернет 3
 ((cFather *)ptr).GetData(); // вернет 3

 return 0;
 }

How does this construction "read"?

((cFather *)ptr).GetData();

Ah, in particular:

(cFather *)ptr)
I'm looking at it and I don't get the gist of what's going on...
 
hoz:

How does this construction "read"?

((cFather *)ptr).GetData();

ptr is converted to cFather type (class) and its (cFather) method GetData() is called

something like this...

 
keekkenen:

ptr is converted to cFather type (class) and its (cFather) method GetData() is called

something like this...

That's a weird thing... I thought so approximately, but... it's too much to be cast to a type (class). In programming anyway, as far as I've heard, it is not recommended to cast to a type (class). But it's not just standard type conversion - it's rather class conversion.

Well, let's return to this code once again:

class cFather
 {
 public:
 int GetData() {return 3;}
 };
 //+------------------------------------------------------------------+
 //| |
 //+------------------------------------------------------------------+
 class cChild : public cFather
 {
 public:
 int GetData() {return 5;}
 };

 int f(cFather *p) {return p.GetData();}
 //+------------------------------------------------------------------+
 //| |
 //+------------------------------------------------------------------+
 int OnStart()
 {
 cChild obj,*ptr=GetPointer(obj);
 f(ptr); // вернет 3
 ((cFather *)ptr).GetData(); // вернет 3

 return 0;
 }

Here in the parameters of the f function prototype we can clearly see that the f function takes a method of the cFather class in the parameters. Further we see the following situation in the START code:

 f(ptr); // вернет 3

This is where the f function takes a pointer to the cChild class, rather than a method of the cFather class. Since cChild obj, *ptr = GetPointer( obj );

What's the logic behind this?

 

mm... logic where ?

in the f() method, I think the logic is that the incoming parameter will be handled correctly for cFather (or its descendants)

 
keekkenen:

mm... logic where ?

in the f() method, I think the logic is that the incoming parameter will be correctly handled for cFather (or its successors)

I.e.

 int f(cFather *p) {return p.GetData();}

here *p is a pointer to cFather?

At the same time, I'm studying C++ and am a bit confused by similar vocabulary. Pointers are not the same here though.

 
hoz:

I.e.

here *p is a pointer to cFather?

At the same time, I'm studying C++ and I'm a bit confused by similar vocabulary. The pointers here are not the same though.

cFather is a pointer type. The pointer variable itself points to an instance of the class.
 

It's been a couple of weeks since I've had a question. I decided to ask it. It's not too complicated, but logically I'm wondering if this is an option. Here is the code:

struct Name
  {
   string            first_name;                 // имя
   string            last_name;                  // фамилия
  };
class CPerson
  {
protected:
   Name              m_name;                     // имя
public:
   void              SetName(string n);
   string            GetName(){return(m_name.first_name+" "+m_name.last_name);}
private:
   string            GetFirstName(string full_name);
   string            GetLastName(string full_name);
  };
void CPerson::SetName(string n)
  {
   m_name.first_name=GetFirstName(n);
   m_name.last_name=GetLastName(n);
  }
string CPerson::GetFirstName(string full_name)
  {
   int pos=StringFind(full_name," ");
   if(pos>0) StringSetCharacter(full_name,pos,0);
   return(full_name);
  }
string CPerson::GetLastName(string full_name)
  {
   string ret_string;
   int pos=StringFind(full_name," ");
   if(pos>0) ret_string=StringSubstr(full_name,pos+1);
   else      ret_string=full_name;
   return(ret_string);
  }

There are only 2 public functions in this code:

public:
   void              SetName(string n);
   string            GetName(){return(m_name.first_name+" "+m_name.last_name);}

And the 2 private functions are called directly through the public function, or rather the public function, at the moment of need. What is interesting is that the void SetName(string n) function has the parameter n, but both private methods private: string GetFirstName(string full_name) and string GetLastName(string full_name) for some reason do not have the same parameter. But if you take this very code (not the other one), the private methods are called through the public ones with the same parameters and there is no other way. So why should I give different names to the parameters?

I hope the question is clear.

Reason: