Recently Added Questions & Answers

 Defination of constructor:
Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructor’s initialize values to data members after storage is allocated to the object. While defining a constructor you must remember that the name of constructor will be same as the name of the class, and constructors never have return type. Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator.
Constructors are of four types :
1. Default Constructor 2. Parameterized Constructor 3. Copy Constructor 4. Explicit Constructor
1. Default Constructor in C++
Default constructor is the constructor which doesn’t take any argument. It has no parameters in default constructors Syntax : class_name () { Constructor Definition } Following program demonstrates the default constructor:

#include<iostream>
 using namespace std;
 class areaRect
 {
     private:
         int h, w;
     public:
     areaRect()
     {
         h = 0;
         w = 0;
     }
     int area()
    {
     cout<<"Enter the height of rectangle: ";
     cin>>h;
     cout<<"Enter the width of the rectangle: ";
     cin>>w;
     return h*w;
   }
 };
 int main()
 {
     int result;
     areaRect a1;
     result = a1.area();
     cout <<endl<<"Area of Rectangle is: "<<result;
 }

Output:
Area of Rectangle is: 0
2. Parameterized Constructor in C++
These are the constructors with parameters. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as an argument. Syntax : class_name () { Constructor Definition } Following program demonstrates the parameterized constructor:

#include<iostream>
 using namespace std;
 class areaRect
 {
     private:  int h, w;
     public:
     areaRect()
     {
         h = 0;
         w = 0;
     }
     areaRect(int x, int y)
     {
         h = x;
         w = y;
     }
     int area()
     {
         return h*w;
     }
 };
 int main()
 {
     int result;
     areaRect a1;
     result = a1.area();
     cout <<endl<<"Area of Rectangle is: "<<result;
     areaRect a2(10, 20);
    result = a2.area();
    cout <<endl<<"Area of Rectangle is: "<<result;
 }    

Output:
READ C++ program to read and print Student information Area of Rectangle is: 0 Area of Rectangle is: 200 3. Copy Constructor in C++
Copy constructor is a special type of constructor in which new object is created as a copy of an existing object. The copy constructor is called whenever a new variable is created from an object. Syntax : class_name (class_name &obj) { Constructor Definition } Following program demonstrates the copy constructor:

#include<iostream>
 using namespace std;
 class areaRect
 {
     private:  int h, w;
     public:
     areaRect()
     {
         h = 10;
         w = 10;
     }
     areaRect(areaRect &obj)
     {
         h = obj.h;
         w = obj.w;
     }
     int area()
     {
         return h*w;
     }
 };
 int main()
 {
     int result;
     areaRect a1;
     result = a1.area();
     cout <<endl<<"Area of Rectangle is: "<<result;
   areaRect a2(a1);
    result = a2.area(); 
    cout <<endl<<"Area of Rectangle is: "<<result;
 }    

Output:
Area of Rectangle is: 100 Area of Rectangle is: 100
4. Explicit Constructor in C++
In C++, compiler is allowed to make one time type conversion to resolve the parameters to functions. READ Friend function in C++ with Programming example In C++, a constructor with only one required parameter is considered as an implicit conversion function. It converts the parameter type to the class type. Prefixing explicit keyword prevents the constructor from using the implicit conversion. Syntax : explicit class_name (parameters) { Constructor Definition } Following program demonstrates the explicit constructor:

#include<iostream>
 using namespace std;
 class test
 {
     private:   
        int val;
     public: explicit test(int x)
    {
     val = x;
  }
  void display()
  {
        cout <<"The value of val is "<<val;
   }
 };
 int main()
 {
     test t1=10;
     t1.display();
 }

Output:
The value of val is 10.
 Following are the features or basic concepts of C++ programming language. 1. Objects and Classes 2. Data abstraction 3. Data encapsulation 4. Inheritance 5. Polymorphism 6. Binding 7. Message passing
1. Objects and Classes:
Classes are user-defined data types on which objects are created. Objects with similar properties and methods are grouped together to form a class. So class is a collection of objects. The object is an instance of a class. 2. Data abstraction
Abstraction refers to the act of representing essential features without including the background details or explanation. READ How to use Constructors and their Types in C++ Example: Let’s take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen. Example Data abstraction:

#include <iostream>

Using namespace std;

int main( )

{

cout << “Hello C++” <<endl;

return 0;

}

