Upload
tom-sebastian-valamparampil
View
240
Download
0
Embed Size (px)
Citation preview
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 1/24
1
Ex.No: 1
STRING CONCATENATION USING DYNAMIC MEMORY
ALLOCATION
AIM:
To write a C++ program for concatenation of two strings using dynamic memory
allocation
ALOGRITHM:
1. Start the program.
2. Declare the variables e, f and g.
3. Get the values for e and f.
4. Copy the value of e to the variable G using string copy (strcpy) function.
5. Concatenate the two strings of G and F using string concatenation (strcat) function
6. Display the result.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char e[10],f[10],g[20];
cout<<"enter the two strings e and f";
gets(e);
gets(f);
strcpy(g,e);
strcat(g,f);
cout<<"string concatention is"<<g<<"\n";
getch();}
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 2/24
2
OUTPUT:
Enter the two strings e & f:
Electrical
Electronics
The string concatenation is ElectricalElectronics.
RESULT:
Thus the C++ program for concatenation of two strings has been done and output
was verified.
Ex.no:2
IMPLEMENTATION OF ARITHMATIC OPERATION USINGCONSTRUCTOR OVERLOADING
AIM:
To write a c++ program to implement the arithmetic operation on complex numbers
using constructor overloading.
ALGORITHM:
1. Start the program.
2. Declare the variables.
3. Get the values.
4. Declare the functions.
5. Copy the constructor over loading function according to the operation.
6. Call function by its object.
7. Compile & run, display the result.
8. Stop the program.
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 3/24
3
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class complex
{
int real;
float img;
public:
complex()
{
real=0;
}
complex(intx)
{
real=x:
img=0-0;
}
complex(intx,floaty)
{
real=x;img=y;
}
void display(void);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 4/24
4
complex operator/(complex);
};
voidcomplex::display(void)
{
if(img>=0)
cout<<"\t"<<real<<"+i("<<img<<")\n";
else
cout<<"\t"<<real<<"-i("<<img<<")\n";
}
complex complex::operator+(complexc2)
{
complex sum;
sum.real:real+c2.real;
sum.img+c2.img;
return(sum);
}
complex complex::operator-(complexc2)
{
complex sub;
sub real=real-c2.real;
sub.img:img-c2.img;
return (sub);
}
complex complex::operator*(complexc2)
{
complex mul;
mul.real=((real*c2.real)-(img*c2.img));
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 5/24
5
mul-img=((real*c2.img)+(img*c2.real));
return (mul);
}
complex complex::operation/(complexc2)
{
complex div;
float d;
d=[(c2.real*c2.real)+(c2.img*c2.img)];
div.real=[(real*c2.real)+(img*c2.img)]/d;
div.img=[(real*c2.real)-(img*c2.real)]/d;
return(div);
}
void main()
{
clrscr();
complex c1,c;
complex c20(10);
complex c3(15,10.5);
complex c4(5.5);
cout<<"\n c1";c1.display();
cout<<"\n c2";c2.display();
cout<<"\n c3";c3.display();
cout<<"\n c4";c4.display();
cout<<"\n result";
c.display();
c=c1+c2;
cout<<"\n result of addition is \n";
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 6/24
6
cout<<"result";
c.display();
c=c3-c2;
cout<<"\n result of sub is=";
cout<<"result";
c.display();
c=c3*c2;
cout<<"\n result of product is:";
cout<<"result";
c.display();
c=c3/c4;
cout<<\n result of division is=";
cout<<"result";
c.display();
getch ();
}
OUTPUT:
C1 0+1(0)
C2 10+(0)
C3 15+(10.5)
C4 5+1(5)Result 0+1()
Result of addition is: result 10+1(0)
Result of sub is: result of 5+1(10.5)
Result of product is: result 150+ (10.5)
Result of division is: result 2+1(0.45)
RESULT:
Thus the C++ program to implement addition and subtraction operations of
two polynomials and display using << operator overloading has been done and output was
verified.
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 7/24
7
Ex.No:3 PROGRAM USING FRIEND FUNCTION
AIM:To read a value of distance from one object and add with a value in another object
using friend function.
ALGORITHM:
1. Start the program.
2. Declare the variable, class name.
3. Declare the functions.
4. Declare the friend function.
5. Declare the operation of the functions.
6. Call the function by its object.
7. Perform the function operation.
8. Display the result.
9. Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class AC;
class AB
{
float d;
public:
AB()
{
cout<<"Enter the first distance(in feet):";
cin>>d;
}
friend void add(AB,AC);
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 8/24
8
};
class AC
{
float d;
public:
AC()
{
cout<<"Enter the second distance(in inches):";
cin>>d;
}
friend void add(AB a,AC b)
{
float total;
b.d=b.d/12;
total=a.d+b.d;
cout<<"Total distance:"<<total<<"feet";
}
};
void main()
{
clrscr();
AB A;
AC B;
add(A,B);
getch();
}
OUTPUT:
Enter the first distance in feet:10
Enter the first distance in inches:20
Total distance : 11.666667 Feet.
RESULT:Thus the program for addition of distance using friend function was executed and the
output was verified.
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 9/24
9
Ex.No:4
IMPLEMENTATION OF OPERATOR OVERLOADING.
AIM:
To write the c++ program to implement + and – operator overloading and
implementation of addition operation of octal object with integer using operator overloading.
ALGORITHM:
1. Start the program.
2. Declare the variable value.
3. Declare the function.
4. Perform the operation by ‘+’ overloading operator.
5. The octal value & integer value is given.
6. Add the given value.
7. Display the result.
8. Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class octal
{
int x,y,h,k;
public:
octal()
{
h=x;
}
void getdata();
void display();void operator+();
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 10/24
10
};
void octal::getdata()
{
cout<<"\n enter the octal no=";
cin>>oct>>h;
cout<<"\n enter the integer value=";
cin>>k;
}
void octal::display()
{
cout<<"\noctal value="<<h<<endl;
cout<<"\ninteger value="<<k<<endl;
cout<<"\nsum="<<y<<endl;
}
void octal::operator+()
{
y=h+k;
}
void main()
{
clrscr();
octal s;
s.getdata();
+s;
s.display();
getch();
}
OUTPUT:
Enter the octal num = 10.
Enter the integer value = 15.
Octal value = 8.
Integer value = 13.
Sum = 21.
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 11/24
11
RESULT:
Thus the c++ program to implement + and – operator overloading and
implementation of addition operation of octal object with integer using operator overloading
has been done and output was verified.
Ex. No.5
ADDITION AND SUBTRACTION OF TWO POLYNOMIALS
AIM:
To implement addition and subtraction operations of two polynomials and display
using << operator overloading.
ALGORITHM:
1. Create a class called POLYNOMIAL with constructors to create polynomial
objects and to initialize with specific values.
2. Create member functions to perform addition and subtraction of two
Polynomials.
3. Overload operator << to display the results of addition and subtraction
operations on two polynomials.
4. Display the results.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class poly
{
public:
int a,b,c,d;
poly()
{}
poly (int w, int x, int y, int z)
{
a=w;
b=x;
c=y;
d=z;
}
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 12/24
12
friend poly add(poly,poly);
friend poly sub(poly,poly);
friend void operator <<(poly z1,poly z2)
{
cout<<z1.a<<"x^3+"<<z1.b<<"x^2+"<<z1.c<<"x+"<<z1.d;
}
};
poly add(poly q1, poly q2)
{
poly r;
r.a=q1.a+q2.a;
r.b=q1.b+q2.b;
r.c=q1.c+q2.c;
r.d=q1.d+q2.d;
return r;
}
poly sub (poly q1, poly q2)
{
poly s;
s.a=q1.a+q2.a;
s.b=q1.b+q2.b;
s.c=q1.c+q2.c;
s.d=q1.d+q2.d;
return s;
}
void main ()
{
poly p3,p1;
poly p2(1,1,1,1);
int a,b,c,d;
clrscr ();
cout<<"\nFIRST POLYNOMINAL: ";
p2<<p2;
cout<<"\nEnter the co-efficient of cubic polynomial:";
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 13/24
13
cout<<"\nx^3";
cin>>p1.a;
cout<<"\nx^2";
cin>>p1.b;
cout<<"x";
cin>>p1.c;
cout<<"constant";
cin>>p1.d;
cout<<"\n\nsum\n";
p3=add(p1,p2);
p3<<p3;
cout<<"\nDifference \n";
p3=sub(p1,p3);
p3<<p3;
getch();
}
OUTPUT:
FIRST POLYNOMINAL: 1x^3+1x^2+1x+1
Enter the co-efficient of cubic polynomial:
x^3 8
x^2 9
x 8
constant 6
sum
9x^3+10x^2+9x+7
Difference
17x^3+19x^2+17x+13
RESULT:
Thus the C++ program to implement addition and subtraction operations of
two polynomials and display using << operator overloading has been done and output was
verified.
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 14/24
14
Ex. No.6
BANK ACCOUNT MAINTENANCE
AIM:
To manage the account information of the customer using inheritance concept.
ALGORITHM:
1. Create a class with the following member variables. Customer name, account
number and account type.
2. Create the derived classes with following member variables.
for current account information
Balance, Deposit and withdrawal amount
for savings account information
Balance and Deposit amount
3. Write a member function to get the Deposit and withdrawal amount and to
update the balance information for current account.
4. Write a member function to get the Deposit amount and to update the balance
information for saving account.
Write a member function to Display the balance information for respective
account type
PROGRAM:
#include<iostream.h>
#include<conio.h>
class bank
{
public:
char*cname;
long int acno;
void display()
{
cout<<cname;
cout<<acno;
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 15/24
15
}
};
class savings : public bank
{
public:
int wdraw, dep, bal;
void saves()
{
cout<<"Enter the amount to be deposited=Rs.";
cin>>dep;
bal+=dep;
cout<<"Enter the amount to be withdrawn=Rs.";
cin>>wdraw;
bal-=wdraw;
cout<<"Your A/c Balance is=Rs."<<bal<<endl;
}
};
class current : public savings
{
public:
void info()
{
cout<<"Last amount witdrawn=Rs."<<wdraw<<endl;
cout<<"Last amount deposited=Rs."<<dep<<endl;
cout<<"Your A/c Balance is=Rs."<<bal<<endl;
}
};
void main()
{
int ch;
current ac;
clrscr();
cout<<"Enter customer name:";
cin>>ac.cname;
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 16/24
16
cout<<endl<<"Enter your A/c no.:";
cin>>ac.acno;
cout<<endl;
while(1)
{
cout<<"Enter the A/c type,1.Savings 2.Current 3.Exit\n";
cin>>ch;
switch(ch)
{
case 1:
ac.saves();
break;
case 2:
ac.info();
break;
case 3:
break;
default:
cout<<"Invalid Choice";
break;
}
if (ch==3)
break;
}
getch();
}
OUTPUT:
Enter customer name: meera
Enter your A/c no.:101
Enter the A/c type: 1.Savings 2.Current 3.Exit
1
Enter the amount to be deposited=Rs.5000
Enter the amount to be withdrawn=Rs.500
Your A/c Balance is=Rs.4500
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 17/24
17
Enter the A/c type: 1.Savings 2.Current 3.Exit
2
Last amount withdrawn=Rs.500
Last amount deposited=Rs.5000
Your A/c Balance is=Rs.4500
Enter the A/c type: 1.Savings 2.Current 3.Exit
3
RESULT:
Thus the C++ program to manage the account information of the customer
using inheritance concept has been done and output was verified.
Ex. No.7
AREA OF TRIANGLE AND RECTANGLE
AIM:
To calculate the area of triangle and rectangle using derived classes and display the
result using virtual function
ALGORITHM:
1. Create a base class SHAPE.
2. Derive two sub classes TRIANGLE and RECTANGLE from the base class
SHAPE.
3. Define member functions get_data() and display_area().
4. Find out the area of triangle and rectangle and display the result using
display_area().
5. Make display_area() as a virtual function
PROGRAM:
#include<iostream.h>
#include<conio.h>
class shape
{
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 18/24
18
public:
virtual void getdata()
{ }
virtual void display()
{ }
};
class triangle:public shape
{
int h,bs;
float at;
public:
void getdata()
{
cout<<"Enter the height and base of triangle = ";
cin>>h>>bs;
}
areatri()
{
at=((bs*h)/2.0);
return (at);
}
void display()
{
cout<<"Area of Triangle = "<<at<<" Sq.Units"<<endl;
}
};
class rectangle:public shape
{
int l,b,ar;
public:
void getdata()
{
cout<<"Enter the length and breadth of rectangle = ";
cin>>l>>b;
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 19/24
19
}
arearec()
{
ar=(l*b);
return(ar);
}
void display()
{
cout<<"Area of Rectangle = "<<ar<<" Sq.Units"<<endl;
}
};
void main()
{
clrscr();
shape s;
triangle t;
rectangle r;
shape *bptr;
cout<<"For Triangle"<<endl;
bptr=&t;
bptr->getdata();
t.areatri();
bptr->display();
cout<<"For Rectangle"<<endl;
bptr=&r;
bptr->getdata();
r.arearec();
bptr->display();
getch();
}
OUTPUT:
For Triangle
Enter the height and base of triangle = 4 6
Area of Triangle = 12 Sq.Units
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 20/24
20
For Rectangle
Enter the length and breadth of rectangle = 7 8
Area of Rectangle = 56 Sq.Units
RESULT:
Thus the C++ program to calculate the area of triangle and rectangle using
derived classes and display the result using virtual function has been done and output was
verified.
Ex. No.8
RANDOM NUMBERS
AIM:
To write a simple java program to generate random numbers
ALGORITHM:
1. Start the process
2. Create a class called rand with variable declaration and includes header file math.
3. Generate random numbers using built in function random ().
4. Display the result.
5. Start the process
PROGRAM:
class rand
{
public static void main(String args[])
{
double ra;
int I ;
System.out.println("Random Numbers are:");
for (i= 1;i<=10;i++)
{
ra=Math.random();
System.out.println("\t"+ra);
}
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 21/24
21
}
}
COMPILATION AND EXECUTION:
D:\javaprg>javac rand.java
D:\javaprg>java rand
OUTPUT:
Random Numbers are:
0.7178800297173848
0.10272303466350396
0.09880382903038853
0.20521914937736818
0.4946446248062951
0.7236876729590774
0.06362232446845784
0.3103631103262836
0.986773064331572
RESULT:
Thus the simple java program to generate random numbers has been done and output
was verified.
Ex. No.9
AREA OF RECTANGLE AND CIRCLE
AIM:
To write a java program to calculate area of rectangle and circle using interfaces.
ALGORITHM:
1. Start the process
2. Create a class with fields and methods and kept it as base class for extension.
3. Create a interface with method prototype.
4. Now extend the previously created class and the interface by implementing
through which multiple inheritance is achieved.
5. Stop the process
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 22/24
22
PROGRAM:
interface Area
{
final static float pi=3.14F;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
class InterfaceTest
{
public static void main(String args[])
{
Rectangle rect=new Rectangle ();
Circle cir=new Circle ();
Area ar;
ar=rect;
System.out.println("Area of Rectangle="+ar.compute(10,20));
ar=cir;
System.out.println("Area of Circle="+ar.compute(10,0));
}
}
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 23/24
23
COMPILATION AND EXECUTION:
D:\javaprg>javac InterfaceTest.java
D:\javaprg>java InterfaceTest
OUTPUT:
Area of Rectangle=200.0
Area of Circle=314.0
RESULT:
Thus the java program for implementing interface has been done and output was
verified.
Ex.No:10 IMPLEMENTATION OF PACKAGES
AIM:
To write a java program to implement the packages utilities.
ALGORITHM:
1. Start the process
2. Import the packages created in the subdirectories.
3. Initiate the class in the packages.
4. Call the methods of packages.
5. Stop the process.
PROGRAM:
SUB PROGRAM1:
package package1;
public class classA
{
public void displayA()
{
System.out.println("classA");
}
}
8/3/2019 Oops Record
http://slidepdf.com/reader/full/oops-record 24/24
SUB PROGRAM2:
package package2;
public class classB
{
protected:
int m=0;
public void displayB()
{
System.out.println("class B");
System.out.println("m="m);
}}
MAIN PROGRAM:
import package1.classA;
import package2.*;
class packagetest2;
{
public static void main(String args[])
{
classA A=new classA();
classB B=new classB();
A.display A();
B.display B();
}
}
OUTPUT:
class A
class A
m=10
RESULT:
Thus the program for creating and accessing package was executed and output was
verified.