Monday, April 27, 2015

static in C++ vs Static class in C#

There is no static class in c++ like in C# , instead we can declare static members and fuctions inside a class in c++

C++ Source :

#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();
}



C# source :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticClassSln
{

    public static class TemperatureConverter
    {
        public static double CelsiusToFahrenheit(string temperatureCelsius)
        {
            // Convert argument to double for calculations.
            double celsius = Double.Parse(temperatureCelsius);

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

            return fahrenheit;
        }

        public static double FahrenheitToCelsius(string temperatureFahrenheit)
        {
            // Convert argument to double for calculations.
            double fahrenheit = Double.Parse(temperatureFahrenheit);

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

            return celsius;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please select the convertor direction");
            Console.WriteLine("1. From Celsius to Fahrenheit.");
            Console.WriteLine("2. From Fahrenheit to Celsius.");
            Console.Write(":");

            string selection = Console.ReadLine();
            double F, C = 0;

            switch (selection)
            {
                case "1":
                    Console.Write("Please enter the Celsius temperature: ");
                    F = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine());
                    Console.WriteLine("Temperature in Fahrenheit: {0:F2}", F);
                    break;

                case "2":
                    Console.Write("Please enter the Fahrenheit temperature: ");
                    C = TemperatureConverter.FahrenheitToCelsius(Console.ReadLine());
                    Console.WriteLine("Temperature in Celsius: {0:F2}", C);
                    break;

                default:
                    Console.WriteLine("Please select a convertor.");
                    break;
            }

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

Wednesday, March 18, 2015

C++ list vs C# list

C++ Source code :

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

using namespace std;


void main()
{

list<int> mylist;

mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);

list<int>::iterator itrlst;

for (itrlst = mylist.begin(); itrlst != mylist.end(); itrlst++)
{
cout << *itrlst << "Element" << *itrlst<<"\n";
}


_getch();

}

output :

1 Element 1
2 Element 2
3 Element 3


C# souce code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project2
{
    
    public class Class1
    {
       
        static void Main()
        {
            
            var mylist = new List<int>();

            mylist.Add(1);
            mylist.Add(2);
            mylist.Add(3);

            foreach(int element in mylist)
            {
                Console.WriteLine("{0} Element:{0}", element, element);
               
            }

            Console.ReadLine();

            return;
        }
    }
}


output :

1 Element 1
2 Element 2
3 Element 3

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

C# source code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project2
{
    public abstract class Area
    {
        public virtual double GetArea(int nlenght, int nbreadth)
        {
            return nlenght * nbreadth;
        }
    }

    public class Rectangle : Area
    {
        public override double GetArea(int nlength, int nbreadth)
        {
            return base.GetArea(nlength, nbreadth);
        }
    }

    public class Triangle : Area
    {
        public override double GetArea(int nlength, int nbreadth)
        {
            return 0.5 * base.GetArea(nlength, nbreadth);
        }
    }
    public class Class1
    {
     
        static void Main()
        {
            Rectangle oRectangle = new Rectangle();
            Triangle oTriangle = new Triangle();

            Console.WriteLine("Area of Rectangle:{0}",oRectangle.GetArea(10, 2));
            Console.WriteLine("Area of Triangle:{0}",oTriangle.GetArea(10, 2));

            Console.ReadLine();

            return;
        }
    }
}


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


Pure virtual function in C++ vs abstract method in C#

C++ Source code :

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


using namespace std;

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

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

class Triangle : Area
{

public:
double GetArea(int length, int breadth)
{
return 0.5 * 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

C# source code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project2
{
    public abstract class Area
    {
        public abstract double GetArea(int nlenght, int nbreadth);
    }

    public class Rectangle : Area
    {
        public override double GetArea(int nlength, int nbreadth)
        {
            return nlength * nbreadth;
        }
    }

    public class Triangle : Area
    {
        public override double GetArea(int nlength, int nbreadth)
        {
            return 0.5 * nlength * nbreadth;
        }
    }
    public class Class1
    {
     
        static void Main()
        {
            Rectangle oRectangle = new Rectangle();
            Triangle oTriangle = new Triangle();

            Console.WriteLine("Area of Rectangle:{0}",oRectangle.GetArea(10, 2));
            Console.WriteLine("Area of Triangle:{0}",oTriangle.GetArea(10, 2));

            Console.ReadLine();

            return;
        }
    }
}


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

Tuesday, March 17, 2015

Iterator in C++ vs foreach in C#

C++ source:

#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <array>
int main() {
 // construction uses aggregate initialization std::array<int, 5> i_array1{ {3, 4, 5, 1, 2} };
 std::cout << "Initial i_array1 : ";
array<int, 5>::iterator  it;
for(it=i_array1.begin(), it!=i_array1.end();it++);
{
   cout<<*it<<endl;
}

return 0;
}


C# Source:



using System;
namespace Wrox{
public class MyFirstClass
{
static void Main()
{

     int[ ] arrayOfInts = {1,2,3,4,5};

    foreach (int temp in arrayOfInts)
   {
        temp++;
       Console.WriteLine(temp);

  }
 return;
}

Get type name of a variable in C++/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


using System;
namespace MyNamespace
{
class MyProgram
{
 static void Main(string[] args)
 {
   var strStringVar = "Bugs Bunny";
   var nIntNum = 25;
   var fFloatNum = 25.3;
   Type IntType = nIntNum.GetType();
   Type strType = strStringVar.GetType();
   Type FloatType = fFloatNum.GetType();
   Console.WriteLine("Iam " + IntType.ToString());
   Console.WriteLine("Iam " + strType.ToString());
   Console.WriteLine("Iam " + FloatType.ToString());
   Console.ReadLine();
 }
 }
}

The output:
Iam System.Int32
Iam System.String
Iam System.Single

Example of a console application

C++ Source code:

#include<stdio.h>
#include<conio.h>

void main( )
{
      printf("Hi world  This is my C++ source code");

    getch( );  // _getch( ) in MSVC compiler

}

C# Source code:

using System;
namespace Wrox{
public class MyFirstClass
{
   static void Main()
  {
     Console.WriteLine("Hi world  This is my C# source code.");
     Console.ReadLine();
    return;
  }
}
}