Here, you don’t need to understand how cout displays the text on the user’s screen. You need to only know the public interface and the underlying implementation of cout is free to change.
3. Data encapsulation
Information hiding, Wrapping (combining) of data and functions into a single unit (class) is known as data encapsulation. READ C++ Video Tutorial Data is not accessible to the outside world, only those functions which are wrapped in the class can access it. 4. Inheritance
Inheritance is the process of deriving a new class from an existing class. Existing class is known as base, parent or super class. The new class that is formed is called derived class, child or sub class. Derived class has all the features of the base class plus it has some extra features also. Writing reusable code. Objects can inherit characteristics from other objects. 5. Polymorphism
The dictionary meaning of polymorphism is “having multiple forms” or Ability to take more than one form. A single name can have multiple meanings depending on its context. It includes function overloading, operator overloading. 6. Binding
Binding means connecting the function call to the function code to be executed in response to the call. READ C++ program to get and display employee details Static binding means that the code associated with the function call is linked at compile time. Also known as early binding or compile time polymorphism. Dynamic binding means that the code associated with the function call is linked at runtime. Also known as late binding or runtime polymorphism. 7. Message passing
Objects communicate with one another by sending and receiving information.
 In normal function, First control will move from calling to called function. Then arguments will be pushed on to the stack, then control will move back to the calling from called function. This process takes extra time in executing. To avoid this, we use inline function. An inline function is expanded whenever it is called. That is, when we call the inline function the compiler replaces the call with the corresponding function definition, thus saving time of context switching between the calling and called function.
The following program demonstrates the C++ program to find the maximum of two numbers using the inline function.

 #include<iostream>
 using namespace std;
 inline int max(int x, int y)
 {
     if (x>y)
         return x;
     else 
         return y;
 }
 int main()
 {
     int a, b;
     cout<<"Enter the first number: ";
     cin>>a;
     cout<<"Enter the second number: ";
     cin>>b;
     cout<<"The maximum number is "<<max(a,b);
 }

OUTPUT:
READ Wipro ELITE National Talent Hunt Hiring 2021 Enter the first number: 30 Enter the second number: 25 The maximum number is 30
 A static member is shared by all objects of the class. Static data members hold global data that is common to all objects of the class. Examples of such global data are count of objects currently present, common data accessed by all objects, etc.
A static data member has certain special characteristics:
It is initialized to zero when first object is created. No other initialization is permitted. Only one copy of the data member is created for the entire class and is shared by all the objects of class, no matter how many objects are created. Static variables are normally used to maintain values common to entire class objects.
The following program demonstrates the C++ program to count the number of objects created.

 #include<iostream>
 using namespace std;
 class counter
 {
     private:
         static int count;
     public:
         counter()
         {
             count++;
         }
     void display()
     {
         cout<<"The number of objects created are: "<<count;
     }
 };
 int counter::count=0;
 int main()
 {
     counter c1;
     counter c2;
     counter c3;
     c2.display();
 }

OUTPUT:
The number of objects created are: 2
 The friend function is a function that is not a member function of the class but it can access the private and protected members of the class.
The friend function is given by a keyword friend.
These are special functions which are declared anywhere in the class but have given special permission to access the private members of the class.

Following program demonstrates the C++ program to find the sum of two numbers using bridge function add().

#include<iostream>
 using namespace std;

 class addtwonumbers
 {
     int x, y;
     public:
         addtwonumbers()
         {
             x = 0;
             y = 0;
         }
         addtwonumbers(int a, int b)
         {
             x = a;
             y = b;
         }
         friend int add(addtwonumbers &obj);
 };

 int add(addtwonumbers &obj)
 {
     return (obj.x+ obj.y);
 }

 int main()
 {
     addtwonumbers a1;
     cout<<"Sum is: "<<add(a1)<<endl;
     addtwonumbers a2(10, 20); 
     cout<<"Sum is: "<<add(a2)<<endl;
 }

OUTPUT:
Sum is: 0 Sum is: 30
 Function overloading means, two or more functions have the same names but different argument lists.
The arguments may differ in the type of arguments or number of arguments, or both.
However, the return types of overloaded methods can be the same or different is called function overloading.
Function overloading conceptshelps us to use the same function names multiple time in the same program.
Following program demonstrates the overloaded function to find the area of the circle, rectangle, and square.

#include<iostream>
 using namespace std;

 int area(int x)
 {
     return x*x; 
} 

int area(int l, int b) 
{
     return l*b;
 }

 double area(double r)
 {
     return 3.142*r*r;
 }

 int main()
 {
     int x, l, b;
     double r;
     cout<<"Enter the length of a square: ";
     cin>>x;
     cout<<"Enter the length of rectangle: ";
     cin>>l;
     cout<<"Enter the width of rectangle: ";
     cin>>b;
     cout<<"Enter the radius of circle: ";
     cin>>r;
     cout<<endl<<"The area of square is "<<area(x)<<endl;
     cout<<endl<<"The area of rectangle is "<<area(l, b)<<endl;
     cout<<endl<<"The area of circle is "<<area(r)<<endl;
 }
