Tuesday, April 8, 2014

Destructor Functions in Detail

If you don’t know what destructor functions are, read Introduction to Constructor and Destructor functions of a Class. As the example
programs in this article makes use of the dynamic memory allocation of C++,
so please read Introduction to Dynamic Memory Allocation in C++ , in case you missed it.


When does the destructor function gets invoked


The examples below illustrates when the destructor function gets invoked:


Example 1:


  //Example Program in C++
#include<iostream.h>

class myclass
{
public:

~myclass()
{
cout<<"destructed
";
}
};

void main(void)
{
myclass obj;
cout<<"inside main
";
}

OUTPUT:


   inside main
destructed
Press any key to continue

As I said in the other article, destructors get invoked when the object of
a class goes out of scope. In this case, the object goes out of scope as the
program terminates. So the destructor gets invoked just before the program’s
termination.


Example 2:


  //Example Program in C++
#include<iostream.h>

void myfunc(void);

  class myclass
{
public:

~myclass()
{
cout<<"destructed
";
}
};

void main(void)
{
cout<<"inside main
";
myfunc();
cout<<"again inside main
";
}

void myfunc(void)
{
cout<<"inside myfunc
";
myclass obj;
cout<<"still inside myfunc
";
}

OUTPUT:


   inside main
inside myfunc
still inside myfunc
destructed
again inside main
Press any key to continue

In this case, destructor function is invoked just as the program’s execution
returns from the function, but before executing any further instruction from
where it was called (main).


Example 3: In the following example we are creating a dynamically
allocated object of a class in the same way as we did with the variables.


  //Example Program in C++
#include<iostream.h>

class myclass
{
public:

   ~myclass()
{
cout<<"destructed
";
}
};

void main(void)
{
myclass *obj;
obj=new myclass;
cout<<"inside main
";

delete obj;

cout<<"still inside main
";
}

OUTPUT:


   inside main
destructed
still inside main
Press any key to continue

Here the programmer is explicitly destroying the object, hence the destructor
function is called.


Now that you know some details about the destructor function, let me give you
a practical example of the use of destructor function:


  //Example Program in C++
#include<iostream.h>

class myclass
{
int *number;

public:
myclass(int num)
{
number=new int[num];
}

~myclass()
{
delete []number;
}

void input_num(int index, int num)
{
number[index]=num;
}

int output_num(int index)
{
return number[index];
}
};

void main(void)
{
int size, num;
cout<<"enter number of elments: ";
cin>>size;

myclass obj(size);

for(int i=0;i<size;i++)
{
cout<<"enter element "<<i+1<<":";
cin>>num;
obj.input_num(i,num);
}

cout<<"
elements have the following values

";

for(i=0;i<size;i++)
{
cout<<"element "<<i+1<<":";
cout<<obj.output_num(i);
cout<<"
";
}
}

Good-Bye!


Related Articles:


Read More..

Note for use programs of this blog

Some programs of this blog are not contains main functions because many places we require to call only required function prototype.
So, you required to edit program and put main function for successfully execution. 
Read More..

Monday, April 7, 2014

Inkey and Liberty BASIC

