C++ Examples
- Write a C++ Program to print a welcome text in a separate line.
- Write a Program in C++ to add two numbers.
- C++ program for addition of two numbers using functions.
- Add two numbers in C++ using class.
- Addition of two numbers using call by reference in C++
- C++ program to write a program to add two numbers using call by value.
- Swapping of two numbers in C++ without using third variable.
- Swap two numbers in C++ using function.
- C++ program to swap two numbers using class.
- C++ program to swap two numbers using call by value.
- C++ program to swap two numbers using pointers.
- Write a program to swap private data members of two classes using friend function.
- write a program to add data objects of two different classes using friend function.
- Threading in C++.
- C++ thread lambda
- C++ thread lambda with parameter
- C++ thread lambda pass by reference
- Singleton C++ example
C++ program to swap two numbers using class.
#include<iostream>
using namespace std;
class swapTwoNumbers
{
public:
int num1,num2;
void getData();
void SwapTwoValues();
void display();
};
void swapTwoNumbers::getData()
{
cout<<"Enter the first number ";
cin>>num1;
cout<<"Enter the second number ";
cin>>num2;
}
void swapTwoNumbers::SwapTwoValues()
{
swap(num1,num2);
}
void swapTwoNumbers::display()
{
cout<<"The numbers are "<<num1<<" "<<num2<<endl;
}
int main()
{
swapTwoNumbers obj;
obj.getData();
cout<<"Before swapping "<<endl;
obj.display();
obj.SwapTwoValues();
cout<<"After swapping "<<endl;
obj.display();
return 0;
}
Output
Enter the first number 45
Enter the second number 86
Before swapping
The numbers are 45 86
After swapping
The numbers are 86 45