#include
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 <
#include
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 <
#include
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 <
#include
using namespace std;
class test
{
private:
int val;
public: explicit test(int x)
{
val = x;
}
void display()
{
cout <<"The value of val is "<
#include
Using namespace std;
int main( )
{
cout << “Hello C++” <
#include
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 "<
#include
using namespace std;
class counter
{
private:
static int count;
public:
counter()
{
count++;
}
void display()
{
cout<<"The number of objects created are: "<
#include
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: "<
#include
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<
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. |
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
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