OUTPUT:
Enter the length of a square: 10 Enter the length of rectangle: 10 Enter the width of rectangle: 15 Enter the radius of circle: 5.5 The area of square is 100 The area of rectangle is 150 The area of circle is 95.0455
 
POP OOP
 Emphasis is given more to procedures (functions)   Emphasis is given more to data in OOP
 The programming task is divided into a collection of data structures and methods or functions.  The programming task is divided into objects (consisting of data members and associated member functions)  
 Procedures are being separated from data being manipulated  Procedures are not separated from data, instead, procedures and data are combined together and put into a class
 A piece of code uses the data to perform the specific task    The data uses the piece of code to perform the specific task
 Data is moved freely from one function to another function using function parameters.  Data is hidden (using private and protected access specifiers) and can be accessed only by member functions, not by an external function.  
 Data is not secure in POP  Data is secure OOP due to the use of access specifiers
 POP follows the top-down approach to solve the problem  OOP follows the bottom-up approach to solve the problem  
 As the size of the program increases the debugging becomes difficult  Irrespective of the size of the code the debugging is simple and easier in OOP.
 

 Applications of Compiler Technology

1. Implementation of high-level programming languages

Ø High-level programming language defines a programming abstraction
Ø Low-level language have more control over computation and produce efficient code
Ø Hard to write, less portable, prone to errors and harder to maintain
Ø Example : register  keyword
Ø Common programming languages (C, Fortran, Cobol) support
Ø User-defined aggregate data types (arrays, structures, control flow )
Ø Data-flow optimizations
Ø Analyze flow of data through the program and remove redundancies
Ø Key ideas behind object oriented languages are Data Abstraction
      and Inheritance of properties
Ø Java has features that make programming easier
Ø Type-safe – an object cannot be used as an object of an unrelated type
Ø Array accesses are checked to ensure that they lie within the bounds
Ø Built in garbage-collection facility
Ø Optimizations developed to overcome the overhead by eliminating unnecessary range checks
2. Optimizations for Computer Architectures

Ø Parallelism
Ø Instruction level : multiple operations are executed simultaneously
Ø Processor level : different threads of the same application run on different processors
Ø Memory hierarchies
Ø Consists of several levels of storage with different speeds and sizes
Ø Average memory access time is reduces
Ø Using registers effectively is the most important problem in optimizing a program
Ø Caches and physical memories are managed by the hardware
Ø Improve effectiveness by changing the layout of data or order of instructions accessing the data
3. Design of new Computer Architectures

Ø RISC (Reduced Instruction-Set Computer)
Ø CISC (Complex Instruction-Set Computer)
Ø Make assembly programming easier
Ø Include complex memory addressing modes
Ø Optimizations reduce these instructions to a small number of simpler operations
Ø PowerPC, SPARC, MIPS, Alpha and PA-RISC
Ø Specialized Architectures
Ø Data flow machines, vector machines, VLIW, SIMD, systolic arrays
Ø Made way into the designs of embedded machines
Ø Entire systems can fit on a single chip
Ø Compiler technology helps to evaluate architectural designs
4. Program Translations
Ø Binary Translation
Ø Translate binary code of one machine to that of another
Ø Allow machine to run programs compiled for another instruction set
Ø Used to increase the availability of software for their machines
Ø Can provide backward compatibility
Ø Hardware synthesis
Ø Hardware designs are described in high-level hardware description languages like Verilog and VHDL
Ø Described at register transfer level (RTL)
Ø Variables represent registers
Ø Expressions represent combinational logic
Ø Tools translate RTL descriptions into gates, which are then mapped to transistors and eventually to physical layout
Ø Database Query Interpreters
Ø Languages are useful in other applications
Ø Query languages like SQL are used to search databases
Ø Queries consist of predicates containing relational and boolean operators
Ø Can be interpreted or compiled into commands to search a database
Ø Compiled Simulation
Ø Simulation Technique used in scientific and engg disciplines,Understand a phenomenon or validate a design
Ø Inputs include description of the design and specific input parameters for that run
5. Software Productivity Tools
Ø Testing is a primary technique for locating errors in a program
Ø Use data flow analysis to locate errors statically
Ø Problem of finding all program errors is undecidable
Ø Ways in which program analysis has improved software productivity
Ø Type Checking
Ø Catch inconsistencies in the program
Ø Operation applied to wrong type of object
Ø Parameters to a procedure do not match the signature
Ø Go beyond finding type errors by analyzing flow of data
Ø If pointer is assigned null and then dereferenced, the program is clearly in error
Ø Bounds Checking
Ø Security breaches are caused by buffer overflows in programs written in C
Ø Data-flow analysis can be used to locate buffer overflows
Ø Failing to identify a buffer overflow may compromise the security of the system
Ø Memory-management tools
Ø Automatic memory management removes all memory-management errors like memory leaks
Tools developed to help programmers find memory management errors

 

 1) Lexical Analyzer

