Wednesday, August 24, 2011

Oops Programs (1 to 6a)

Function overloading
#include<iostream.h>
#include<conio.h>
int add(int x, int y)
{
int c;
c=x+y;
return(c);
}
void add(intd,inte,int f)
{
int z;
z=d+e+f;
cout<<z<<"\n";
}
void add()
{
intx,y;
cout<<"\nEnter two integers:";
cin>>x>>y;
cout<<"\nAddition is: "<<x+y;
}
void main()
{
inta,b,c,d;
clrscr();
cout<<"\nEnter the three values:";
cin>>a>>b>>c;
cout<<"add(a,b,c)=";
add(a,b,c);
cout<<"add(a,b)="<<add(a,b);
add();
getch();
}







OUTPUT:
Enter the three values:
2
3
add(a,b,c)=6
add(a,b)=3
Enter two integers:1
2
Addition is: 3
DEFAULT ARGUMENTS
#include<iostream.h>
#include<conio.h>
int add(int,int,int n=23);
void main()
{
inta,b,c,d,e;
clrscr();
cout<<"\nEnter two values:";
cin>>a>>b;
d=add(a,b);
cout<<"d="<<d;
e=add(a,b,50);
cout<<"e="<<e;
getch();
}
int add(intx,inty,int z)
{
int d;
d=x+y+z;
cout<<"\nPrinting values :";
return(d);
}


OUTPUT:
Enter two values:1
2
Printing the values :d=26
Printing values :e=53




















SIMPLE CLASS DESIGN
#include<iostream.h>
#include<conio.h>
class add
{
inta,b;
public:
voidgetvalue()
{
cout<<"\nEnter two numbers:";
cin>>a>>b;
}
voidprintresult()
{
cout<<"SUM = "<<(a+b);
}
};
void main()
{
clrscr();
add a;
a.getvalue();
a.printresult();
getch();
}

OUTPUT:
Enter two numbers :30
40
SUM= 70




















CONSTRUCTOR & DESTRUCTOR:
#include<iostream.h>
#include<conio.h>
int count=0;
class matrix
{
public:
matrix();
 ~matrix();
};
matrix::matrix()
{
count++;
cout<<"\nObject "<<count<<" Constructor of class test is called";
}
matrix::~matrix()
{
cout<<"\nObject "<<count<<" Destructor of class test is called";
count--;
}
void main()
{
clrscr();
  {
matrix x;
matrix y;
  }
getch();
}














OUTPUT:

Object 1 constructor of class test is called
Object 2 constructor of class test is called
object 2 destructor of class test is called
object 1 destructor of class test is called

COPY CONSTRUCTOR
#include<iostream.h>
#include<conio.h>
class matrix
{
int id;
public:
matrix()
{
}
matrix(int a)
{
id=a;
}
matrix(matrix &x)
{
id=x.id;
}
display()
{
cout<<id;
return 0;
}
};
void main()
{
clrscr();
matrix a(100);
matrix b(a);
matrix c=a;
matrix=d;
d=a;
cout<<"\n ID of A object is:";
a.display();
cout<<"\n ID of B object is:";
b.display();
cout<<"\n ID of C object is:";
c.display();
cout<<"\n ID of D object is:";
d.display();
getch();
}


OUTPUT:
 ID of A object is:100
 ID of B object is:100
 ID of C object is:100
 ID of D object is:100

FRIEND FUNCTION

#include<iostream.h>
#include<conio.h>
class distance1;
class distance
{
private:
intdist;
public:
distance()
{
dist=10;
}
voidshowdist()
{
cout<<dist;
}
friend distance add(distance,distance1);
};
class distance1
{
private:
int dist1;
public:
distance1()
{
dist1=20;
}
void showdist1()
{
cout<<dist1;
}
friend distance add(distance,distance1);
};
int distance add(distance aa,distance1 bb)
{
return (aa.dist+bb.dist1);
}
void main()
{
distance d;
clrscr();
distance1.d1;
cout<<"\n Distance of class 1 object=";
d.showdist();
cout<<"\n Distance of class 2 object=";
d1.showdist1();
cout<<"\n Addition of two distances =";
cout<<distanceadd(d,d1);
getch();
}






OUTPUT:

 Distance of class 1 object=10
 Distance of class 2 object=20
 Addition of two distances =30












RUN TIME POLYMORPHISM:
#include<iostream.h>
#include<conio.h>
class base
{
private:
intx;float y;
public:
virtual void getdata();
virtual void display();
};
classderivedB:public base
{
private:
longintrollno;
char name[20];
public:
voidgetdata();
void display();
};
void base::getdata()
{
cout<<"enter an integer\n";
cin>>x;
cout<<"enter a real no\n";
cin>>y;
}
void base::display()
{
cout<<"entered nos are x="<<x<<"&y="<<y;
cout<<endl;
}
voidderivedB::getdata()
{
cout<<"enter rollno of student\n";
cin>>rollno;
cout<<"enter name of student\n";
cin>>name;
}
voidderivedB::display()
{
cout<<"rollno"<<rollno<<"\t"<<"name"<<name<<endl;
}
void main()
{
clrscr();
base *ptr;
derivedBobj;
ptr=&obj;
ptr->getdata();
ptr->display();
getch();
}







