Professional Documents
Culture Documents
cout<<“dist1=“; dist1.showdist();
cout<<“dist2=“; dist2.showdist();
cout<<“dist3=“; dist3.showdist();
cout<<“dist4=“; dist4.showdist();
}
Calling using this
object
• On other hand, it can also make your program more difficult and hard
to understand.
2.
• a. Write a program to calculate salary of a employee (before
taxes given the hours worked and the hourly rate. The program output
should look similar to: (USING *= OPERATOR OVERLOADING)
• b. Extend the program for calculating Monthly Salary of more
than one employee.
• c. Firm employs students who are exempted from taxes and
others who are not. Modify the program so that if an employee is not
exempt, 10% will be deducted from Salary, otherwise "NO TAXES
DEDUCTED" will be displayed.
Two program runs are shown below:
void main( )
{
int myarray[ ] = {5,10,15};
void print(myarray, 3);
}
void print (int a[ ], int length)
{
for (int n=0; n<length; n++)
cout<<a[n]<<endl;
}
• The size of the array is fixed and cannot be changed at run time.
• In C++, we use normally static arrays. But the disadvantage with
the static arrays is that once we have declared the array of a
particular size, we cannot change its size when program is
running. So the solution for this problem is to use the dynamic
array allocation.
DYNAMIC MEMORY ALLOCATION
• In last program we had fixed size of memory size for
array element. But what will happen if we need
variable amount of memory, which can only be
determined during the execution time.
• Then we require DYNAMIC MEMORY.
• In case of dynamic array, the size of an array does not
know by the compiler in advanced. This dynamic
memory is allocated by the system at the runtime. To
allocate memory dynamically, we have to use pointers.
• The real power of pointers is seen when they are used
to point dynamically allocated variables.
• But the dynamic allocation does not mean that we can
create new variables names during execution time.
• It is necessary that compiler must know all the variables
identifiers at the compile time. For this reason, creating
dynamic memory involves two steps:
void print ( )
{ cout<<"The area of square is : "<<result<<endl; }
void main( )
{
square sqr1( 2,3 );
area(sqr1) ;
sqr1.print();
}