There has been some discussion in the support forum about extending keyboard handling in Liberty BASIC programs. Currently we only allow doing something like this in the graphics views. Two things (maybe more?) are missing:
  • Getting the next character in the mainwindow without stopping the program. Currently only INPUT and INPUT$() are supported. Both stop the program and wait.
  • Getting the next character typed for some window or widget that has focus. In other words, reading keypresses for objects with handles (ie. #thisWindow)

What I suggested in the forum is a function called inkey$().

The following would read keypresses for the mainwindow:

print inkey$()

or it could be abbreviated to:

print inkey$

To read keypresses for windows or widgets with handles:

print inkey$(#handle)

Id like to solicit feedback here. Any ideas? Please leave a comment, and thanks!

Read More..

GMAIL


  • Note: Must have "keyboard shortcuts" on in settings.
  • C: Compose new message.
  • Shift + C: Open new window to compose new message.
  • Slash (/): Switch focus to search box.
  • K: Switch focus to the next most recent email. Enter or "O" opens focused email.
  • J: Switch focus to the next oldest email.
  • N: Switch focus to the next message in the "conversation." Enter or "O" expands/collapses messages.
  • P: Switch focus to the previous message.
  • U: Takes you back to the inbox and checks for new mail.
  • Y: Various actions depending on current view:
  • Has no effect in "Sent" and "All Mail" views.
  • Inbox: Archive email or message.
  • Starred: Unstar email or message.
  • Spam: Unmark as spam and move back to "Inbox."
  • Trash: Move back to "Inbox."
  • Any label: Remove the label.
  • X: "Check" an email. Various actions can be performed against all checked emails.
  • S: "Star" an email. Identical to the more familiar term, "flagging."
  • R: Reply to the email.
  • A: Reply to all recipients of the email.
  • F: Forward an email.
  • Shift + R: Reply to the email in a new window.
  • Shift + A: Reply to all recipients of the email in a new window.
  • Shift + F: Forward an email in a new window.
  • Shift + 1 (!): Mark an email as spam and remove it from the inbox.
  • G then I: Switch to "Inbox" view.
  • G then S: Switch to "Starred" view.
  • G then A: Switch to "All Mail" view.
  • G then C: Switch to "Contacts" view.
  • G then S: Switch to "Drafts" view.

Read More..

Sunday, April 6, 2014

Security

Scriptman responded to my last post with the following questions:

O.K. point taken, but how do you overcome security issues such as passing "nasty" commands to the interpreter? (yes even Perl has them if scripts are not properly configured.)

This is a very good question. Our web BASIC uses a custom web server with the language engine built right into it. We are trying to be very security minded. We will sandbox each session to prevent improper access to files and to avoid server swamping, processes that fail to terminate, etc.

Another question I have is, will your Basic have a feature to "get" (sorry no pun intended) parameters such as the IP of the client ?Much like the Perl $ENV{REMOTE_ADDR}

We dont have this yet, but I see no reason why it cannot be made available.
Read More..

Creating file in Turbo C

Step 1:
Go to drive where Turbo C is installed then go to bin folder
Click on turbo c icon then follow steps
Create a file:
Open menu by using cursor or type shortcut key ALT+F4 then select new to open new file.
Step 2:
Save a file:
To save a file select save from menu or press F2 shortcut key.
Then a box will appear like that
Then before extension .cpp an asterisk will appear or you will see a word "noname" remove asterisk or noname word and replace it your file desired name e.g. first.cpp and then press OK your file will save in respective directory.
RECOMMENDED way naming to your file:
Name your file or program to less than or equal than six characters otherwise compiler can face problem to execute a program which have greater name than six characters.  
Step 3:
Compile a program:
To compile a program go to menu bar and select compile option or press short cut key ALT+F9
Then compiler will show how many errors, warnings and lines executed in your program and also identify line number in which error is found.
Step 4:
Running a program by using Turbo C++ menu bar.
To run program go to menu bar select the run option or press shortcut key ALT+F5.
Read More..

Saturday, April 5, 2014

Factorial C program Algorithm Flowchart

Q. Write a C program to find the factorial value of a number. Also write the algorithm and draw flowchart.

Ans.

/*c program to find out factorial value of a number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i,fact=1;
 printf("Enter any number : ");
 scanf("%d", &n);
 for(i=1; i<=n; i++)
    fact = fact * i;
 printf("Factorial value of %d = %d",n,fact);
 return 0;
}

The output of above program would be:
Output of calculate factorial value  of a number C program
Screen shot for calculate factorial value
of a number C program



Algorithm for calculate factorial value of a number:

[algorithm to calculate the factorial of a number]
step 1. Start
step 2. Read the number n
step 3. [Initialize]
        i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
[process finish of calculate the factorial value of a number]

Flowchart for calculate factorial value of a number:

flowchart for calculate factorial value of a number
Figure: Flowchart for calculate factorial value of
a number C program

Read More..
 
Blog Information - Powered By Blogger