Thursday, December 21, 2017

C++ Code Review: Swaps two numbers using class.

Description: This program swaps two numbers using class.

Code:

//Compiling Tool: Code::Block IDE with MinGW

//Program swaps two numbers using class

 

#include<iostream>

#include<conio.h> //for getch()

 

using namespace std;

 

class swapCls

{

    int a,b;

public:

    void getdata();

    void swapv();

    void display();

};

 

void swapCls::getdata()

{

    cout<<"Enter two numbers:";

    cin>>a>>b;

}

 

void swapCls::swapv()

{

    a=a+b;

    b=a-b;

    a=a-b;

}

 

void swapCls::display()

{

    cout<<"a="<<a<<"\tb="<<b;

}

 

main()

{

 

    swapCls s;

 

    s.getdata();

    cout<<"nBefore swap:\n";

    s.display();

 

    s.swapv();

    cout<<"\nAfter swap:\n";

    s.display();

 

    getch();

    return 0;

}              

 

Output:

clip_image002

If you want to write or construct or program C++ mini-project and do not know how or from where to start buy this simple e-book: Code Review of 26 C++ mini-projects. For book sample click this link.

Add your comments in below available comment box.

Note: Click these label/tags to view all related posts. Tags: C++Code

 

…till next post, bye-bye & take care.

No comments:

Post a Comment