Saturday, November 12, 2011

PL/SQL Example to find average of three numbers

This is a PL/SQL sample example to accept three numbers from the user and find average of accepted numbers.

Aim - PL/SQL Example to find average of three numbers



Sample Program : -


declare
a number(3);
b number(3);
c number(3);
avgr number(5,2);
begin
a:=&a;
b:=&b;
c:=&c;
avgr:=((a+b+c)/3);
dbms_output.put_line('Average of a, b, c is: '||avgr);
end;


Output of PL SQL Block: -

Enter value for a: 22
old 7: a:=&a;
new 7: a:=22;
Enter value for b: 32
old 8: b:=&b;
new 8: b:=32;
Enter value for c: 43
old 9: c:=&c;
new 9: c:=43;
Average of a, b, c is: 32.33

PL/SQL program to generate Fibonacci series

It is a sample example of generate fibonacci series upto 10 numbers.

Aim - PL/SQL program to generate Fibonacci series.



PLSQL Sample Program: -

declare
f1 number(3);
f2 number(3);
f3 number(3);
num number(3);
begin
f1:=0;
f2:=1;
f3:=0;
num:=1;
while num<=10
loop
dbms_output.put_line(f3);
f1 :=f2;
f2:=f3;
f3:=f1+f2;
num:=num+1;
end loop;
end;




Output of PL SQL Block: -
0
1
1
2
3
5
8
13
21
34

PL/SQL Program to reverse a string

This is a sample PLSQL example to accept a string from user and print reverse of accept string.

Aim - PL/SQL Program to reverse a string.



Sample PLSQL Program: -
declare
str1 varchar2(30);
len number(3);
str2 varchar2(30);
i number(3);
begin
str1:='&str1';
len:=length(str1);
for i in reverse 1..len
loop
str2:=str2 || substr(str1,i,1);
end loop;
dbms_output.put_line('Reverse string is: '||str2);
end;




Output of PL SQL Block: -
Enter value for str1: hello
old 6: str1:='&str1';
new 6: str1:='hello';
Reverse string is: olleh

Sunday, September 25, 2011

Multipath Inheritance in CPP

Hello friends, today I am going to discuss on one type of inheritance in c++. This is "Multipath Inheritance in CPP". Here also discuss about problem in multipath inheritance and how to solve this problem. Multipath Inheritance explains by using a sample c++ example.

What is Multipath Inheritance?

Multipath Inheritance is a hybrid inheritance. It is also called as Virtual Inheritance. It is combination of hierarchical inheritance and multiple inheritance.
In Multipath Inheritance there is a one base class GRANDPARENT. Two derived class PARENT1 and PARENT2 which are inherited from GRANDPARENT. Third Derived class CHILD which is inherited from both PARENT1 and PARENT2.

Problem in Multipath Inheritance

There is an ambiguity problem. When you run program with such type inheritance. It gives a compile time error [Ambiguity]. If you see the structure of Multipath Inheritance then you find that there is shape like Diamond.

Multipath Inheritance

Why Ambiguity Problem in Virtual Inheritance?

Suppose GRANDPARENT has a data member int i. PARENT1 has a data member int j. Another PARENT2 has a data member int k. CHILD class which is inherited from PARENT1 and PARENT2. It has a data member int l.
CHILD class have data member
int l(CHILD class data member)
int j( one copy of data member PARENT1)
int k ( one copy of data member PARENT2)
int i(two copy of data member GRANDPARENT)

This is ambiguity problem. In CHILD class have two copies of Base class. There are two duplicate copies of int i of base class. One copy through PARENT1 and another copy from PARENT2. This problem is also called as DIAMOND Problem.

Solve Multipath Inheritance Problem

To avoid duplicate copies of class Base we inherited intermediary classes with the prefix of the keyword "virtual". We use virtual keyword so it is also called as "Virtual Inheritance".
This results is avoiding duplicate copies of class Base members and in child class have only one copy of base class members.

C++ Program to demonstrate Multipath Inheritance

#include < iostream.h >
#include < conio.h >
class A
{
public:
int a;
};
class B: virtual public A
{
public:
int b;
};
class C: virtual public A
{
public:
int c;
};
class D:public B,public C
{
private:
int d;
public:
void sum()
{
d=a+b+c;
}
void display()
{
cout<<"Sum of a,b,c is:  " << d ;  
}  
}; 
void main() 
{  
D d1;  
clrscr();  
d1.a=10;  
d1.b=20;  
d1.c=30;  
d1.sum();  
d1.display();  
getch(); 
}  

Output


when not using virtual keyword as prefix for intermediary class
Compile time error
Member is ambiguous: A::a and A::a

Output When use keyword virtual as prefix intermediary class
Sum of a,b,c =60

Hope, you like this post. If anybody known further details about multipath inheritance then share to others by comments.