— The first phase of compiler is lexical analyzer it reads stream of characters in the source program

— Groups the characters into meaningful sequences – lexemes

— For each lexeme, a token is produced as output

—  <token-name , attribute-value>

Token-name : symbol used during syntax analysis

Attribute-value : an entry in the symbol table for this token

— Information from symbol table is needed for syntax analysis and code generation

— Consider the following assignment statement 

2) Syntax Analysis

The second phase of compiler is  syntax analysis  is also  called Parsing

— Parser uses the tokens to create a tree-like intermediate representation

— Depicts the grammatical structure of the token stream

— Syntax tree is one such representation

                                 Interior node – operation

                                 Children  - arguments of the operation

Other phases use this syntax tree to help analyze source program and generate target program


3) Semantic Analysis

The third phase of compiler is Semantic Analyzer

— Checks semantic consistency with language using:

                               Syntax tree  and Information in symbol table

— Gathers type information and save in syntax tree or symbol table

— Type Checks each operator for matching operands

                               Ex: Report error if floating point number is used as index of an array

— Coercions or type conversions

                         Binary arithmetic operator applied to a pair of integers or floating point numbers

                           If applied to floating point and integer, compiler may convert integer to floating-

                            point number


4) Intermediate Code Generation

     After syntax and semantic analysis  Intermediate Code Generation is the fourth  phase of compiler

— Compilers generate machine-like intermediate representation

— This intermediate representation should have the two properties:

                         Should be easy to produce

                         Should be easy to translate into target machine

             Three-address code

— Sequence of assembly-like instructions with three operands per instruction

— Each operand acts like a register

                        Points to be noted about three-address instructions are:

— Each assignment instruction has at most one operator on the right side

— Compiler must generate a temporary name to hold the value computed by a three-address instruction

— Some instructions have fewer than three operands

 

5) Code Optimization

            Attempt to improve the target code

— Faster code, shorter code or target code that consumes less power

                        Optimizer can deduce that

— Conversion of 60 from int to float can be done once  at compile time

— So, the inttofloat can be eliminated by replacing 60 with 60.0

 t3 is used only once to transmit its value to id1


6) Code Generation

— Takes intermediate representation as input

— Maps it into target language

— If target language is machine code

                        Registers or memory locations are selected for each of the variables used

                        Intermediate instructions are translated into sequences of machine instructions

                        performing the same task

— Assignment of registers to hold variables is a crucial aspect

 The scheme that postpones the linking functions until execution. A subroutine is loaded and linked to the rest of the program when it is first called – usually called dynamic linking, dynamic loading or load on call. The advantages of dynamic linking are, it allow several executing programs to share one copy of a subroutine or library. In an object oriented system,
dynamic linking makes it possible for one object to be shared by several programs. Dynamic linking provides the ability to load the routines only when (and if) they are needed. The actual loading and linking can be accomplished using operating system service request.

Jump to Page : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

Recommended Question Bank

General - Computer Science
View
- Computer organisation and architecture
View
NA - Java
View
- javascript
View
- STORAGE AREA NETWORKS
View
Mejona - Mejona Technology
View
VTU - NCES(Non - Conventional Energy Resources
View
- Java Basic Codes
View
VTU - STORAGE AREA NETWORK
View
- HIGHWAY ENGINEERING
View
- COMPUTER ORGANIZATION
View
- Quantity Surveying and Contracts Management
View
- Design of RC Structural Elements
View
- Ground Water and Hydraulic
View
- Urban Transport Planning
View
- Basic Geotechnical Engineering
View
VTU - Municipal Waste Water Engineering
View
VTU - Design of Steel Structures Elements
View
- Interview Question Bank
View
VTU - Artificial Intelligence
View
Visvesvaraya Technological University (VTU) - Ground water hydraulic
View
-
View
VTU - Artificial intelligence and Machine Learning (AIML)
View
VTU - Energy and Environment
View
VTU - User Interface Design
View
- Data Structures and Algorithms
View
VTU - Big Data Analytics
View
VTU - Engineering Chemistry
View
VTU - User Interface Design (UID)
View
Entrance Exam for job - Entrance Exam Questions
View
VTU - Elements of Civil Engineering and Mechanic
View
VTU - Computer Graphics and Visualization
View
VTU - Object Oriented Concepts
View
VTU - System Software and Compiler Design
View
VTU - Web Technology and its Applications
View
VTU - Cloud Computing and Its Applications
View