String Concatenation Using Dynamic Memory Allocation Concept
String Concatenation Using Dynamic Memory Allocation Concept
ALLOCATION CONCEPT*/
#include<iostream.h>
#include<string.h>
#include<conio.h>
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(void)
{
cout<<name<<”\n”;
}
void join(string &a,string &b);
};
joseph
louis
lagrange
joseph louis
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex(){}
complex(float a){x=y=a;}
complex(float real,float imag)
{
x=real;y=imag;}
friend complex sum(complex,complex);
friend void show(complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
void show(complex c)
{
cout<<c.x<<”+j”<<c.y<<”\n”;
}
int main()
{
clrscr();
complex A(2.7,3.5);
complex B(1.6);
complex C;
c=sum(A,B);
cout<<”A=”;
show(A);
cout<<”B=”;
show(B);
cout<<”C=”;
show(C);
complex P,Q,R;
P=complex(2.5,3.9);
Q=complex(1.6,2.5);
R=sum(P,Q);
cout<<”P=”;
show(P);
cout<<”Q=”;
show(Q);
cout<<”R=”;
show(R);
getch();
return 0;
}
OUTPUT:
A = 2.7 + J 3.5
B = 1.6 + J 1.6
C = 4.3 + J 5.1
P = 2.5 + J 3.9
Q = 1.6 + J 2.4
R = 4.1 + J 6.4
/*TO READ A VALUE OF DISTANCE FROM ONE OBJECT AND
ADD WITH A VALUE IN ANOTHER OBJECT USING FRIEND
FUNCTION*/
#include<iostream.h>
#include<conio.h>
class AC
class AB
{
void setdist(int a)
{
dist1=a;
}
void display(void)
{
cout<<dist1<<”\n”;
}
friend void adddistance(AB,AC);
};
class AC
{
int dist2;
public:
void setdist(int b)
{
dist2=b;
}
void display(void)
{
cout<<dist2<<”\n”;
};
void adddistance(AB x,AC y)
{
int temp=x.dist1+y.dist2;
cout<<”result”<<temp;
}
int main()
{
AB ab;
AC ac;
clrscr();
ab.setdist(500);
ac.setdist(500);
cout<<”Values of distance”<<”\n”;
ab.display();
ac.display();
cout<<”Total distance”<<”\n”;
adddistance(ab,ac);
return 0;
}
OUTPUT:
Values of distance:
500
500
Total distance:10000
/*IMPLENTATION OF + AND – OPERATOR OVERLOADING */
#include<iostream.h>
#include<conio.h>
class date
{
char date1[10];
int dd,mm,yy,days,month[15],feb;
public:
date( );
void get_date( )
{
int t,t1;
cout<<”\n Enter the date format as(dd/mm/yy):”;
cin>>date1;
dd=((date1[10]-48*10)+(date1[1]-48));
mm=((date1[3]-48*10)+(date1[4]-48));
yy=((date1[6]-48*1000)+(date1[7]-48)*100)+((date1[8]-48)*10)+(date1[9]-48);
cout<<”\n\t dd/mm/yyyy:”;
void display result( )
{
cout<<”\n\t Number of days between two dates are:”<<days;
}
date date::operator-(date);
};
dd/mm/yyyy : 03/05/2005
dd/mm/yyyy : 13/05/2005
#include<iostream.h>
#include<conio.h>
class poly
{
float x,y,z;
public:
poly( ){ }
poly(float x2,float x1,float con);
{
x=x2;
y=x1;
z=con;
}
poly addition(poly,poly);
poly subtraction(poly,poly);
friend ostream & operator<<(ostream &,poly &);
};
poly poly::addition(poly p1,poly p2)
{
poly temp;
temp.x=p1.x+p2.x;
temp.y=p1.y+p2.y;
temp.z=p1.z+p2.z;
return(temp);
}
poly poly::subtraction(poly p1,poly p2)
{
poly temp;
temp.x=p1.x-p2.x;
temp.y=p1.y-p2.y;
temp.z=p1.z-p2.z;
return(temp);
}
ostream & operator<<(ostream & dout,poly & c)
{
cout<<”(“<<c.x<<”x2”<<”+(“<<c.y<<”)x”<<”+(“<<c.z<<”)\n\n”;
return(dout);
}
void main( )
{
clrscr( );
poly c1,c2,c3,temp,result1,result2;
c1=poly(2.5,3.5,5.0);
c2=poly(1.6,2.7,5.0);
result1=temp.addition(c1,c2);
result2=temp.subtraction(c1,c2);
cout<<”\n\n addition of polynomial expression”;
cout<<”\tp1=”<<c1;
cout<<”\tp2=”<<c2;
cout<<”\tp3=”<<result;
cout<<”\n subtraction of poly expression”;
cout<<”\tp1=”<<c1;
cout<<”\tp2=”<<c2;
cout<<”\tp3=”<<result;
getch( );
}
OUTPUT:
import java.io.*;
import java.long.*;
import java.util.*;
class ex1
{
public static void main(string args[])
{
int a,b,c,d,e;
a=(int)(math.round(math.random()*255));
b=(int)(math.round(math.random()*255));
c=(int)(math.round(math.random()*255));
d=(int)(math.round(math.random()*255));
e=(int)(math.round(math.random()*255));
System.out.println(The First Random Number is”+a);
System.out.println(The Second Random Number is”+b);
System.out.println(The Third Random Number is”+c);
System.out.println(The Fourth Random Number is”+d);
System.out.println(The Fifth Random Number is”+e);
}
}
OUTPUT
class rectangle
{
int l,b;
rectangle(int x,int y)
{
l=x;
b=y;
}
int area();
{
return(l*b);
}
}
class triangle extends rectangle
{
int h;
triangle(int x,int y.int z)
{
super(x,y);
h=z;
}
int volume();
{
return(l*b*h);
}
}
class inherit
{
public static void main(string args[])
{
triangle tri=new triangle(10,10,20);
int a1=triangle.area();
int v=tri.volume();
System.out.println(“Area=”+a1);
System.out.println(“Volume=”
OUTPUT:
Area : 100
Volume : 200
/*DEVELOPING PACKINGES IN JAVA*/
import java.io.*;
class student
{
int m1,m2;
void getdata(int a,int b)
{
m1=a;
m2=b;
}
void putdata()
{
System.out.println(“mark1=”+m1);
System.out.println(“mark2=”+m2);
}
}
class arts extends student
{
int rollno,tot;
float avg;
void getdata(int c,int a,int b)
{
m1=a;
m2=b;
rollno=c;
tot=m1+m2;
avg=tot/2;
}
void put()
{
System.out.println(“ARTS STUDENTS DETAILS”);
System.out.println(“roll number=”+rollno);
System.out.println(“total=”+tot);
System.out.println(“average=”+avg);
}
}
class medical extends student
{
int rolno,tot;
float avg;
void getdata(int c,int a,int b)
{
m1=a;
m2=b;
rollno=c;
tot=m1+m2;
avg=tot/2;
}
void put1()
{
System.out.println(“Medical Student Details”);
System.out.println(“Average=”+avg);
}
}
class hirac
{
public static void main{string args[]}
{
arts a1=new arts();
a1.get(111,98,88);
a1.put();
medical m1=new medical();
m1.get1(113,98,99);
m1.put1();
}
}
ARTS STUDENT \S DETAILS
Roll no : 7347346
Total : 375
Average : 75
Roll no : 7347656
Total : 375
Average : 75
/*AREA OF RECTANGLE USING INHERITANCE*/
#include<iostream.h>
#include<conio.h>
int b;
class shape
{
public:
virtual void get()
{
cout<<”\n Enter the value for breath/base:”;
cin>>b;
}
virtual void show()
{
cout<<”\n Breath/Base:”<<b;
}
virtual void area();
{}
};
class rect:public shape
{
public:
int l;
void get();
{
cout<<”\n AREA OF RECTANGLE”;
cout<<”\n Enter the value for length:”;
cin>>l;
}
void area()
{
a1=l*b;
}
void show()
{
cout<<”\n Length:”<<l;
cout<<”\n Breath:”<<b;
cout<<”\n Area of rectangle:”<<a1;
}
};
class tr::public shape
{
int h;
float a2;
void get()
{
cout<<”\n\n Enter the value for height:”;
cin>>h;
}
void area()
{
a2=0.5*(b*h);
}
void show();
{
clrscr();
shape s;
shape *ptr;
rect r;
tri t;
ptr =$s;
ptr->get();
ptr->area();
ptr->show();
ptr->$t;
ptr->get();
ptr->area();
ptr->show();
}
AREA OF RECTANGLE
Breath : 30
int empno;
char name[20];
public:
void getdata(int,char*)
void put data(void);
};
void details::putdetails(void)
{
cout<<”Designation”<<designation<<”\n”;
cout<<”phone no:”<<phone no<<”\n”;
}
class salary::public details
{
int basicpay,pf,hra,da,salary,ded;
public:
void getsal(void);
void display( );
};
void salary::display(void)
{
ded=pf;
salary=(basicpay+hra+da)-ded;
putdata( );
putdetails( );
cout<<”Salary:”<<salary;
}
int main( )
{
clrscr( );
salary s;
s.getdata(21,”ram”);
s.getdetails(“engg”,9842315395)
s.getsal( );
s.display( );
getch( );
return0;
}
OUTPUT: