Tuesday, 3 June 2014

swap two variables using abstract class

         swapping of two variables using abstract class


Abstract Class => class is a class whose object is never created and is only used for inheritance.

** Abstract class always contain a pure virtual function which is declared in the abstract class but defined in       the derived class.

#include<conio.h>
#include<iostream>
using namespace std;
class abstract
{
public:
int a,b;
public:
abstract(){a=0;b=0;}
void get()
{cout<<"\nEnter the value of A:";
cin>>a;
cout<<"\nEnter the value of B:";
cin>>b;
   }
   void show()
   {
    cout<<"\nA="<<a;
    cout<<"\nB="<<b;
   }
virtual void swap()=0;   // pure virtual function
};
class derived : public abstract
{public:
void swap()
{
a=a+b;
b=a-b;
a=a-b;
}
};
int main()
{
derived D;

D.get();
D.swap();
cout<<"\n After swapping :";
D.show();
getch();
return 0;
}


ask your queries in the comment box.


Naveen dengani
Shashank Chauhan

No comments:

Post a Comment