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
Ex 4
#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*10)+(date1[7]-48)*100)+((date1[8]-48)*10)+(date1[9]-48);
cout<<”\n\t dd/mm/yy:”;
void display result()
{
cout<<”\n\t Number of days between two dates are:”<<days;
}
date date::operator-(date);
};
month[2]=28;
days=0;
feb=29;
month[1]=month[3]=month[5]=month[7]=month[8]=month[10]=month[12]=3
1;
month[4]=month[6]=month[9]=month[11]=30;
}
date date::operator-(date c)
{
date temp;
if(yy==c.yy)
{
if(mm=c.mm)
{
temp.days=c.dd-dd;
return(temp);
}
else
{
if(mm==z&y%4==0)
temp.days=(29-dd)+c.dd;
else
temp.days=(month[mm]-dd)+c.dd;
mm++;
while(mm<c.mm)
{
if(mm==2&&yy%4==0)
emp.days=temp.days+29;
else
temp.days=temp.days+month[mm];
mm++;
}
return(temp);
}
}
if(mm==2&&%4==0)
temp.days=(29-dd)+c.dd;
else
temp.mm=1;
y++;
}
else
{
temp.m=mm+1;
while(temp.mm<=12)
{
if(temp.mm==2&&yy%4==0)
temp.days=temp.days+29;
else
temp.days=temp.days+month[temp.mm];
temp.mm=temp.mm+1;
}
yy++;
if(yy=c.yy)
return(temp);
}
temp.mm=1;
while(yy<c.yy)
{
if(yy%4==0)
temp.days=temp.days+366;
else
temp.days=temp.days+365;
yy++;
}
if(yy=c.yy)
{
while(temp.mm<c.mm)
{
if(temp.mm==2&&%4==0)
temp.days=temp.days+29;
else
temp.days=temp.days+month[temp.mm];
temp.mm++;
}
}
return(temp);
}
void main()
{
int t;
date d1,d2,d3;
clrscr();
d1.getdata();
d2.getdata();
d3=d1-d2;
d3.displayresult();
getch();
}
OUTPUT:
dd/mm/yyyy : 03/05/2005
dd/mm/yyyy : 13/05/2005
***STUDENT DETAIL***
TOTAL : 240
AVERAGE : 80
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
{
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=”+v);
}
}
OUTPUT:
Area : 100
Volume : 200
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
#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