Wednesday, November 27, 2013

Virtual Function in C++ Programming

                             If there are member function with same name in derived classes, virtual functions gives programmer capability to call member function of different class by a same function call depending upon different context. This feature in C++ programming is known as polymorphism which is one of the important feature of OOP.

Example
#include <stdio.h>
  using namespace std; 
   class B { 
       public: 
           void display() {
                  cout<<"Content of base class.\n"; 
               }
 }; 
class D : public B {

 public:   
 void display() {
      cout<<"Content of derived class.\n"; 
      }
 };
 int main() { 

B *b; 
D d;
 b->display(); 
b = &d; /* Address of object d in pointer variable */ 
b->display(); 
return 0; 
}

Output 
Content of base class. 
Content of base class. 



If you want to execute the member function of derived class then, you can declare display( ) in the base class virtual which makes that function existing in appearance only but, you can't call that function. In order to make a function virtual, you have to add keyword virtual in front of a function. 



* Example to demonstrate the working of virtual function in C++ programming. */

#include <iostream>
using namespace std;
class B
{
    public:
     virtual void display()      /* Virtual function */
         { cout<<"Content of base class.\n"; }
};

class D1 : public B
{
    public:
       void display()
         { cout<<"Content of first derived class.\n"; }
};

class D2 : public B
{
    public:
       void display()
         { cout<<"Content of second derived class.\n"; }
};

int main()
{
    B *b;
    D1 d1;
    D2 d2;

/* b->display();  // You cannot use this code here because the function of base class is virtual. */

    b = &d1;
    b->display();   /* calls display() of class derived D1 */
    b = &d2;           
    b->display();   /* calls display() of class derived D2 */
    return 0;
}
Output
-------------


Content of first derived class.
Content of second derived class.

Inheritance in C++


What to inherit?
In principle, every member of a base class is inherited by a derived class 
– just with different access permission
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:
class derived-class: access-specifier base-class
Access Control Over the Members
• Two levels of access control over class members
class definition inheritance type
base class/ superclass/ parent class

class Point{
protected: int x, y;
public: void set(int a, int b);
};
derived class/ subclass/ child class
class Circle : public Point{ ......
};
0 

Example 
// derived classes
#include <iostream>
using namespace std;

class CPolygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b;}
  };

class CRectangle: public CPolygon {
  public:
    int area ()
      { return (width * height); }
  };

class CTriangle: public CPolygon {
  public:
    int area ()
      { return (width * height / 2); }
  };
  
int main () {
  CRectangle rect;
  CTriangle trgl;
  rect.set_values (4,5);
  trgl.set_values (4,5);
  cout << rect.area() << endl;
  cout << trgl.area() << endl;
  return 0;
}

Friend classes

In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends.

Friends are functions or classes declared with the friend keyword.
A class as friend of another one, granting that first class access to the protected and private members of the second one.

/ friend class
#include <iostream>

class CSquare;

class CRectangle {
    int width, height;
  public:
    int area ()
      {return (width * height);}
    void convert (CSquare a);
};

class CSquare {
  private:
    int side;
  public:
    void set_side (int a)
      {side=a;}
    friend class CRectangle;
};

void CRectangle::convert (CSquare a) {
  width = a.side;
  height = a.side;
}
  
int main () {
  CSquare sqr;
  CRectangle rect;
  sqr.set_side(4);
  rect.convert(sqr);
  cout << rect.area();
  return 0;
}

Friend functions

Private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends,

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.

Friends are functions or classes declared with the friend keyword.
                          If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend: 

#include <iostream>
 
 
class Student
{
   double id;
public:
   friend void printid( Student Student );
   void setId( double wid );
};

// Member function definition
void  Student ::setId( double wid )
{
    id = wid;
}

// Note: printId() is not a member function of any class.
void printId(  Student  student )
{
   /* Because setid() is a friend of Student, it can
    directly access any member of this class */
   cout << "Id of student : " << student .id <<endl;
}
 
// Main function for the program
int main( )
{
    Student  student ;
 
   // set student id without member function
   student .setId(10.0);
   
   // Use friend function to print the wdith.
   printId( student );
 
   return 0;
}

What is Arduino?

                 

Arduino is a tool for making computers that can sense and control more of the physical world than your desktop computer. It's an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board.

   Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

Arduino can be used to develop interactive objects, taking inputs from a variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects can be stand-alone, or they can be communicate with software running on your computer (e.g. Flash, Processing, MaxMSP.) The boards can be assembled by hand or purchased preassembled; the open-source IDE can be downloaded for free.

Thursday, February 7, 2013

Dynamic gradient in android




Step One:- create a class
import android.graphics.drawable.GradientDrawable;

public class DrawableGradient extends GradientDrawable {
public DrawableGradient(int[] colors, int cornerRadius) {
super(GradientDrawable.Orientation.BR_TL, colors);

try {
this.setShape(GradientDrawable.RECTANGLE);
this.setGradientType(GradientDrawable.RECTANGLE);
this.setCornerRadius(cornerRadius);
} catch (Exception e) {
e.printStackTrace();
}
}

public DrawableGradient SetTransparency(int transparencyPercent) {
this.setAlpha(255 - ((255 * transparencyPercent) / 100));

return this;
}
}

step 2 : use this
TextView.setBackgroundDrawable(new DrawableGradient(new int[] {
0xFFFF0000, 0xFF800000, 0xFF000000 }, 0).SetTransparency(10));

email validation in android




protected final Pattern EMAIL_ADDRESS_PATTERN = Pattern
.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@"
+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");

/**
* for email validation
*
* @param str
*            = string for validation
* @return true if validation is correct
*/
public boolean emailValidation(String str) {
if (str.equals("")
|| EMAIL_ADDRESS_PATTERN.matcher(str).matches() == false)
return false;
else
return true;
}