0% found this document useful (0 votes)
94 views

String Concatenation Using Dynamic Memory Allocation Concept

The document discusses several C++ and Java programs that demonstrate different programming concepts: 1. The first section shows a C++ program that implements string concatenation using dynamic memory allocation and constructor overloading. 2. The second section shows a C++ program that implements arithmetic operations on complex numbers using constructor overloading. 3. The third section discusses operator overloading in C++ to add and subtract distances between objects. 4. Further sections demonstrate operator overloading for date differences, polynomial addition/subtraction, and more in C++. 5. The final sections provide examples of random number generation, inheritance, and packages in simple Java programs.

Uploaded by

api-3830748
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

String Concatenation Using Dynamic Memory Allocation Concept

The document discusses several C++ and Java programs that demonstrate different programming concepts: 1. The first section shows a C++ program that implements string concatenation using dynamic memory allocation and constructor overloading. 2. The second section shows a C++ program that implements arithmetic operations on complex numbers using constructor overloading. 3. The third section discusses operator overloading in C++ to add and subtract distances between objects. 4. Further sections demonstrate operator overloading for date differences, polynomial addition/subtraction, and more in C++. 5. The final sections provide examples of random number generation, inheritance, and packages in simple Java programs.

Uploaded by

api-3830748
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 32

/*STRING CONCATENATION USING DYNAMIC MEMORY

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);
};

void string::join(string &a,string &b)


{
length=a.length+b.length;
delete name;
name=new char[length];
strcpy(name,a.name);
strcat(name,b.name);
};
int main()
{
clrscr();
char *first=”joseph”;
string name1(first),name2(“louis”),name(“lagrange”),s1,s2;
s1.join(name1,name2);
s2.join(s1,name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
getch();
return 0;
}
OUT PUT:

joseph

louis

lagrange

joseph louis

jospeph louis lagrange


/*IMPLEMENTATION OF ARITHMETIC OPERATION ON
COMPLEX NUMBER USING CONSTRUCTOR
OVERLOADING*/

#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);
};

void date :: date( )


{
int i;
month[2]=28;
days=0;
feb=29;
month[1]=month[3]=month[5]=month[7]=month[8]=month[10]=month[12]=31;
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= =2&&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)
temp.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.days = (month[mm]-dd)+c.dd;
if(mm= =12)
{
temp.mm=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&&yy%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:

Enter the date format as (dd/mm/yyyy) = 03/05/2006

dd/mm/yyyy : 03/05/2005

Enter the date format as (dd/mm/yyyy) = 13/05/2006

dd/mm/yyyy : 13/05/2005

Number of days between two days are : 10


/*IMPLEMENTATION OF ADDITION AND SUBTRACTION OF
TWO POLYNOMIAL OBJECTS USING OPERATOR
OVERLOADING*/

#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:

Addition of polynomial expression

P1 = (2.5)X2 + (3.5)X +5.0

P2 = (1.6)X2 + (2.7)X + 5.0

P3 = (4.1)X2 + (6.2)X + 10.0

Subtraction of polynomial expression

P1 = (2.5)X2 + (3.5)X +5.0

P2 = (1.6)X2 + (2.7)X + 5.0

P3 = (0.9)X2 + (0.8)X + 0.0


/*SIMPLE JAVA PROGRAM*/

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

The First Random Number Is 117

The Second Random Number Is 216

The Third Random Number Is 111

The Fourth Random Number Is 57

The Fifth Random Number Is 87


/*USE OF INTERFACE IN JAVA*/

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

MEDICAL STUDENTS DETAILS

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

Enter the values of length :48

Breath : 30

Area of rectangle : 1824


/*MANAGING BANK ACCOUNT USING INHERITANCS
CONCEPT*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class employee
{

int empno;
char name[20];
public:
void getdata(int,char*)
void put data(void);
};

void employee::get data(int a,char b)


{
empno=a;
strcpy(name,b);
}
void employee::put data(void)
{
cout<<”employee no:”<<empno<<”\n”;
cout<<”Enter the name:”<<name<<”/n”;
}
class details:publicemployee
{
char designation[20];
int phone no;
public:
void getdetails(char*,int);
void putdetails(void)
};
void details::getdetails(char*c,int d);
{
strcpy(designation,c);
phone no=d;
}

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:get sal(void)


{
cout<<”Enter Basic Pay:”;
cin>>basicpay;
cout<<”DA:”;
cin..da;
cout<<”HRA:”;
cin>>hra;
cout<<”PF:”;
cin>>pf;
}

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:

Enter the Basic pay:2000


Da:200.00
HRA:300.00
PF:234.00
Employee no:21
Enter the name:Ram
Desigation:Engg
Phone no:9842315395
Salary:2266.00

You might also like