Wednesday, August 29, 2012

Simple string program using string library

// simple string using c++ style
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1 = "pine";
    string s2 = "apple";
    cout << "\n length of s1 is " << s1.length() << endl;
    string s3 = s1 + s2;
    cout << "\n s1 + s2 = " << s3 << endl;
    string s4 = s3.substr(2,4);
    cout << "\n s3.substr(2,4)= " << s4 << endl;
   
    cout << "\n s3.find(\"neap\")= " << s3.find("neap") << endl;
    cout << "\n s3.rfind('p')= " << s3.rfind('p') << endl;
    return 0;
}


OUTPUT:
 ----------------------------------------------------------------------------------------------

// to get line of text (that may include spaces) from the user
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    cout << "\n type a string : ";
    getline(cin,str);
   
    cout << "\n you typed : " << str << endl;
    return 0;
}


OUTPUT:

Thursday, August 16, 2012

What is Copy Constructor

What is a COPY CONSTRUCTOR and when is it called?
A copy constructor is a method that accepts an object of the same class and
copies it’s data members to the object on the left part of assignement.

#include <iostream>
using namespace std;

class Point2D
{
    int x; int y;

    public: int color;
    protected: bool pinned;
    public: Point2D() : x(0) , y(0) {} //default (no argument) constructor
    public: Point2D( const Point2D & ) ;
};

Point2D::Point2D( const Point2D & p )
{
    this->x = p.x;
    this->y = p.y;
    this->color = p.color;
    this->pinned = p.pinned;
}

int main()
{
    Point2D MyPoint;
    MyPoint.color = 345;
    Point2D AnotherPoint = Point2D( MyPoint ); // now AnotherPoint has color = 345
   
    cout << "\n" << AnotherPoint.color << endl;
}


OUTPUT:

Tuesday, August 14, 2012

virtual inheritance and runtime polymorphism

// Using dot net and C#
using System;

public class a
{
    public void Hockey()
    {
        Console.WriteLine("A play hockey");
    }

    public virtual void education()
    {
        Console.WriteLine("MBBS");
    }

}
public class b:a
{
    public void cricket()
    {
        Console.WriteLine("B play cricket");
    }
    public override void education()
    {
        Console.WriteLine("MCA");
    }
}

class demo
{
    public static void Main()
    {
        a a1=new b();            //run time polymorphism
        a1.education();
        Console.ReadLine();
    }
}


OUTPUT:

To see in c++ CLICK HERE

Quick notes to C programmers

instead of macros use
    • const or enum to define constants
    • inline to prevent function call overload
    • template to declare families of type and families of functions 

  • use new/delete instead of free/malloc (use delete[] for arrays) 

  • don't use void* and pointer arithmetic 

  • an explicit type conversion reveals an error of conception. 

  • avoid to use C style tables, use vectors instead. 

  • don't recode what is already available in the C++ standard library. 

  • variables can be declared anywhere: initialization can be done when variable is required. 

  • whenever a pointer cannot be zero, use a reference. 

  • when using derived class, destructors should be virtual.

Monday, August 6, 2012

Give a one-line C expression to test whether a number is a power of 2

Ans. //( (x > 0) && ((x & (x - 1)) == 0) )

#include <iostream>
using namespace std;

int main()
{
    int x;
    cout << "enter the no of : ";
    cin >> x;
   
    if( (x > 0) && ((x & (x - 1)) == 0) )
    {
        cout <<"no is power of 2";
    }
   
    else
    {
        cout <<"no is not a power of 2";
    }
   
}

Program to find whether the number is power of 2 or not?

//without  using  functions.

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

int main()
{
    int num, rem, flag=0;
    printf ("\n Enter a Number: ");
    scanf ("%d", &num);
   
    while(num > 2)
    {
        rem = num % 2;
        if( rem == 1 )
        {
            flag = 1;
            break;
        }
        else
        num = num/2;
    }
   
    if( flag == 1 )
    {
        printf("\n Number is not Power of two.");
    }
    else
    {
        printf("\n Number is Power of two");
    }
}