of 51
8/4/2019 progams(1)
1/51
PROGRAM:
#include
#include
class arith
{
int x,y;
public:
arith()
{ }
arith(int a,int b)
{
x=a;
y=b;
}
arith operator+(arith);
arith operator-(arith);
arith operator*(arith);
arith operator/(arith);
void display();
}
arith arith::operator+(arith s)
{
arith t;
t.x=x+s.x;
t.y=y+s.y;
8/4/2019 progams(1)
2/51
return(t);
}
arith arith::operator-(arith s)
{
arith t;
t.x=x-s.x;
t.y=y-s.y;
return(t);
}
arith arith::operator*(arith s)
{
arith t;
t.x=x*s.x;
t.y=y*s.y;
return(t);
}
arith arith::operator/(arith s)
{
arith t;
t.x=x/s.x;
t.y=y/s.y;
return(t);
}
void arith::display()
{
8/4/2019 progams(1)
3/51
cout
8/4/2019 progams(1)
4/51
OUTPUT:
Binary operator overloading using member function
The addition:11 14
The subtraction :-1 0
The multiplication:30 49
The division:0 1
8/4/2019 progams(1)
5/51
PROGRAM:
#include
#include
class arith
{
int x,y;
public:
arith()
{ }
arith(int a,int b)
{
x=a;
y=b;
}
friend arith operator+(arith,arith);
friend arith operator-(arith,arith);
friend arith operator*(arith,arith);
friend arith operator/(arith,arith);
void display();
};
arith operator+(arith s1,arith s2)
{
arith t;
8/4/2019 progams(1)
6/51
t.x=s1.x+s2.x;
t.y=s1.y+s2.y;
return(t);
}
arith operator-(arith s1,arith s2)
{
arith t;
t.x=s1.x-s2.x;
t.y=s1.y-s2.y;
return(t);
}
arith operator*(arith s1,arith s2)
{
arith t;
t.x=s1.x*s2.x;
t.y=s1.y*s2.y;
return(t);
}
arith operator/(arith s1,arith s2)
{
arith t;
t.x=s1.x/s2.x;
t.y=s1.y/s2.y;
return(t);
}
8/4/2019 progams(1)
7/51
void arith::display()
{
cout
8/4/2019 progams(1)
8/51
OUTPUT:
Binary operator overloading using friend function
The addition:11 14
The subtraction :-1 0
The multiplication:30 49
The division:0 1
8/4/2019 progams(1)
9/51
PROGRAM:
#include
#include
class ABC;
class XYZ
{
int x;
public:
void setvalue(int i)
{
x=i;
}
friend void max(XYZ,ABC);
};
class ABC
{
int m;
public:void setvalue(int i)
{
m=i;
}
friend void max(XYZ,ABC);
};
void max(XYZ a,ABC b)
{
8/4/2019 progams(1)
10/51
if(a.x>b.m)
cout
8/4/2019 progams(1)
11/51
OUTPUT:
Friend function
20
8/4/2019 progams(1)
12/51
PROGRAM:
#include
#include
class space
{
int x;
int y;
int z;
public:
void getdata(int ,int ,int );
void display();
void operator-();
};
void space::getdata()
{
couty>>z;
}
void space::display()
{
cout
8/4/2019 progams(1)
13/51
{
x= -x;
y= -y;
z= -z;
cout
8/4/2019 progams(1)
14/51
OUTPUT:
Unary operator overloading using member function
Enter the numbers:1 2 5
1 2 5
The output:-1 -2 -5
8/4/2019 progams(1)
15/51
PROGRAM:
#include
#include
class space
{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c);
void display();
friend void operator-(space &s);
};
void space::getdata()
{
coutb>>c;
}
void space::display()
{
cout
8/4/2019 progams(1)
16/51
s.x= -s.x;
s.y= -s.y;
s.z= -s.z;
cout
8/4/2019 progams(1)
17/51
OUTPUT:
Unary operator overloading using friend function
Enter the numbers:1 2 5
1 2 5
The output:-1 -2 -5
8/4/2019 progams(1)
18/51
PROGRAM:
#include
#include
class code
{
int id;
public:
code(){ }
code(int a){id=a;}
code(code &x)
{
id=x.id;
}
Void display(void)
{
Code
8/4/2019 progams(1)
19/51
code C=A;
code D;
D=A;
cout
8/4/2019 progams(1)
20/51
OUTPUT:
COPY CONSTRUCTOR
id of A:100
id of B:100
id of C:100
id of D:100
8/4/2019 progams(1)
21/51
PROGRAM:
#include
#include
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout
8/4/2019 progams(1)
22/51
cout
8/4/2019 progams(1)
23/51
OUTPUT:
DESTRUCTOR
No of objects created 1
No of objects created 2
No of objects created 3
No of objects created 4
Enter block1
No of objects created 5
No of objects destroyted 5
Enter block2
No of objects created 6
No of objects destroyed 6
Re-enter main
No of objects destroyed 1
No of objects destroyed 2
No of objects destroyed 3
No of objects destroyed 4
8/4/2019 progams(1)
24/51
Program:
#include
#include
#include
class string
{
char *name;
int length;
public:
string()
{
length=0;
name=new char[length+1];
}
string(char *s)
{
length=strlen(s);
name=new char[length +1];
strcpy(name,s);
}
Void display()
{
cout
8/4/2019 progams(1)
25/51
};
Void string::join(string &a,string&b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
};
int main()
{
char *first=Joseph;
clrscr();
cout
8/4/2019 progams(1)
26/51
OUTPUT:
DYNAMIC MEMORY ALLOCATION
Joseph
Louis
Lagrange
Joseph Louis
Joseph Louis Lagrange
8/4/2019 progams(1)
27/51
PROGRAM:
#include
#include
void main()
{
int n;
clrscr();
cout
8/4/2019 progams(1)
28/51
OUTPUT:
EVEN OR ODD:
enter n 5
the number is odd
enter n 6
the number is even
8/4/2019 progams(1)
29/51
PROGRAM:
#include
#include
void main()
{
int a,b,s;
float d;
clrscr();
coutb;
s=a+b;
d=s/float(2);
cout
8/4/2019 progams(1)
30/51
OUTPUT:
SUM AND AVERAGE
enter the 2 numbers 3 5
8 4
8/4/2019 progams(1)
31/51
PROGRAM:
#include
#include
void main()
{
int n;
long int fact(int);
clrscr();
cout
8/4/2019 progams(1)
32/51
OUTPUT:
FACTORIAL OF TWO NUMBERS
enter n: 6
720
8/4/2019 progams(1)
33/51
PROGRAM:
#include
#include
void main()
{
int f1=-1,f2=1,f3=0,n,i;
clrscr();
cout
8/4/2019 progams(1)
34/51
OUTPUT:
FIBONACCI SERIES:
enter the value of n: 8
fibonacci series is 0
1
1
2
3
5
8
13
21
8/4/2019 progams(1)
35/51
PROGRAM:
#include
#include
void main()
{
int a,b,sum,sub,mul,div;
clrscr();
coutb;
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
cout
8/4/2019 progams(1)
36/51
OUTPUT:
ARITHMETIC OPERATORS
enter the two numbers 5 5
sum=10
sub=0
mul=25
div=1
8/4/2019 progams(1)
37/51
PROGRAM:
#include
#include
int main()
{
float value(float p,int n,float r=0.15);
void printline(char ch='*',int len=40);
float amt;
printline();
cout
8/4/2019 progams(1)
38/51
year=year+1;
}
return(sum);
}
void printline(char ch,int len)
{
for(i=1;i
8/4/2019 progams(1)
39/51
OUTPUT:
DEFAULT ARGUMENTS
****************************************
10056.786133
****************************************
8/4/2019 progams(1)
40/51
PROGRAM:
#include
#include
int volume(int);
double volume(double int);
long volume(long,int,int);
void main()
{
clrscr();
cout
8/4/2019 progams(1)
41/51
double volume(double r,int h)
{
return(3.14519*r*r*h);
}
long volume(long l,int b,int h)
{
return (l*b*h);
}
8/4/2019 progams(1)
42/51
OUTPUT:
FUNCTION OVERLOADING
The volume of cube is:1000
The volume of cylinder is:157.2595
The volume of rectangular box is:112500
8/4/2019 progams(1)
43/51
PROGRAM:
#include
#include
namespace N
{
class M
{
int c;
public:M(int a)
{
c=a;
}
void display()
{
cout
8/4/2019 progams(1)
44/51
using namespace N;
N::M m2(20);
m2.display();
return 0;
}
8/4/2019 progams(1)
45/51
OUTPUT:
USING CLASSES IN NAMESPACES
m=10
m=20
8/4/2019 progams(1)
46/51
PROGRAM:
#include
#include
namespace functions
{
int divide(int x,int y)
{
return(x/y);
}
int prod(int x,int y);
}
int functions::prod(int x,int y)
{
return(x*y);
}
int main()
{
using namespace functions;
cout
8/4/2019 progams(1)
47/51
OUTPUT:
USING FUNCTIONS IN NAMESPACES
division=2
multiplication=200
8/4/2019 progams(1)
48/51
PROGRAM:
#include
#include
class book
{
int bookno;
char bookname[30];
char autname[15];
float bookprice;
clrscr();
public:
void getdata()
{
coutbookno;
coutbookname;
coutautname;
coutbookprice;
}
void display();
};
8/4/2019 progams(1)
49/51
void book::display()
{
cout
8/4/2019 progams(1)
50/51
return 0;}
OUTPUT:
OBJECT CREATION
Enter no of books
2
Book1 enter book id:236
Enter book name:2states
Ether author name:chetan
Enter book price:254
Book2 enter book id:259
Enter book name:fivepointsomeone
Ether author name:chetan
Enter book price:269
Book1bookid
236
Book name
2states
Authorname
Chetan
Book price
254
Book2bookid
254
Book name
8/4/2019 progams(1)
51/51
fivepointsomeone
Authorname
Chetan
Book price
269