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

7. Give an example of C++ code that creates an alias. 8. In the following C++ fu

ID: 3559884 • Letter: 7

Question

7. Give an example of C++ code that creates an alias.

8. In the following C++ function, state where stack-dynamic and explicit heap-dynamic variables are being created:

int* maker()

           {

                       int x;

              int *pInt;

                     x = 3;

              pInt = new int;        

              *pInt = x;

              return pInt;

           }

9. According to the text, a language is strongly typed if:

a. it is specifically designed for one purpose

b. type errors are always detected

c. the type for all variables must be declared

d. it uses coercion to force agreement of operators

10. Consider the following program:

program main;

var x, y, z : integer;

procedure sub1;

var a, y, z : integer;

procedure sub2;

var a, b, z : integer;

begin                          { of sub2 }

                       { sub2 processing }

end;                            { of sub2 }

           begin                           { of sub1 }

      { sub1 processing }

end;

     

procedure sub3;

var a, x, w : integer;

begin                          { of sub3 }

      { sub3 processing }

end;                            { of sub3 }

           begin                           { of main }

      { main processing }

end.

List all variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3. Assume static scoping is used.

Explanation / Answer

7)

There are a few concepts in C/C++ that get called "Aliases".

#1 namespace alias -- A way of simplifying complicated namespace names. I don't think this is standard, but many of the major compilers implement it.

namespace I_AM_A_LONG_NAMESPACE {
...
};

namepace IAM = IAMALONGNAMESPACE; // <-- The alias


#2 Type Aliases. Generally when you create a struct or union like so:

struct SomeStruct {
int a;
int b;
};

To use this struct you have to declare each variable as:

struct SomeStruct var1;
struct SomeStruct *var2; //pointer to a SomeStruct struct.

but you can use a typedef to create an alias to simplify the syntax:

typedef struct SomeStruct {
int a;
int b;
} MyStruct;

MyStruct var1;
MyStruct *var2;

#3 Pointer Aliasing/Referances -- When you use a pointer or a referance to point to another variable:

int a = 5;
int alias = a;
int *ptr = &a;

alias++;
std::cout << "a = " << a << std::cout;
*ptr++;
std::cout << "a = " << a << std::cout;

Basically an alias is any time when you can give something a new name. There are quite a few ways to do this in C/C++ -- for example you can use the preprocessor to alias all kinds of things.

9. According to the text, a language is strongly typed if:

a. it is specifically designed for one purpose

10.    Consider the following skeletal program:

procedure Main is

     X, Y, Z : Integer;

procedure Sub1 is

          A, Y, Z : Integer;

          begin -- of Sub1

         

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote