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