OUTPUT:
Enter the roll number of the student
2
Enter the name of a student
xxxx
roll number of student name is
2      xxxx








OVERLOADING ASSIGNMENT OPERATOR:

#include<iostream.h>
#include<conio.h>
class sample
{
private:
int x;
float y;
public:
sample(int,float);
void operation=(sample abc);
void display()

};
sample::sample(intone,float two)
{
x=one;
y=two;
}
void sample::operator=(sample abc)
{
x=abc.x;
y=abc.y;
}
void sample::display()
{
cout<<"integer number[x]="<<x<<endl;
cout<<"float value[y]="<<y<<endl;
cout<<endl;
}
void main()
{
clrscr();
sample obj1(10,-22.55);
sample obj2(20,-33.44);
obj1.operator=(obj2);
cout<<"contents of 1st object\n";
obj.display();
cout<<"contents of 2nd object\n";
obj2.display();
getch();
}








OUTPUT:
contents of the first object:
integer number(x)=20
floating value(y)    =-33

contents of the second object:
integer number(x)=20
floating value(y)    =-33
















TEMPLATE:
#include<iostream.h>
#include<conio.h>
template<class x>
voidswap_args(x &a,x&b)
{
x temp;
temp=a;
a=b;
b=temp;
}
void main()
{
int i=10,j=20;
double x=10.1,y=23.3;
char a='x',b='y';
clrscr();
cout<<"\n\n\t\t *** TEMPLATE FUNCTION *** \n";
cout<<"\n original i,j:"<<i<<"\t"<<j<<"\n";
cout<<"\n original a,b:"<<a<<"\t"<<b<<"\n";
cout<<"\n original x,y:"<<x<<"\t"<<y<<"\n";
swap_args(i,j);
swap_args(x,y);
swap_args(a,b);
cout<<"\n swapped i,j:"<<i<<"\t"<<j<<"\n";
cout<<"\n swapped a,b:"<<a<<"\t"<<b<<"\n";
cout<<"\n swapped x,y:"<<x<<"\t"<<y<<"\n";
getch();
}

OUTPUT:
originali,j  : 10   20
originalx,y: 10.1   23.3
originala,b: x,y
swappedi,j : 20   10
swappedx,y: 23.3   10.1
swappeda,b:y,x













TYPE CONVERSION
#include<iostream.h>
#include<conio.h>
class invent2;
class invent1
{
int code;
int items;
float value;
float price;
public:
invent1(inta,intb,int c)
{
code=a;
items=b;
price=c;
}
voidputdata()
{
cout<<"code:"<<code<<"\n";
cout<<"items:"<<items<<"\n";
cout<<"value:"<<value<<"\n";
}
intgetcode()
{
return code;
}
intgetitems()
{
return items;
}
floatgetprice()
{
return price;
}
operator float()
{
return(items*price);
}
};
/*operator invent1()
{
invent1 temp;
temp.code=code;
temp.value=price*items;
return temp;
}
};*/
class invent2
{
int code;
float value;
public:
invent2()
{
code=0;
value=0;
}
invent2(intx,float y)
{
code=x;
value=y;
}
voidputdata()
{
cout<<"code:"<<code<<"\n";
cout<<"value:"<<value<<"\n";
}
invent2(invent1 p)
{
code=p.getcode();
value=p.getitems()*p.getprice();
}
};
int main()
{
clrscr();
invent1 s1(100,5,140.0);
invent2 d1;
floattotal_value;
total_value=s1;
d1=s1;
cout<<"product details_invent type"<<"\n";
s1.putdata();
cout<<"stock value"<<"\n";
cout<<"value_"<<total_value<<"\n\n";
cout<<"product details_invent2 type"<<"\n";
d1.putdata();
return 0;
}
OUTPUT:
product details _invent 1 type
code:100
items:5
price:140
store value
value=700
product details _invent 2 type
code:100
value=700

INHERITANCE
#include<iostream.h>
#include<conio.h>
class student
{
protected:
introll_number;
public:
voidget_number(int a)
{
roll_number=a;
}
voidput_number(void)
{
cout<<"roll no:"<<roll_number<<"\n";
}
};
classtest:public student
{
protected:
float part1,part2;
public:
voidget_mark(float x,float y)
{
part1=x;
part2=y;
}
voidput_mark(void)
{
cout<<"marks obtained:"<<"\n"<<"part1 ="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
voidget_score(float s)
{
score=s;
}
voidput_score(void)
{
cout<<"sports wt:"<<score<<"\n";
}
};
classresult:publictest,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_number();
put_mark();
put_score();
cout<<"total score:"<<total<<"\n";
}
int main()
{
clrscr();
result student_1;
student_1.get_number(12.34);
student_1.get_mark(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
getch();
return 0;
}








OUTPUT:
roll number=12
marks obtained:
part 1=27.5
part 2=33
sports wt:6
total score=66.5

No comments:

Post a Comment

Thanks for Visiting the Blog :-)