Professional Documents
Culture Documents
OF
PROGRAMMING
IN
C++
SUBMITTED TO:
SUBMITTED BY:
JAHANVI ALAGH
ASSISTANT PROFESSOR
INDEX
Program for demonstration of function
Program to short the element of array
Program for swapping integer by passing values
Program for swapping integer by passing address
Demonstration of inline functions
Function with default argument
Demonstration of pointer to function
Program for function Overloading
Program for swapping integer using function Overloading
Accessing member function with in a class
Demonstration of data hiding
Demonstration of Friend function
Program for defining member function inside a class
Program for defining member function outside of class
Program for defining member function outside as inline35
Demonstration of static member function of a class
Program to implement a Constructor
Program to demonstration of Parameterized constructor
Demonstration Constructor together with Destructor
Program for overloading a Constructor
Demonstration of Constructor with default argument
Program to implement copy constructor
Program for Operator Overloading
#include<iostream.h>
#include<conio.h>
int max(int x, int y); //prototype
void main()
{
int a,b,c;
clrscr();
cout<<"Enter two integer<a,b>";
cin>>a>>b;
c=max(a,b);
cout<<"Max(a,b): "<<c<<endl;
getch();
}
int max(int x, int y) //Function defination
{
if(x>y)
return x;
else
return y;
}
Max(a,b):5
12
void main(void)
{
13
int a[25];
int i,size;
clrscr();
cout<<"Program to sort elements ..."<<endl;
cout<<"Enter the the size of integer vector <max-25>:";
cin>>size;
cout<<"Enter the elements of the integer vector...."<<endl;
for(i=0;i<size;i++)
{
cin>>a[i];
}
bubblesort(a,size);
cout<<"Sorted vector:"<<endl;
for(i=0;i<size;i++)
cout<<a[i]<<"";
getch();
}
15967
Sorted vector:
15679
if (fQuit)
break;
PrintVals(valOne, valTwo);
pFunc(valOne, valTwo);
PrintVals(valOne, valTwo);
}
return 0;
}
void PrintVals(int x, int y)
{
cout <<"x: "<< x <<" y: "<< y << endl;
}
void Square (int & rX, int & rY)
{
rX *= rX;
rY *= rY;
}
void Cube (int & rX, int & rY)
{
int tmp;
tmp = rX;
rX *= rX;
rX = rX * tmp;
tmp = rY;
20
rY *= rY;
rY = rY * tmp;
}
void Swap(int & rX, int & rY)
{
int temp;
temp = rX;
rX = rY;
rY = temp;
}
void GetVals (int & rValOne, int & rValTwo)
{
cout <<"New value for ValOne: ";
cin >> rValOne;
cout <<"New value for ValTwo: ";
cin >> rValTwo;
}
OUTPUT:(0) Quit (1) Change value (2) Square (3) cube (4) swap :1
x: 1 y: 2
New value for valone: 4
New value for valtwo:5
x:4 y:5
(1) Quit (1) Change value (2) Square (3) cube (4) swap :2
x: 4 y:5
x: 16 y: 25
OUTPUT:myInt: 6500
myLong: 65000
myFloat: 6.5
myDouble: 6.5e+20
inDouble(int)
inDouble(long)
inDouble(float)
inDouble(double)
DoubleInt: 13000
Doublelong: 130000
Doublefloat: 13
Doubledouble: 1.3e+21
clrscr();
char ch1,ch2;
cout<<"Enter two chatacter <ch1,ch2>:";
cin>>ch1>>ch2;
swap(ch1, ch2); //Copiler calls swap(char &x, char &y)
cout<<"After swapping<ch1,ch2>:"<<""<<ch1<<""<<ch2<<endl;
int x,y;
cout<<"Enter two integer <x,y>:";
cin>>x>>y;
swap(x,y); //Copiler calls swap(int &x, int &y)
cout<<"After swapping<x,y>:"<<""<<x<<""<<y<<endl;
float c,d;
cout<<"Enter two Floats <c,d>:";
cin>>c>>d;
swap(c, d); //Copiler calls swap(float &x, float &y)
cout<<"After swapping<c,d>:"<<""<<c<<""<<d<<endl;
getch();
}
};
void main()
{
clrscr();
numberpairs n1;
n1.read();
n1.showmax();
getch();
}
clrscr();
//values passed to object
p1.setpart(1996,23,1250.55);
p2.setpart(2000,243,2354.75);
//each object display their value
cout<<"First part details....."<<endl;
p1.showpart();
cout<<"Second part details...."<<endl;
p2.showpart();
getch();
}
d1.set(26,2,1990);
d2.set(14,4,1971);
d3.set(1,9,1733);
cout<<"Birthdate of the first author:";
d1.show();
cout<<"Birthdate of the second author:";
d2.show();
cout<<"Birthdate of the third author:";
d3.show();
getch();
}
clrscr();
// Set date of birth
d1.set(26,2,1990);
d2.set(14,4,1971);
d3.set(1,9,1733);
cout<<"Birthdate of the first author:";
d1.show();
cout<<"Birthdate of the second author:";
d2.show();
cout<<"Birthdate of the third author:";
d3.show();
getch();
}
clrscr();
// Set date of birth
d1.set(26,2,1990);
36
d2.set(14,4,1971);
d3.set(1,9,1733);
cout<<"Birthdate of the first author:";
d1.show();
cout<<"Birthdate of the second author:";
d2.show();
cout<<"Birthdate of the third author:";
d3.show();
getch();
}
38
//Alternative calling setpath without an object of the class directory
directory::setpath("/usr");
cout<<"path:"<<directory::path<<endl;
//calling with object
directory dir;
dir.setpath("/etc" );
cout<<"path:"<<dir.path;
getch();
}
OUTPUT:path/usr/udit
path/usr
path/etc
cout<<endl;
}
void main()
{
int item;
bag bag;
clrscr();
while(1)
{
cout<<"Enter item number<0-no item>:";
cin>>item;
if(item==0) //item ends,break
break;
bag.put(item);
cout<<"Item in bag:";
bag.show();
}
getch();
}
{
if(itemcount)
for(int i=0; i<itemcount; i++)
cout<<contents[i]<<"";
else
cout<<"Nil";
cout<<endl;
}
void main()
{
int item;
bag bag1;
bag bag2=4;
clrscr();
cout<<"Gifted bag 1 initially has:";
bag1.show();
cout<<"Gifted bag2 initially has:";
bag2.show();
while(1)
{
cout<<"Enter Item number:";
cin>>item;
if(item==0)
break;
bag2.put(item);
cout<<"Item in bag2:";
bag2.show();
}
getch();
}
clrscr();
obj1.show();
{ //new block
myclass obj1,obj2;
obj2.show();
getch();
}
void display()
{
cout<<"Account number is :"<<accno<<endl;
cout<<"Balance is:"<<balance<<endl;
}
void moneytransfer(AccClass &acc,float amount);
};
void AccClass::moneytransfer(AccClass &acc,float amount)
{
balance=balance-amount;
acc.balance=acc.balance+amount;
}
void main()
{
int trans_money;
clrscr();
AccClass acc1;
AccClass acc2(10);
AccClass acc3(20,750.5);
cout<<"Account Information......"<<endl;
acc1.display();
acc2.display();
acc3.display();
cout<<"How much money to be transferd from acc3 to acc1:";
cin>>trans_money;
acc3.moneytransfer(acc1,trans_money);
cout<<fabs(imag);endl;
}
complex add(complex c2);
};
complex complex::add(complex c2)
{
complex temp;
temp.real=real+c2.real;
temp.imag=imag+c2.imag;
return(temp);
}
void main()
{
clrscr();
complex c1(1.5 , 2.0);
complex c2(2.2);
complex c3;
c1.show("C1=");
c2.show("C2=");
c3=c1.add(c2); //add c1 and c2 assign to c3
c3.show("C3=c1.add(c2);");
getch();
}
OUTPUT:C1= 1.5 + i2
C2= 2.2 + i0
C3= c1.add(c2): 3.7 + i2
code c=a;
51
code d;
d=a;
cout<<"\n id of a:";
a.display();
cout<<"\n id of b:";
b.display();
cout<<"\n id of c:";
c.display();
cout<<"\n id of d:";
d.display();
getch();
}
OUTPUT:id of a: 100
id of b: 100
id of c: 100
id of d: 100
cout<<"\n Index1="<<idx1.getindex();
cout<<"\n Index2="<<idx2.getindex();
// Advance index object with ++ operator
++idx1;
idx2++;
idx2++;
cout<<"\n Index1="<<idx1.getindex();
cout<<"\n Index2="<<idx2.getindex();
getch();
}
OUTPUT:Index1=0
Index2=0
Index1=1
Index2=2
void main()
{
index idx1 =5;
index idx2=10;
clrscr();
cout<<"\n Index1="<<idx1.getindex();
cout<<"\n Index2="<<idx2.getindex();
if(idx1<idx2)
cout<<"\n Index1 is less than Index2";
else
cout<<"\nIndex1 is not less than Index2";
getch();
}
OUTPUT:Index1=5
Index2=10
Index1 is less then Index2
int sum();
};
void vector::read()
{
for( int i=0;i<ARRAY_SIZE; i++)
{
cout<<"Vector["<<i<<"]=?";
cin>> array[i];
}
}
int vector::sum()
{
int sum=0;
for(int i=0;i<ARRAY_SIZE;i++)
sum+=array[i];
return sum;
}
void main()
{
clrscr();
vector *my_vector= new vector;
cout<<"Enter Vector data..."<<endl;
my_vector->read();
cout<<"Sum of Vector="<<my_vector->sum();
delete my_vector;
getch();