Are you prepared for the day when your dream starts to become reality?
Are you prepared for the day when your dream starts to become reality?
Published 22/02/2011 MyThoughts Leave a CommentTags: Are you prepared for the day when your dream starts to become reality?
Binding refers to the process that is used to convert identifiers (such as variable and function names) into machine language addresses. Although binding is used for both variables and functions, in this lesson we’re going to focus on function binding.
#include <iostream>
void PrintValue(int nValue)
{
std::cout << nValue;
}
int main()
{
PrintValue(5); // This is a direct function call
return 0;
}
This is early binding.Early binding (also called static binding)
means the compiler is able to directly associate the identifier
name (such as a function or variable name) with a machine address.
Remember that all functions have a unique machine address.
So when the compiler encounters a function call, it replaces
the function call with a machine language instruction that tells
the CPU to jump to the address of the function.
int Add(int nX, int nY)
{
return nX + nY;
}
int main()
{
// Create a function pointer and make it point to the Add function
int (*pFcn)(int, int) = Add;
cout << pFcn(5, 3) << endl; // add 5 + 3
return 0;
}
This is late binding.
it is not possible to know which function will be called
until runtime (when the program is run). This is known as
late binding (or dynamic binding). In C++, one way to get
late binding is to use function pointers. To review function
pointers briefly, a function pointer is a type of pointer that
points to a function instead of a variable. The function that
a function pointer points to can be called by using the function
call operator (()) on the pointer.
|
Calling a function via a function pointer is also known as an indirect function call.
|
Function Abstraction & Data Abstraction:
In functional abstraction, access to the function is provided through a specific interface defined to invoke the function. In contrast, in data abstraction, access to the data is provided through a specific set of operations defined to examine and manipulate the data. For instance, when a programmer is using C++ standard data types, this means that users are using the concept of data abstraction. When using data types, the users are not concerned with how the data is stored but they are concerned with what operations are provided and what properties are supported.
class AB {
public:
virtual void f() = 0;
};
class D2 : public AB {
void g();
};
int main() {
D2 d;
}
The compiler will not allow the declaration of object d because D2 is an abstract class; it inherited the pure virtual function f()from AB. The compiler will allow the declaration of object d if you define function D2::g().
Note that you can derive an abstract class from a non abstract class, and you can override a non-pure virtual function with a pure virtual function.
A virtual function must be one of the following:
Defined
Declared pure
Defined and declared pure
A base class containing one or more pure virtual member functions is called an abstract class.
C++ Virtual function – Call Mechanism:
Whenever a program has a C++ virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address. This is how the Dynamic binding happens during a virtual function call.C++ virtual function is,
A member function of a class
Declared with virtual keyword
Usually has a different functionality in the derived class
A function call is resolved at run-time
The difference between a non-virtual c++ member function and a virtual member function is, the non-virtual member functions are resolved at compile time. This mechanism is called static binding. Where as the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic binding.
C++ Virtual function – Example:
This article assumes a base class named Window with a virtual member function named Create. The derived class name will be CommandButton, with our over ridden function Create.
class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual function example
{
cout <<"Base class Window"<
class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button – Overridden C++ virtual function"Create();
y = new CommandButton();
y->Create();
}
The output of the above program will be,
Base class Window
Derived class Command Button
then the base class function would have been called all the times. Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.
The main difference between an abstract base class and a regular polymorphic class is that because in abstract base classes at least one of its members lacks implementation we cannot create instances (objects) of it.
But a class that cannot instantiate objects is not totally useless. We can create pointers to it and take advantage of all its polymorphic abilities. Therefore a declaration like:
CPolygon poly;
would not be valid for the abstract base class we have just declared, because tries to instantiate an object. Nevertheless, the following pointers:
1
2
CPolygon * ppoly1;
CPolygon * ppoly2;
would be perfectly valid.
Here I would like to share the concept of Program & console interaction in c++.
We have used cin & cout in cpp program, we can use scanf & printf too in cpp but to achieve oos concept, we have been using cin & cout.
So, What is cin & cout? They are stream objects.
Now what is stream?
A stream is a flow which take u from one place to another.
Like you have to enter some text thru keyboard(one place) it should be stored somewhere(other place) should be shown in screen(other place).
Like 8 decades back, we had a ship in London which used to sail to NewYork thru stream of water(sea, river).
Coming back to cin & cout.
cin is an instance of Istream_with_assign class which inherits istream & ios class.
cout is an instance of ostream_with_assign class which inherits ostream & ios class.
Char: The most basic unit addressable by the machine, typically a single octet. or 1 byte.
The CPU:
General Purpose Registers:
CPU provides 32 GPRegisters each 32 bits long.
ro,r1,……,r31
they are used in procdeures calling, stack frame, returning values from procedure.
Control Registers:
they hold values that control the execution of the processor. All are 32 bits long.
ia- the instruction address register contains the address of next instruction
psw- program status word (2bit)bit 0 is 1 user mode, else kernel mode
bit 1 is 1 if interrupt is enabled and 0 is interrupt is masked.
iia- interrupt instruction address register stores the value of ia before an interrupt.
When an interrupt occurs, the value of ia is saved in iia, and the ia is loaded with the address of the interrupt handler.
base- memory base register is added to all the addresses when the system is in user mode.
bound- the memory bound register is the address limit.
In user mode all address must be less than the bound register, otherwise a program error interrupt will occur.
ipsw- interrupt program status word stores the value of psw register before an interrupt.
ip- interrupt parameter register contains data about the last interrupt.
iva-interrupt vector address register is the location in memory where the interrupt vector table is located.
Instruction Set:
load, store, loadAll, storeAll, move, syscall, rti(return from interrupt)
Questions:
Q1> how do we know interrupt is masked or active?
Q2> how do we know progam is running in user mode or system mode?
(hint check psw for both questions)
Q3>why do we have base and bound registers?
(hint: mapping logical address to physical address)
Q4) how does interrupt handle at lowlevel?
Answer: psw is saved in the ipsw register by the cpu. Reset psw it means
processor is in system mode with interrupt masked or disabled.
Stores the interrupt argument in the ip register.
save ia register to iia register, get the address of interrupt handler by checking iva register& load the address into the ia register.
I/O devices:
Memory Mapped IO: Communication between io devices and processor is done thru physical memory locations in the io address space.
Each device will have some locations in the memory.
Processor will respond when those locations are placed on the bus.
Getting Global variables in C++ => ::variable_name
Getting Member variable in C++ => Class_name::variable_name
Getting Local variables in C++ => variable_name
Calloc allocates Allocates an array in memory with elements initialized to 0.
Bond The power behind love, the force behind everything, I count it for you.
A writer wrote in his book,
I struggled to think what should i write here for you,and finally settled on
“YOU ARE SPECIAL TO ME”
gdb is in the gnu package, if you can run g++, then you will be able to run gdb.
gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With g++, this is accomplished using the -g command line argument. For even more information, the -ggdb switch can be used which includes debugging symbols which are specific to gdb. The makefile for this tutorial uses the -ggdb switch.
#include
using namespace std;
const int MAX = 6;
class Array
{
private:
int a[MAX];
public:
int insert();
int del(int pos);
int reverse();
int display();
//int search(int num);
};
int Array :: insert()
{
cout << "Enter the elements\n";
for(int i=0; i> a[i];
return 0;
}
int Array::display()
{
cout <<"Your Array is\n";
for(int i=0;i<MAX;i++)
cout << a[i]<<"\t";
cout<MAX)
{
cout<<"Invalid element to delete"<<endl;
return -1;
}
/* Deleting an array element requries shifting shift the elements to the left*/
for(; i<MAX; i++)
a[i-1]=a[i];
/* a[i-1] is the current element to be deleted */
a[i-1]=0;
cout<<"After deletion"<<endl;
display();
return 0;
}
int Array::reverse()
{
for(int i=0;i<MAX/2;i++)
{
int temp=a[i];
a[i]=a[MAX-1-i];
a[MAX-1-i]=temp;
}
cout<<"after Reverse"<<endl;
display();
}
int main()
{
Array a;
cout<<"The size of a is "<<sizeof(a)<<endl;
a.insert();
cout << "your array is as shown below:\n";
a.display();
a.del(7);
a.del(2);
a.reverse();
return 0;
}