Thursday, November 2, 2017

Questions

Questions.
Use this post to ask questions regarding the part we have covered so far.

12 comments:

  1. WOULD YOU PLEASE POST SOME PRACTICING PROBLEM?
    THANK U.

    ReplyDelete
    Replies
    1. Try to practice on the problems we covered in the lab.

      Delete
  2. The Statement : "delete Pa ;" from the code in lecture 6
    is it means Deletion for the object the pointer points to & pointer also

    or just deletion for the dynamically allocated object ? without pointer

    ReplyDelete
    Replies
    1. The object is deleted. Technically speaking, the pointer is a variable that holds the memory address of a certain type, and is statically allocated in the mentioned example. Therefore, it does not require deletion. There is a way to dynamically create pointers, which we have not covered in the course. Great question!

      Delete
  3. in the last portion of Lecture 6 it is mentioned that with primitive data type pointers (int, double...etc) the "object" which the pointer points to is automatically created with the pointer, does this mean that if a class has a pointer ,let's assume of type integer, we don't need to override the class constructor and destructor? (no dynamic allocation and deallocation is needed?)

    ReplyDelete
    Replies
    1. Even though the rule still applies when you have a pointer to a primitive type as member of a class, eventually the copy constructor will have to be overridden to achieve proper member copying, which involves dynamically creating a primitive type with new. Therefore, an object created using the copy constructor will not delete the dynamically primitive type if its destructor is the default destructor. This makes overriding the destructor to deal with the possibility of a dynamically primitive member a must. But having a destructor which deletes primitive types as if they were dynamically allocated necessitates that we make sure that the default constructor creates the primitive type dynamically. See? It's all set up.
      That's a very deep and penetrating question. Keep them coming!

      Delete
  4. In the Concept of inheritance do we concern the situation when our base class is a deep class or not ?

    ReplyDelete
    Replies
    1. They are unrelated topics. If you have a deep class in the context of inheritance, just do what it takes for deep classes to work properly.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. when we have deep class inherits a deep class , how should we built the copy constructor ? should we use the initialization list ?

    ReplyDelete
    Replies
    1. You need to use the copy constructor of the base class in the initialization and pass the object being copied to it. The object will get sliced and treated as a base class type. The following code illustrates. Good question.

      #include

      using namespace std;

      class A
      {
      public:int i;
      };
      class B:public A
      {
      public:int k; B(){};
      B (const B & og):A (og), k (og.k)
      {
      };
      };

      int
      main ()
      {
      B b;
      b.i = 99;
      b.k = 22;
      B n (b);

      cout << n.i;
      cout << n.k;

      return 0;
      }

      Delete
  7. This comment has been removed by a blog administrator.

    ReplyDelete