Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1)Define a function template for a function cast() that given one element of a g

ID: 3599631 • Letter: 1

Question

1)Define a function template for a function cast() that given one element of a given type (type1) cast it to another type(type2) in c++?

2)How do you instantiate the functions for casting unsigned char to double in c++?

3)Create the template of a class to store dynamic arrays of objects of array class, implement access and set methods and constructors and destructors in c++?

4)Implement a class that implements the dynamic array of chars(c++ string) using a c style char array allocated dynamically.Provide constructor, destructor and set/get(val,index) in c++?

5)Consider the structure,where Node Values is a constant:

struct tree_node{

int values[Node Values];

struct tree_node*left;

struct tree_node*right;

};

Define a c++ function getNewNode() that returns new tree node with fields initialized to 0 and null.Avoid returning large data structures by value.

Explanation / Answer

2)

}

3)Constructor and Destructor

Classes can have complicated internal structures, so object initialization and clean-up of a class is much more complicated then for any other data structures. Constructors and destructors are special member functions of classes that are used to construct and destroy class objects. The construction can be for example: initialization for objects or memory allocation. The destruction may involve de-allocation of memory or other clean-up for objects.

Constructors and destructor’s are declared within a class declaration (as like any other member function). A constructor or a destructor can be defined in-line or external to the class declaration. We may declare some default arguments when we make a constructor. There are some restrictions that apply to constructors and destructors:

Constructors are called automatically by the compiler when defining class objects. The destructor’s are called when a class object goes out of scope.

So let’s take a look at an example:

4)  

       

The main use of the concept of dynamic memory allocation is for allocating arrays when we have to declare an array by specifying its size but are not sure about the size.

Consider a situation when we want the user to enter the name but are not sure about the number of characters in the name that the user will enter. In that case, we will declare an array of characters for the name with some array size such that the array size should be sufficient enough to hold any name entered. Suppose we declared the array with the array size 30 as follows.

char name[30];

And if the user enters the name having only 12 characters, then the rest of the memory space which was allocated to the array at the time of its declaration would become waste, thus unnecessary consuming the memory.

In this case, we will be using the new operator to dynamically allocate the memory at runtime. We use the new operator as follows.

char *arr = new char[length];

Let's see an example to understand its use.

5)

    

int findMin(struct Node* root)

{

    // Base case

    if (root == NULL)

      return INT_MAX;

    // Return minimum of 3 values:

    // 1) Root's data 2) Max in Left Subtree

    // 3) Max in right subtree

    int res = root->data;

    int lres = findMin(root->left);

    int rres = findMin(root->right);

    if (lres < res)

      res = lres;

    if (rres < res)

      res = rres;

    return res;

}