C++ program swap two numbers using single inheritance

Swap two numbers using single inheritance

C++ program swap two numbers using single inheritance.Derived class with only one base class in called single inheritance.

swap two numbers using single inheritance

Logic used to swap two numbers.

    first number = first number  +  second number;
    second number = first number  -  second number;
    first number = first number  -  second number;

Program

#include<iostream.h>
#include<conio.h>
class scan
{
 protected:
 int x,y;
 public:
 void get()
 {
  cout<<"Enter value of x:- ";
  cin>>x;
  cout<<"Enter value of y:- ";
  cin>>y;
 }
};
class swap:public scan
{
 public:
 void swap_val()
 {
    x=x+y;
    y=x-y;
    x=x-y;
 }
 void display()
 {
  cout<<"Value of x:- "<<x<<endl;
  cout<<"Value of y:- "<<y;
 }
};
void main()
{
 clrscr();
 swap s1;
 s1.get();
 s1.swap_val();
 cout<<"................................"<<endl;
 s1.display();
 getch();
}

OUTPUT

swap two numbers using single inheritance


Comment below if you are facing any problem.


Also read

C++ Program to add two numbers by defining function Outside the class
C++ Simple Program of Static DataMember


Summary
C++ program swap two numbers using single inheritance
Article Name
C++ program swap two numbers using single inheritance
Description
This program illustrate the concept of swap two numbers using single inheritance.Derived class with only one base class in called single inheritance.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *