Sunday, 16 June 2013

Swap Two Values by Call By reference

void main(){
int a=10;b=20;
cout<<"Before Exchanging";
cout<<a << b;
exchange(&a,&b);
cout<<"After Exchanging";
cout<<a << b;
}

void exchange(int *a,int *b){
int *c;
*c=*a;
*a=*b;
*b=*c;
}

Output of the above Program:

Before Exchanging: 10 20
After Exchanging: 20 10

Object Oriented Methodology

Object Oriented Methodology (OOM) is a system development approach encouraging and facilitating re-use of software components. With this methodology, a computer system can be developed on a component basis which enables the effective re-use of existing components and facilitates the sharing of its components by other systems.

Saturday, 15 June 2013

Operator Overloading

Generally the + operator is able to add two integer values or two float values etc but it is not able to add two objects. Suppose we need to add two complex numbers using + operator. We cannot do that directly. for that we need  to overload + operator and that can be done using operator overloading as given below.

Class Complex{
public:
int x;
int y;
Complex(int a,int b){
x=a;
y=b;
}

Complex operator+(Complex c);
};

Complex Complex :: operator+(Complex c){
Complex c2;
c2.x=x+c.x;
c2.y=y+c.y;
return c2;
}

void main(){
Complex c1(10,20),c2(-2,4),c3;
c3=c1+c2;
cout<<c3.x
cout<<c3.y
}

State Diagram for ATM


State Diagram for Phone Call


Tuesday, 4 June 2013

Virtual Keyword

class A{
public:
void hello(){
cout<<"hello from A";
}
}

class B:public A{
public:
// over riding method hello from A
void hello(){
cout<<"hello from B";
}
}

void main(){
A *a; // reference of A
B b; // object of B
a=&b; // reference of  A pointing to B object
a->hello();
}

Output of the above code:
hello from A

But if we make hello method in class A virtual then the output will be "hello from B".

and if the method hello in class A is written as

void hello(); (i.e no definition is given)  then this method is called pure virtual function.


There is also a concept of virtual base class.

class A{
public :
int i;
}
class B:virtual public A{
}
class C:virtual public A{
}
class D: public B,C{
}

Here class B and C virtually inherits A and class D inherits B and C both. So there will be only one copy of i in class D.
Here class A is called virtual base class.

Sunday, 2 June 2013

C++ Master Example

Following is the C++ master Program which contains examples for the following topics
// static
// this pointer
// constructor copy & parameterized
// destructor
// friend function
// inline function
// class and Objects
// call by reference
// overloading constructor
//default argument
You can write this example for any of the above theory.

class Person{

private:
int id;

public :
int age;
char fname[25];
char lname[25];
static int count;

// same name as class name
//no return type
// inittializes instance variables
// are called automatically when objects are created

Person(){
id=0;
age=0;
fname="";
lname="";
count++;
}

// default arguments
// parameterized constructor

Person(int a,int b=10,char c[],char d[]){
id=a;
age=b;
fname=c;
lname=d;
count++;
}

// copy constructor
Person(Person &p){
id=p.id;
age=p.age;
fname=p.fname;
lname=p.lname;
count++;
}
// destructor
~Person(){
cout << "Deleting Object "+count;
count--;
}

// conditions for function should be inline
// no static variable should be used
// loop or switch should not be there
// for fucntion not return values if return statement exists
// no recursion

inline int getAgeAfter10Years(){
return (age+10);
}

// can access private members of class
// generally has objects as arguments
// is not a member of a class
friend Person getPersonWithGreaterId(Person &p1,Person &p2);

// this pointer explanation
public Person greater(Person &p);
public static Person smaller(Person &p1,Person &p2);
}; // class ends

public Person Person::greater(Person &p){
if(age>p.age){
return *this;
}
else{
return p;
}
}

public static Person Person::smaller(Person &p1,Person &p2){
if(p1.age>p2.age){
return p2;
}
else{
return p1;
}
}
public Person getPersonWithGreaterId(Person &p1,Person &p2){
if(p1.id>p2.id){
return p1;
}
else{
return p2;
}
}


void main(){
Person p1,p2(1,20,"Mohan","Sharma"),p3(2,21,"Yatin","Makwana"),p4(3,22,"Pradip","Makwana");
Person p11(1,"Ronak","Kotak");
{
Person p5;
}
Person p6(p4);  //calling copy constructor
cout<<p6.fname;

Person p7=p3;  //calling copy constructor

cout<<p7.fname;

Person p8=p1.greater(p2);
cout<< p8.fname;
Person p9=Person.smaller(p2,p3);
cout<< p9.fname;
cout<< p9.getAgeAfter10Years();
Person p10=getPersonWithGreaterId(p3,p7);
cout<< p10.fname;
}

Output:

Deleting object 6
Pradip
Yatin
Mohan
Mohan
30
Yatin
Deleting Object 7
Deleting Object 6
Deleting Object 5
Deleting Object 4
Deleting Object 3
Deleting Object 2
Deleting Object 1