Question

In: Computer Science

execute coding examples that demonstrate the 4 scope rules file,function,class,block coding language c++

execute coding examples that demonstrate the 4 scope rules

file,function,class,block

coding language c++

Solutions

Expert Solution

1. File scope example:

#include<iostream>

using namespace std;

int main()

{

{

      int x = 10, y = 20;

      {

        // there is declaration of x and y in outer block

        // this statement is work and print x=10 and y=20

        cout<<"x = "<<x;

        cout<<", y = "<<y<<endl;

          {

              // here, y is declared again and it can't

              // access outer block y

              int y = 40;

    

// increment the outer block variable, x to 11

              x++;  

// increment this block's variable y that is 41 now

              y++;

          // display x and y

        cout<<"x = "<<x;

        cout<<", y = "<<y<<endl;

          }

          // here, it will access only

          // outer x and y

        cout<<"x = "<<x;

        cout<<", y = "<<y<<endl;

      }

}

return 0;

}

OUTPUT:

------------------------------------------------------------------------------------------

Block scope:

#include<iostream>

using namespace std;

int main()

{

{

      int x = 10;

}

{

      // this will give error, becuase it cant access x

      cout<<"x = "<<x;

}

return 0;

}

-----------------------------------------------------------------------------------------------

Function Prototype scope:

#include<iostream>

using namespace std;

int main()

{

    // local variables

    int x = 3, y = 5, z = 7;

    cout<<"x = "<<x;

    cout<<", y = "<<y;

  cout<<", z = "<<z<<endl;

    {

        // change the value of variable x and y

        int x = 10;

        float y = 20;

         

    cout<<"x = "<<x;

    cout<<", y = "<<y;

    cout<<", z = "<<z<<endl;

        {

            // change the variable z

            int z = 100;

    cout<<"x = "<<x;

    cout<<", y = "<<y;

    cout<<", z = "<<z<<endl;

        }

    }

    return 0;

}

-------------------------------------------------------------------------------

Function scope:

YOu can use block space example in this function scope.

There willl be no change.

----------------------------------------

PLEASE UPVOTE


Related Solutions

This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right?...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right? There are 5 objectives and 20 points each. Please indicate in code (or comments) where each objective begins and ends. It may be prudent to place each objective in it's own function. In this lab you will be reading in a file of student results. The students each ran 3 laps in a race and their times to complete each lap are posted in...
An invoice file would best be coded using a(n) coding scheme. a alphabetic b. mnemonic c. block d. sequential
An invoice file would best be coded using a(n) coding scheme. a alphabeticb. mnemonicc. blockd. sequential
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
JAVA LANGUAGE coding required. You are to take the payroll file called DeweyCheatemAndHow.txt to use as...
JAVA LANGUAGE coding required. You are to take the payroll file called DeweyCheatemAndHow.txt to use as input to your program and produce a file that lists all the employee and their gross pay. At the end of the file you are to show the average gross pay for each type of employee. In order to do this, you will need to create the following classes: Employee:             Attribues:                         First name                         Last name                         Address                         Phone                         SSN             Methods:                         Appropriate constructors                         Set methods for...
C++ // Rectangle.cpp is the Rectangle class function implementation file. #include "Rectangle.h" /******************************************************************* * Rectangle::setLength *...
C++ // Rectangle.cpp is the Rectangle class function implementation file. #include "Rectangle.h" /******************************************************************* * Rectangle::setLength * * If the argument passed to the setLength function is zero or * * greater, it is copied into the member variable length, and true * * is returned. If the argument is negative, the value of length * * remains unchanged and false is returned. * *******************************************************************/ bool Rectangle::setLength(double len) { bool validData = true; if (len >= 0) // If the len...
Class object in C++ programming language description about lesson unary overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT