0% found this document useful (0 votes)
74 views29 pages

String Concatenation Using Dynamic Memory Allocation Concept

The document contains code for multiple C++ programs that demonstrate different object-oriented programming concepts like classes, objects, constructors, operator overloading, inheritance etc. The programs include string concatenation using dynamic memory allocation, arithmetic operations on complex numbers, adding distances between objects using friend functions, calculating days between dates, polynomial addition and subtraction, and displaying student details.

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)
74 views29 pages

String Concatenation Using Dynamic Memory Allocation Concept

The document contains code for multiple C++ programs that demonstrate different object-oriented programming concepts like classes, objects, constructors, operator overloading, inheritance etc. The programs include string concatenation using dynamic memory allocation, arithmetic operations on complex numbers, adding distances between objects using friend functions, calculating days between dates, polynomial addition and subtraction, and displaying student details.

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/ 29

/*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
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:

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


#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.x<<”)\n\n”;
return(cout);
}
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 +2.5

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

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

Subtraction of polynomial expression

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

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

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


#include<iostream.h>
#include<conio.h>
class student
{
int rollno;
int m1,m2,m3,tot,avg;
char name[30],d[10];
public:
void getdata(void);
void calc(void);
void display(void);
};
void student::getdata(void)
cout<<”***STUDENT DETAILS***”<<”\n”;
cout<<”ENTER ROLL NUMBER:”;
cin>>rollno;
cout<<”ENTER NAME:”;
cin>>name;
cout<<”DEPARMENT:”;
cin>>d;
cout<<”ENTER THE FIRST MARK:”;
cin>>m1;
cout<<”ENTER THE SECIOND MARK:”;
cin>>m2;
cout<<”ENTER THE THIRD MARK:”;
cin>>m3;
}
void student::calc(void)
{
tot=m1+m2+m3;
avg=tot/3;
}
void student::display(void)
{
cout<<”***STUDENT MARK SHEET***”<<”\n”;
cout<<”ROLL NUMBER:”<<rollno<<”\n”;
cout<<”NAME:”<<name<<”\n”;
cout<<”ENTER NAME:”
cout<<”DEPARTMENT:”<<d<<”\n”;
cout<<”MARK1:”<<m1<<”\n”;
cout<<”MARK2:”<<m2<<”\n”;
cout<<”MARK3:”<<m3<<”\n”;
cout<<”TOTAL:”<<tot<<”\n”;
cout<<”AVERAGE:”<<avg<<”\n”;
cout<<”*******************”;
}
int main()
{
clrscr();
student s;
s.getdata();
s.calc();
s.display();
getch();
return 0;
}
OUTPUT:

***STUDENT DETAIL***

ROLL NUMBER: 40604105001

ENTER THE NAME: KUMAR

DEPARTMENT: ELECTRICAL AND ELECTRONICS

ENTER THE FIRST MARK: 90

ENTER THE SECOND MARK: 75

ENTER THE THIRD MARK: 75

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

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 193


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=”+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

MEDICAL STUDENTS DETAILS

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

Enter the values of length :48

Breath : 30

Area of rectangle : 1824

You might also like