Wednesday, April 29, 2015

Integer Complemet of a Number or Once Complemet of a Integer

The Program will give an out put for Once complemet for a input Integer number :

#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

int GetIntegerComplement(int nInputNum)
{
int nRemider = 0, nCompRem = 0, nSum = 0, nCompSum=0, nTempNum;
int nPos=1;
string strComp="";
char ChArray[2];
size_t Size = 1;
nTempNum = nInputNum;
do
{
nRemider = nTempNum % 2;

if (nRemider == 0)
{
nCompRem = 1;

}
else
{
nCompRem = 0;
}

nSum = nSum + (nPos*nRemider);
nCompSum = nCompSum + (nPos*nCompRem);
_itoa(nCompRem,ChArray,10);
strComp = strComp + string(ChArray);
nTempNum = nTempNum / 2;
nPos = nPos * 10;

} while (nTempNum>0);

cout << "\n The Binary equivalent of " << nInputNum << " is " << nSum;

int nIntComp = 0;

nPos = 0;
nRemider = 0;

while (nCompSum > 0)
{
nRemider = nCompSum % 10;

nIntComp = nIntComp + pow(2,nPos)*nRemider;

nCompSum = nCompSum / 10;

nPos++;

}

reverse(strComp.begin(),strComp.end());

cout << "\n The complement Binary equivalent" << " is " << strComp;


return nIntComp;
}

void main()
{
int nNumber;
cout << "\n Enter a number to Convert to Binary";
cin >> nNumber;
cout << "\n The Integer complement of " << nNumber << " is " << GetIntegerComplement(nNumber);

_getch();
}


OutPut :


Monday, April 27, 2015

Friend function simple example in C++

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.
To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows:

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class access
{
// String Variable declared as internal
public:
void SetName(string Name);
friend void print(access objAcess);
private:
string name;
};
void access::SetName(string Name)
{
name = Name;
}

void print(access objAcess)
{
cout << "\nMy name is " << objAcess.name;
}

void main( )
{
access objAcess;
objAcess.SetName("Sreeyush");
print(objAcess);
    _getch();

}

Static Members inside a C++ Class , there is no static class in C++

We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.
A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.

#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

class TemperatureConverter
{
public :
static double CelsiusToFahrenheit(std::string temperatureCelsius)
{
// Convert argument to double for calculations. 
std::string::size_type sz; 
double celsius = std::stod(temperatureCelsius, &sz);

// Convert Celsius to Fahrenheit. 
double fahrenheit = (celsius * 9 / 5) + 32;

return fahrenheit;
}

static double FahrenheitToCelsius(std::string temperatureFahrenheit)
{
// Convert argument to double for calculations. 
std::string::size_type sz;
double fahrenheit = std::stod(temperatureFahrenheit, &sz);

// Convert Fahrenheit to Celsius. 
double celsius = (fahrenheit - 32) * 5 / 9;

return celsius;
}
};

void main()
{
cout<<"Please select the convertor direction\n";
cout << "1. From Celsius to Fahrenheit.\n";
cout << "2. From Fahrenheit to Celsius.\n";
cout << ":";

string selection;
int selectionType;
cin >> selection;
double F, C = 0;

if (selection.compare("1") == 0)
{
selectionType = 1;
}
else if (selection.compare("2") == 0)
{
selectionType = 2;
}
else
{
selectionType = 3;
}

std::string value;

switch (selectionType)
{
case 1:
cout<<"Please enter the Celsius temperature:";
cin >> value;
F = TemperatureConverter::CelsiusToFahrenheit(value);
printf("Temperature in Fahrenheit:%0.2f",F);
break;

case 2:
cout << "Please enter the Fahrenheit temperature:";
cin >> value;
C = TemperatureConverter::FahrenheitToCelsius(value);
printf("Temperature in Celsius:%0.2f", C);
break;

default:
cout << "Please select a convertor.";
break;
}

// Keep the console window open in debug mode.
cout <<"\nPress any key to exit.";
_getch();
}

Wednesday, March 18, 2015

virtual function of base called from derived class

#include<iostream>
#include<conio.h>


using namespace std;

class Area
{
public:
 virtual double GetArea(int length, int breadth)
 {
  return length * breadth;
 }
};

class Rectangle : Area
{
public:
 double  GetArea(int length, int breadth)
 {
  return Area::GetArea(length, breadth);
 }
};

class Triangle : Area
{

public:
 double GetArea(int length, int breadth)
 {
  return 0.5 * Area::GetArea(length, breadth);
 }
};

void main()
{
 Rectangle oRectangle;
 Triangle oTriangle;

 cout<<"\nArea of Rectangle:"<< oRectangle.GetArea(10, 2);
 cout << "\nArea of Triangle:"<<oTriangle.GetArea(10, 2);

 _getch();

}

Out Put :
Area of Rectangle: 20
Area of Triangle:10

Tuesday, March 17, 2015

How to Get type name of a variable in C++

#include<typeinfo>
#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

void main()
{
int nIntNum = 10;
float fFloatNum = 20.5;

std::string strStringVar = "Iam String";

double dDouble = 2.001;

cout <<"Iam :"<< typeid(nIntNum).name()<<endl;
cout <<"Iam :"<< typeid(fFloatNum).name() << endl;
cout <<"Iam :"<< typeid(strStringVar).name() << endl;
cout << "Iam :" << typeid(dDouble).name() << endl;

_getch();

}

Output :

Iam : int
Iam : float
Iam : class std::basic_string
Iam : double