Sunday 23 November 2014

Difference between call by value and call by reference

1. In call by value we pass a copy of actual arguments to formal arguments however in call by reference we pass the original values through reference variable.
2.In call by value if we make any change in formal argument the same changes will not take place in actual argument, whereas in call by reference the changes will take place in call by reference.

Example of call by value

#include<iostream.h>
#include<conio.h>
void swap(int,int);
void main()
{
clrscr();
int x,y;
cout<<"enter two numbers "<<endl;
cin>>x>>y;
swap(x,y);  // actual arguments
cout<<"After Swapping:-"<<endl;
cout<<"The value of x is "<<x<<endl;
cout<<"The value of y is "<<y<<endl;
getch();
}
void swap(int a,int b)        // Formal arguments
{
int c;
c=a;
a=b;
b=c;
cout<<"In swap function :-"<<endl;
cout<<"The value of a is "<<a<<endl;
cout<<"The value of b is "<<b<<endl;
}

Output :
Enter two number 
10
20

In Swap function:-
The value of a is 20
The value of b is 10
In main function:-
The value of x is 10
The value of y is 20

Example of call by reference

#include<iostream.h>
#include<conio.h>
void swap(int & ,int &);
void main()
{
clrscr();
int x,y;
cout<<"enter two numbers "<<endl;
cin>>x>>y;
swap(x,y);  // actual arguments
cout<<"After Swapping:-"<<endl;
cout<<"The value of x is "<<x<<endl;
cout<<"The value of y is "<<y<<endl;
getch();
}
void swap(int &a,int &b)        // Formal arguments
{
int c;
c=a;
a=b;
b=c;
cout<<"in swap function :-"<<endl;
cout<<"The value of a is "<<a<<endl;
cout<<"The value of b is "<<b<<endl;
}


Output :
Enter two number 
10
20

In Swap function:-
The value of a is 20
The value of b is 10
In main function:-
The value of x is 20
The value of y is 10

Monday 14 July 2014

More programs on Class Concept

Program 1

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class stock
{
int icode,qty;
char des[20];
float price,discount;
void finddisc()
{
if(qty<=50)
discount=0;
else if(qty<=100)
discount=5;
else
discount=10;
}
public:
void buy()
{
cout<<"Enter icode,name,price,quantity"<<endl;
cin>>icode;
gets(des);
cin>>price>>qty;
finddisc();
}
void showall()
{
cout<<"The item code is"<<icode<<endl;
cout<<"The item description is"<<des<<endl;
cout<<"The Price is"<<price<<endl;
cout<<"The quantity is"<<qty<<endl;
cout<<"The discount  is"<<discount<<endl;
}
};
void main()
{
stock s;
clrscr();
s.buy();
s.showall();
getch();
}





Program 2

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class resort
{
int rno;
char name[20];
float charges,amount;
int days;
float compute()
{
  if((days*charges)>11000)
{
return 1.02*days*charges;
}
else
{
return days * charges;
}
}
public:
void getinfo()
{
cout<<"enter room no , name , charges and days"<<endl;
cin>>rno;
gets(name);
cin>>charges>>days;
}
void dispinfo()
{
cout<<"Room No is:  " <<rno<<endl;
cout<<"Customer name is :" <<name<<endl;
cout<<"Amount is :"<<compute();
}
};
void main()
{
clrscr();
resort r;
r.getinfo();
r.dispinfo();
getch();
}




Friday 2 May 2014

Classes & Objects
A Class is a collection of data and functions working on that data. The class follows the concept of encapsulation.
An object is an instance of class containing real world data.

In a class we simply declare member data and define member functions that work on the data but the value of the data is in correspondence to the object.

 #include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[20];
float fees;
public:
void accept()
{
cout<<"Enter the details of the student"<<endl;
cin>>rno
gets(name);
cin>>fees;
}
void disp()
{
cout<<"The rollno is '<<rno<<endl;
cout<<"The Nmae is"<<name<<endl;
cout<<"The Fees is "<<fees;
}
};
void main()
{
clrscfr();
student s;
s.accept();
s.disp();
getch();
}