Thursday, September 23, 2010

Constructors:

Constructors are similar to functions which has function identifier similar to class identifier. It gets executed when a object came into existence,i.e., when a object is created. There are variety of constructors. They are:

1.Default constructors
2.Parametrized constructors
3.Copy constructors
4.Overloading constructors

Default constructors:
        Default constructor are constructors which has no parameters. Default constructor assign default value as 0 to its public, private and protected data members.

Syntax:
class class_name
{
//class definitions

class_name()
{}
}

Example:
class book
{
int a,b;
public:
book()
{
}
};
int main()
{
book b;
}

Wednesday, September 22, 2010

Inheritance:

Base class functions and members can be redefined in derived class check it out:

Program:
#include<iostream>
class A
{
protected:
int a,b;
public:
void assign()
{
a=30,b=20;
}
void display()
{
cout<<"\na= "<<" b="<<b;
}
};
class B : public A
{
protected:
int c,d;
public:
void assign()
{
a=40,b=50,c=100,d=200;
}
void display1()
{
cout<<"\na="<<a<<" b="<<b<<" c="<<c<<" d="<<d;
}
};
void main()
{
B deriv;
deriv.assign();
deriv.display1();
A base;
base.assign();
base.display();
}

output:a=40 b=50 c=100 d=200
a=30 b=20

Tuesday, September 21, 2010

Operator Precedence in C.

Operator precedence plays a very important role while evaluating a expression and logical output predict in a program. Here i have listed the precendence of operator. So when a expression or logical condition is evaluated, the evaluation takes place in the order of precedence.



Operator Symbol
Name of the operator
!
Logical Not
*,/,%
Arithmetic and modulo
<,>,<=,>=
Relational
==,!=
Relational
&&,||
Logical AND and OR
=
Assignment

Simple java program:

Let's start our java with simple Hello world program.

class sample {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}
Output:
Hello World!

Monday, September 20, 2010

after every successfull execution of 'C' program what will happen?

After every successfull execution of a "C"-Program, main() returuns 0 to operating system, to tell that the program is executed successfully....

why we use getch() in main(), to get output?

 getch() is a function call : it reads in a single character and waits for the user to hit enter before reading the character. This line is included before the end of the main() because many compiler environments will open a new console window, run the program, and then close the window before you can see the output. This command keeps that window from closing because the program is not done yet because it waits for you to hit enter. Including that line gives us time to see the program run.

Wednesday, September 15, 2010

Write program which executes a function(main or any function) to infinite times without any iteration and conditional statements.

There only two ways to execute program to  infinite number of times:

1. Using iteration or conditional statement.
2.Recursive functuion-function calls itself.

So,if iteration or conditional statement are not used, then recursive is another go...using recursive function we can write main() function as follows.

main()
{
printf("Hello world\n");
main();
}

Output:

Hello world is printed infinte number of times, till u break the program. 

Write printf() and scanf() statement without semi-colon(;)

Print statement:

main()
{
int a;
clrscr();
if(scanf("%d",&a))
{}
if(printf("%d"),a)

{}
}


output:
2
2

Here in output,i entered 2 is given as input that is scanned and stored in variable 'a',then it is printed out 2 using printf(). Note that after if statement i used set of braces({}) to show that next statement to be executed after if. If the braces are omitted then error -"; statement missing" is produced.

The above program can be re-written as follows:

main()
{
int a;
clrscr();
if(scanf("%d",&a))
 ;
if(printf("%d"),a)

 ;
}

Monday, September 6, 2010

Execute both if and else statements

There are two ways to execute both if and else statements.

1.

if(1)
printf("if loop");
else;
printf(" else loop");

output:

if loop else loop

In the above statement note that there is a semi-colon (;) in the else statement. This statement is called as NULL statement in 'C'.

The above program becomes:


if(1)
printf("if loop");
else
 ;
printf(" else loop");


2.

if(printf("if")==0)
printf(" if");
else
printf(" else");

output:

 if else

In the statement-2, the printf which is inside the if condition is executed. Generally if executes the statement which is given as condition, so executes the print statement and then checks it is equal to 0, here condition becomes false. When condition comes false else is executed.

In this above step-2 the print statement which is given inside the if loop is executed. So the question is:

if(condition)
printf("Hello");
else
printf(" world");

write the condition such that your output will be:

Hello world