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;
  }
}
}