Thread: 5 questions

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    5 questions

    Hey I'm reviewing for a test I have in one of my classes and I'm looking at review questions from the book and from what the teacher said should be known for the test. I was hoping i could write the question then my answer and someone could tell me if i'm right or what needs to be added to know. I'll do a question at a time to make it easier.
    When creating a class that uses dynamically allocated memory what three member functions must you provide besides the constructor. For each of the three know why it is necessary.

    My answer is:
    A class destructor, a deep copy operation, and a class copy-constructor are the three member functions needed. A destructor is necessary because it implicitly gets invoked when the class object is destroyed. A class object is destroyed when it goes out of scope. With a destructor the memory from the destroyed class object is deallocated freeing up space in the free store for other operations that require dynamic data to use. A deep copy operation is needed because when assigning one class object to another you want to copy not only the class members but also the data pointed to by the class object. This will leave two identical but separate class objects each with identical but separate class members and data. A copy-constructor is needed when there is only one class object to begin with and that one class object is initialized by another. With a copy constructor present a copy of a class object is made to a new class object identical to the first. Then when modifying the data of that object, you are actually working on a copy of the data, rather than the original. If there was no copy constructor present a new class object would be made but it would point to the same data as the original, thus when modifying the data in it you would be modifying the original data not a copy of it,which could make the data corrupt or useless.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sounds fine to me, though I believe "deep copy" refers to what the copy assignment operator should do, and it is the "copy assignment operator" that is one of the member functions in question.

    Though note that all three are provided by default when needed. So to elaborate on why a destructor should be defined, note that the compiler generated destructor would not take care of deallocating such dynamically allocated memory.
    Last edited by laserlight; 04-15-2007 at 12:19 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    These two questions I couldn't come up with an answer for, maybe someone could point me in the right direction or something.
    1) Consider implementing a multiset as a linked list that only has a head pointer. Overload the += operator to take another multiset as an argument and append copies of all the members of the argument to the current multiset.
    a) describe the steps the function must take to implement this operator.
    b) what is the running time of the += operation?

    2)Consider a template class, DLListNode, used when implementing doubly linked lists. Create a complete header file for this class that contains at least.
    -the macro guard
    -default contructor
    -a constructor that takes arguments that allow a node to be immediately linked into a list, similar to other node constructors
    -a destructor
    -member functions to set and retrieve the private data member values
    -where appropriate use the const keyword and make the parameters and return value references
    -a private data member called data of type T
    -private data members that are links to the previous and next nodes in the doubly linked list
    -all necessary #includes

    for this question i have some parts not sure if they are right maybe someone can check
    Code:
    #ifndef DLListNode_H
    #define DLListNode_H
    #include <iostream>
    //not sure if i should include anything else
    template <class T>
    class DLListNode
    {
        public:
                     DLListNode(); //default constructor
                     DLListNode(const T&);//constructor tht knows the data but not what it points to
                     DLListNode(const T&, DLListNode<T>*,  DLListNode<T>*); //constructor that knows the data and the two pointer values
                     ~DLListNode(); //class destructor
                     //what i need now are functions that set and retrieve the private data member values however i am unsure what this means
                     //because i think it would be the same thing as the constructors
    
        private: 
                       T data;
                        DLListNode<T>* nextPtr;
                        DLListNode<T>*prevPtr;
    };
    #include "DLListNode.template"
    As i said out of the 5 questions, one i posted and the other two involved drawing which i can't obviously post to see if i'm right, these two questions i don't know how to do/answer and i need to know how to for my upcomming test. so maybe someone can point me in the right direction as i said above or give me the answer so i can study it later. thanks in advance.
    Last edited by DarkDot; 04-15-2007 at 07:31 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://cboard.cprogramming.com/showthread.php?t=88723
    http://cboard.cprogramming.com/showthread.php?t=88721
    This is the first question, and already there are multiple threads for it.

    We're not here to write essay answers to your essay questions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM