Wednesday 17 July 2013

Know about virtual functions in c++

// Base class is going to add a virtual pointer
// Every class having virtual functions have a vtable
// vtable contains the list of functions pointers which object of that class can call
// vptr points to the function defination of the class it points to.




* Class Test show the concept of virtual tables
// Derived class having overridden functions
// VPTR And the concept of virtual table which contains static array created at compile time listing all function pointer to
// virtual functions that can be called by the object of the class .

 FUNCTION IS MAPPED TO THE CORRESPONDING FUNCTION OF THE CLASS

class test
{
int val;
public:
test()
{
cout<<"Default Constructor of base class"<<endl;
}
virtual void fun1()
{
cout<<"Base class function fun1"<<endl;
}
test(int x)
{
val=x;
}
virtual void fun2()
{
cout<<"Base class function fun2"<<endl;
}
};class Housetest:public test
{
int houseval;
public:
Housetest()
{
cout<<"I AM DEFAUTL CONSTRUCNTOR FOR HOUSE TEST"<<endl;
cout<<"House Test dont have fun1"<<endl;
}
Housetest(int x):test(x)
{
houseval=x;
cout<<"Value of x"<<houseval;
}
void fun1()
{
cout<<" I am fun1 from house test"<<endl;
}
};
class classtest:public test
{
int classval;

public:
classtest()
{
cout<<"Class test dont have fun1"<<endl;
}
void fun2()
{
cout<<" I AM FUN2 FROM CLASS TEST"<<endl;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
test *x= new classtest;

x->fun1();
x->fun2();
cout<<"----------------------------------------------------\n"<<endl;
cout<<"I am creating a new object of House test now"<<endl;
cout<<"----------------------------------------------------\n"<<endl;
x=new Housetest;
x->fun1();
x->fun2();
return 0;
}

Operator overloading in c++ with example

Operator overloading

This gives us flexibility to use operators with class objects.

Operator overloading is done by two ways

1. Member function : Take atmost one argument
2. Friend function: Take at most two arguments

This limits the overloading to apply for uninary and binary operators.

// The Test Class show the opertor overloading
// Overloads + operator using Friend function
// Overloads - operator using Member function
class test
{
public:
int a;
int b;
friend test operator +(test &, test &);
test operator -(test &);
test()
{
}
test(int x, int y)
{
a=x;
b=y;
}};test test::operator -(test &t1)
{
test temp;
temp.a= t1.a-a;
temp.b= t1.b-b;
return temp; }test operator +(test &t1, test &t2)
{
test temp;
temp.a=t1.a+t2.a;
temp.b=t2.b+t2.b;
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
test t1(3000,2000);
test t2(1000,1000);
test t3;t3= t1-t2;
cout<<t3.a<<t3.b<<endl;
t3=t1+t2;
cout<<t3.a<<t3.b<<endl;
return 0;
}



// Class Balance Perfom operator Overloading
/// Overloads + * using member function
// Overloads / - using friend function
class balance
{
public:
int start;
int end;
public:
friend balance operator /(balance &, balance &);
balance()
{
start=end=0;
}
balance(int x,int y)
{
start=x;
end=y;
}
balance operator +(balance&);
friend balance operator -(balance &, balance &);
balance operator *(balance &);
};
balance balance::operator*(balance &b1)
{
balance temp;
temp.start= b1.start*start;
temp.end=b1.end*end;
return temp;
 }balance operator /(balance &b1,balance &b2)
{
balance temp;
temp.start= b1.start / b2.start;
temp.end =b1.end /b2.end;
return temp;
}
 
 
 
 
balance balance:: operator +(balance &b1)
{
balance temp;
temp.start=b1.start+start;
temp.end=b1.end+end;
return temp;
}
balance operator -(balance &t1, balance &t2)
{
balance temp;
temp.start=t1.start -t2.start;
temp.end=t1.end- t2.end;
return temp; }int _tmain(int argc, _TCHAR* argv[])
{
 balance b1(100,0);
balance b2(100,20);
balance b3;
cout<<"------------START---------------------\n";
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------add---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1+b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------sub---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1-b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;

cout<<"------------mul---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1*b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------div---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1/b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
return 0;
}

Casting in c++

The below example demostrates the types of casting available with c++.

C++ is strongly types language. 

The example below demostrates the diffrent.

Please read the comments to know some theory about the diffrent types of castings.

class base
{
public:
int a;base()
{
cout<<
"Base class object"<<endl;}

void fun(){
cout<<
"Function from base class"<<endl;}
};
class der:public base{
public:der()
{
cout<<
"Der class object"<<endl;}

int b;
void fder(){
cout<<
"Function from derieved"<<endl;}
};
class unrelated{
public:unrelated()
{
cout<<
"Unrelated class object"<<endl;}

void funrelelated(){
cout<<
"Function is unrelated"<<endl;}
};
 
int _tmain(int argc, _TCHAR* argv[]){

// created pointer and objects of each of the class we defined base *bp;
base bo;
der *dp;
der doo;
unrelated *up;
unrelated uo;
// Lets talk about Dynamic cast before we start implementing// 1. Applies to pointer and objects of related classe// 2. Allows downcasting fails in downcasting// 3 Always check that the object are converted or notbp= dynamic_cast<base * >(&doo);//dp= dynamic_cast<der *>(&bo);// This statement will fail or not compile
// Static cast can perform conversion between pointers to related clasees.
// No overhead of type safety check
der *stcst=static_cast<der *>(bp);// Reineterpretcast covnverts any pointer type to any other pointer typer even of unrelated classesbase *bp= new base;unrelated *ur=
reinterpret_cast<unrelated *> (bp);// Constant removes the constantness of object


return
0;}

Apache Beam Learning In Java Tutorials

 Wow  https://youtu.be/9kGETU63rkc