Sie sind auf Seite 1von 71

# EXPERIMENT 1

1. Write a function using reference variables as arguments to swap the values of pair of integers

Program Code :
#include <iostream>

## using namespace std;

void swap(int&x,int&y)

int z=x;

x=y;

y=z;

int main()

intx,y;

cin>>x>>y;

swap(x,y);

Output :

## ASHISH KUMAR SALVI 0827CS183D02

2. Write an inline function to find largest of three numbers

Program Code :
#include <iostream>

## using namespace std;

inlineint max(inta,intb,int c)

return a>b&&a>c?a:b>a&&b>c?b:c;

int main()

inta,b,c;

cin>>a>>b>>c;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

3. Write a function power() to raise a number m to a power n. The function takes a
double value for m and int value for n and returns the result correctly. Use a default
value of 2 for n to make the function to calculate squares when this argument is
omitted. Write a main that gets the values of m and n from the user to test the
function.

Program Code :
#include <iostream>

## double pow(double m,int n=2)

double p=1.0;

inti;

for(i=1;i<=n;i++)

p*=m;

return p;

int main()

double m;

intn,ch;

## cout<<"\nEnter your choice : ";

cin>>ch;

switch(ch)

{
ASHISH KUMAR SALVI 0827CS183D02
case 1 : cout<<"\nEnter value of m : ";

cin>>m;

break;

cin>>m>>n;

## cout<<"\nPower function when both arguments are used = "<<pow(m,n);

break;

default :cout<<"\nExit";

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Experiment 2
1. Define a class to represent a bank account which include the following members
as-
Data members :
1. Name of the depositor
2. Account Number
3. Withdraw amount
4. Balance amount in the account
Member Functions:
2. To deposit an amount
3. To withdraw an amount after checking the balance
4. To display name and balance.
Write a main program to test the program.

Program Code :
#include <iostream>

#include <string>

## using namespace std;

class Account

string name;

longacno;

stringactype;

doublebal;

public:

Account()

name="nill";

actype="nill";

acno=0;

bal=0.0;
ASHISH KUMAR SALVI 0827CS183D02
}

voidopbal()

cin.ignore();

## cout<<"\nEnter name : ";

getline(cin,name);

cin>>actype;

cin>>acno;

cin>>bal;

void deposit()

floatdepo=0.0;

cin>>depo;

bal+=depo;

void withdraw()

float with=0.0;

cin>>with;

if(bal<with)

## ASHISH KUMAR SALVI 0827CS183D02

cout<<"\n!! Not enough balance !!";

else

bal-=with;

## cout<<"\nBalance after withdraw : "<<bal;

void display()

cout<<"\n\tDETAILS\n"<<"Name : "<<name;

## cout<<"\nAccount No. : "<<acno<<"\nAccountType : "<<actype;

cout<<"\nBalance : "<<bal;

};

int main()

Account obj;

intch;

do

## cout<<"\nEnter your choice : ";

cin>>ch;

switch(ch)

case 1 : obj.opbal();

## ASHISH KUMAR SALVI 0827CS183D02

break;

case 2 : obj.deposit();

break;

case 3 : obj.withdraw();

break;

case 4 : obj.display();

break;

case 5 : cout<<"\nExit";

}while(ch!=5);

Output :

## ASHISH KUMAR SALVI 0827CS183D02

ASHISH KUMAR SALVI 0827CS183D02
2.Write the above program for handling n number of account holders using array of
objects for data initialize and displaying all records.

Program Code :
#include <iostream>

#include <string>

class Account

string name;

longacno;

stringactype;

doublebal;

public:

Account()

name="nill";

actype="nill";

acno=0;

bal=0.0;

voidopbal()

cin.ignore();

## cout<<"\nEnter name : ";

getline(cin,name);

cin>>actype;

cin>>acno;

## cout<<"Enter opening balance : ";

cin>>bal;

void display()

cout<<"\n\tDETAILS\n"<<"Name : "<<name;

## cout<<"\nAccount No. : "<<acno<<"\nAccountType : "<<actype;

cout<<"\nBalance : "<<bal;

};

int main()

{ intn,i;

## cout<<"\nEnter number of records : ";

cin>>n;

Account obj[n];

for(i=0;i<n;i++)

{ cout<<"\nRecord "<<i+1;

obj[i].opbal();

for(i=0;i<n;i++)

{ cout<<"\nRecord "<<i+1;

obj[i].display();

Output :

## ASHISH KUMAR SALVI 0827CS183D02

3. Write a C++ program to compute area of right angle triangle, equilateral

Program Code :
#include <iostream>

#include <cmath>

## using namespace std;

float area(inta,int b)

return (0.5)*a*b;

float area(int a)

returnsqrt(3)/4*a*a;

float area(inta,intb,int c)

float s=(float)(a+b+c)/2;

returnsqrt(s*(s-a)*(s-b)*(s-c));

int main()

intch;

cin>>ch;

switch(ch)

case 1 : intl,h;

cin>>l>>h;

cout<<area(l,h);

break;

case 2 : int s;

cin>>s;

## cout<<"\n\nArea of Equilateral Triangle is : ";

cout<<area(s);

break;

case 3 : inta,b,c;

cin>>a>>b>>c;

## cout<<"\n\nArea of Scalene Triangle is : ";

cout<<area(a,b,c);

break;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Experiment 3
1. Write a C++ program to swap the values two integer members of different
classes using friend function

Program Code :
#include <iostream>

class B;

class A

int a;

public:

A()

cin>>a;

intgetdata()

return a;

};

class B

int b;

public:

B()

cin>>b;

intgetdata()

return b;

};

int temp=x.a;

x.a=y.b;

y.b=temp;

int main()

Aaobj;

B bobj;

swap(aobj,bobj);

Output :

## ASHISH KUMAR SALVI 0827CS183D02

2.Write a C++ program for addition of two complex numbers using friend
function (use constructor function to initialize data members of complex
class)

Program Code :
#include <iostream>

## using namespace std;

class Complex

int real;

intimg;

public:

Complex()

real=0;

img=0;

voidgetdata(intx,int y)

real=x;

img=y;

void show()

cout<<real<<" + i"<<img;

};

## ASHISH KUMAR SALVI 0827CS183D02

void sum(Complex a,Complex b)

Complex c;

c.real=a.real+b.real;

c.img=a.img+b.img;

c.show();

int main()

Complex a,b,c;

intx,y;

cin>>x>>y;

a.getdata(x,y);

## cout<<"\nEnter real and imaginary part of complex number b : ";

cin>>x>>y;

b.getdata(x,y);

cout<<"a = ";

a.show();

cout<<"\nb = ";

b.show();

cout<<"\n\na+b = ";

sum(a,b);

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Experiment 4
1. Define a class string and overload == to compare two strings and + operator for
concatenation two strings

Program Code :
#include<iostream>

#include<string.h>

## using namespace std;

class String

public:

char *s;

intlen;

String()

s=0;

len=0;

String(char *s1)

len=strlen(s1);

s=new char[len+1];

strcpy(s,s1);

void show()

cout<<s;

## ASHISH KUMAR SALVI 0827CS183D02

}

void operator==(String x)

## cout<<"calling to overload opeartor =="<<endl;

if(strcmp(x.s,s)==0)

else

## cout<<"strings are not equal"<<endl;

String operator+(String y)

## cout<<"\ncalling to operator +"<<endl;

String z;

z.len=len+y.len;

z.s=new char[z.len+1];

strcpy(z.s,s);

strcat(z.s,y.s);

return z;

};

int main()

char s1[100],s2[100];

cout<<"P=";

cin>>s1;

cout<<endl;

cout<<"Q=";

cin>>s2;

cout<<endl;

String obj1(s1);

String obj2(s2);

String obj3;

obj1==obj2;

obj3=obj1+obj2;

obj3.show();

return 0;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Program Code :
#include<iostream>

classAbc

public:

int m, n;

Abc()

m=8;

n=9;

void show()

## int operator ++()

m=m+1;

n=n+1;

cout<<m<<"\n"<<n;

};

int main()

Abc o1;

o1.show();

## cout<<"\nCalling to overload operator ++ :\n";

cout<<++o1.m<<endl;

cout<<++o1.n;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Experiment 5
1. Define two classes polar and rectangle to represent points in the polar and rectangle
systems. Use conversion routines to convert from one system to the other

Program Code :
#include<bits/stdc++.h>

## using namespace std;

class Rectangular

double x, y;

public:

Rectangular()

x = 0;

y = 0;

Rectangular(double a, double b)

x = a;

y = b;

doublegetx()

return x;

doublegety()

{

return y;

void input()

cin>> x>>y;

void output()

## cout<< "(" << x << "," << y << ")";

};

class Polar

double theta, r;

public:

Polar()

theta = 0;

r = 0;

Polar(Rectangular rect)

## ASHISH KUMAR SALVI 0827CS183D02

}

operator Rectangular()

double x, y;

## floatatheta = theta * pi / 180;

x = r * cos(atheta);

y = r * sin(atheta);

void input()

cin>> r>>theta;

void output()

};

int main()

intch, n;

do

## ASHISH KUMAR SALVI 0827CS183D02

cout<< "\n\t\t-----------";

## cout<< "\n\t\t CONVERSION";

cout<< "\n\t\t-----------";

cin>>ch;

switch (ch)

case 1:

r1.input();

p1 = r1;

r1.output();

p1.output();

break;

case 2:

p2.input();

r2 = p2;

r2.output();

break;

cin>>n;

}while (n == 1);

Output :

## ASHISH KUMAR SALVI 0827CS183D02

concept.

Program Code :
#include<iostream>

#include<stdio.h>

## using namespace std;

class Matrix

int a[100][100],m,n;

public:

voidtakedata()

cin>>m>>n;

## cout<<"enter values : "<<endl;

for(inti=0;i<m;i++)

for(int j=0;j<n;j++)

## cout<<"enter value ofa["<<i<<"]["<<j<<"]"<<endl;

cin>>a[i][j];

void show()

for(inti=0;i<m;i++)

## ASHISH KUMAR SALVI 0827CS183D02

{

for(int j=0;j<n;j++)

cout<<a[i][j]<<" ";

cout<<"\n";

## Matrix operator+(Matrix &x)

if(x.m==m &&x.n==n)

for(inti=0;i<m;i++)

for(int j=0;j<n;j++)

a[i][j] = x.a[i][j]+a[i][j];

else

exit(0);

};

int main()

Matrix a,b;

a.takedata();

a.show();

b.takedata();

## ASHISH KUMAR SALVI 0827CS183D02

b.show();

a+b;

cout<<"\nSum is :"<<endl;

a.show();

return 0;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Experiment 6:
1. C++ Program to calculate the area and perimeter of rectangles using concept of
inheritance.

Program Code :
#include<iostream>

class Area

public:

return l * b;

};

classPeri

public:

## float peri1(double l,double b)

return 2 * ( l + b );

};

public:

voidgetdata()

cin>>length;

voidcalc()

};

int main()

Rectangle rect;

rect.getdata();

rect.calc();

return 0;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

2. Consider an example of declaring the examination result. Design four classes student,
exam, sports and result. The student has data members such as rollno ,name. Create
the class exam by inheriting the student class. The exam class adds data members
representing the marks scored in 2 subjects and sport class contains sports mark.
Derive the result from exam-class,sports class and it has own data members like total,
avg. Write the interactive program into model this relationship.

Program Code :
#include<iostream>

## using namespace std;

class Student

public:

intrno;

string nm;

voidget_n(intrno,string nm)

this->nm=nm;

this->rno=rno;

voidput_n()

cout<<"Name = "<<nm;

## cout<<"\nRoll number = "<<rno;

};

classTest:public Student

public:

int part1,part2;

## voidget_m(int part1,int part2)

this->part1=part1;

this->part2=part2;

voidput_m()

cout<<"\nPart1 = "<<part1;

cout<<"\nPart2 = "<<part2;

};

## class Sports :public Student

public:

int score;

voidget_s(int score)

this->score=score;

voidput_s()

cout<<"\nScore = "<<score<<endl;

};

## ASHISH KUMAR SALVI 0827CS183D02

public:

int total;

floatavg;

void show()

total = part1+part2+score;

avg = (float)total/3.0;

Test::put_n();

put_m();

put_s();

## cout<<"Total = "<<total<<"\nAverage = "<<avg;

};

int main()

Result res;

res.Test::get_n(12,"CHANDU");

res.get_m(30,35);

res.get_s(7);

res.show();

return 0;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Experiment 7:
1. Create a base class called shape, this class to store two double type values that
could be used to compute the area of figures. Derive two specific classes called
triangle and rectangle from the base shape. Add tp the base class, a member
function getdata() to initialize base class data members and another member
function display_area() to compute and display area of figures.. Make display_area
as a virtual function and redefine the function in the derived class to suit their
requirements.Using these three classes, design a program that will accept dimensions
of a triangle or a rectangle interactively and display area.

Program Code :
#include<iostream>

#include<math.h>

class shape

public:

doublex,y;

voidgetxy()

## cout<<"Enter x and y: ";

cin>>x>>y;

voiddisplay_Area()

};

## ASHISH KUMAR SALVI 0827CS183D02

public:

double z;

voidgetz()

cout<<"Enter z: ";

cin>>z;

voiddisplay_Area()

double s;

s = x+y+z/2;

cout<<sqrt(s*(s-x)*(s-y)*(s-z))<<endl;

};

## class Rectangle : public shape

public:

voiddisplay_Area()

cout<<x*y<<endl;

};

int main()

intch;

area:

cout<<"select 1. Triangle"<<endl;

## ASHISH KUMAR SALVI 0827CS183D02

cout<<"select 2. Rectangle"<<endl;

cin>>ch;

switch(ch)

case 1:

Triangle t;

t.getxy();

t.getz();

## cout<<"Area of Triangle is : ";

t.display_Area();

break;

case 2:

Rectangle obj;

obj.getxy();

## cout<<"Area of Rectangle is : ";

obj.display_Area();

break;

default: exit(0);

goto area;

return 0;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

2.Run the above program with following modification-
i. Make shape class as abstract class with display_area() as pure virtual function.
ii. Use constructor function to initialize the data members of base class not through the
getdata().

Program Code :
#include<iostream>

#include<math.h>

class Shape

public:

doublex,y;

x=x1;

y=y1;

## virtual double display_Area()=0;

};

classTriangle:public Shape

public:

double z;

z=z1;

## ASHISH KUMAR SALVI 0827CS183D02

}

doubledisplay_Area()

double s;

s=(x+y+z)/2;

## cout<<"Area of Triangle is :"<<sqrt(s*(s-x)*(s-y)*(s-z))<<endl;

};

classRectangle:public Shape

public:

## Rectangle(double x1,double y1):Shape(x1,y1)

doubledisplay_Area()

};

int main()

doublech,z,x,y;

do

cout<<"Option ";

cin>>ch;

## ASHISH KUMAR SALVI 0827CS183D02

if(ch==1)

cout<<"Enter x:";

cin>>x;

cout<<"Enter y:";

cin>>y;

cout<<"Enter z:";

cin>>z;

Triangle t(x,y,z);

t.display_Area();

else if(ch==2)

cout<<"Enter x:";

cin>>x;

cout<<"Enter y:";

cin>>y;

Rectangle r(x,y);

r.display_Area();

else

cout<<"Invalid choice";

}while(ch==1||ch==2);

return 0;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Experiment 8:
1. Write an interactive program to compute square root of a number. The input value
must be tested for validity . If it is negative, the user defined function my_sqrt() should
raise an exception.

Program Code :
#include<iostream>

#include<math.h>

intmysqrt(int n)

try

if(n>=0)

## cout<<"Square root is "<<sqrt(n);

else

throw n;

catch(int e)

cout<<"Number is negative";

int main()

int n;

## ASHISH KUMAR SALVI 0827CS183D02

cout<<"Enter the no :"<<endl;

cin>>n;

mysqrt(n);

Output :

## ASHISH KUMAR SALVI 0827CS183D02

2. Write a program in C++ that illustrate the mechanism of validating array element
references.

Program Code :
#include <iostream>

class array

int max;

int a[];

public:

array()

cin>>max;

## cout<<"\nEnter array elements : ";

for(inti=0;i<max;i++)

cin>>a[i];

operator[](inti)

try

if (i<0 || i>=max)

throw i;

else

cout<<a[i];

catch(inti)

};

int main()

arrayar;

inti;

cin>>i;

ar[i];

Output :

## ASHISH KUMAR SALVI 0827CS183D02

Experiment 9:
1. Write a c++ program to find maximum of two data items using function template.

Program Code :
#include <iostream>

## using namespace std;

template<class T>

T Max(T a, T b)

{ if(a>b)

return a;

else

return b;

int main()

## cin>> f1 >> f2;

ASHISH KUMAR SALVI 0827CS183D02
cout<< Max(f1, f2)<<endl;

return 0;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

2. Write a class template to represent a generic vector. Include member functions to
a. To create a vector
b. Sort the elements in ascending order
c. Display the vector

Program Code :
#include <iostream>

## using namespace std;

template<typename Temp>

class Array

public:

int n;

Temp *a;

voidget_data();

voidput_data();

void sort( );

};

template<typename Temp>

void Array<Temp>::get_data()

inti;

## cout<<"\nEnter how many no:"<<endl;

cin>>n;

a=new Temp[n];

cout<<"Enter numbers:"<<endl;

## ASHISH KUMAR SALVI 0827CS183D02

for(i=0;i<n;i++)

cin>>a[i];

template<typename Temp>

void Array<Temp>::put_data()

for(inti=0;i<n;i++)

cout<<a[i]<<" ";

template<typename Temp>

void Array<Temp>::sort()

Temp k;

inti,j;

cout<<"After sorting:"<<endl;

for(i=0;i<n;i++)

for(j=0;j<n;j++)

if (a[i]<a[j])

k=a[i];

a[i]=a[j];

a[j]=k;

int main()

Array<int> a;

Array<float> b;

a.get_data();

a.sort();

a.put_data();

b.get_data();

b.sort();

b.put_data();

return 0;

Output :

Experiment 10:

## 1. Write a c++ program for matrix multiplication with following specifications-

a. Use constructor dynamic memory allocation for matrix
b. Use getdata() function to input values for matrix
c. Use show() to display the matrix
d. Use mul() to multiply two matrices

Program Code :
#include <iostream>

## using namespace std;

class matrix

int **a;

intm,n;

public :

matrix(intr,int c)

{
ASHISH KUMAR SALVI 0827CS183D02
m=r; n=c;

a=new int*[m];

for(inti=0;i<m;i++)

a[i]=new int[n];

voidgetdata()

for(inti=0;i<m;i++)

for(int j=0;j<n;j++)

cin>>a[i][j];

void show()

for(inti=0;i<m;i++)

for(int j=0;j<n;j++)

cout<<a[i][j]<<" ";

cout<<endl;

~matrix()

for(inti=0;i<m;i++)

delete a[i];

delete a;

matrixmul(matrix &y)

## ASHISH KUMAR SALVI 0827CS183D02

{

matrix z(m,y.n);

for(inti=0;i<m;i++)

for(int j=0;j<y.n;j++)

z.a[i][j]=0;

for(int k=0;k<n;k++)

z.a[i][j]+=a[i][k]*y.a[k][j];

return z;

};

int main()

intm,n;

cin>>m>>n;

matrix x(m,n);

cin>>m>>n;

matrix y(m,n);

x.getdata();

## cout<<"\nEnter Matrix Y :\n";

y.getdata();

cout<<"\nMatrix X :\n";

x.show();

## ASHISH KUMAR SALVI 0827CS183D02

cout<<"\nMatrix Y :\n";

y.show();

## cout<<"\nMultiplication of X and Y is : \n";

matrix z=x.mul(y);

z.show();

Output :

## ASHISH KUMAR SALVI 0827CS183D02

2. Modify the above program as follows-
a. Use operator*() for matrix multiplication instead of mul()
b. Make operator*() as friend function

## ASHISH KUMAR SALVI 0827CS183D02

Program Code :
#include <iostream>

## using namespace std;

class matrix

int **a;

intm,n;

public :

matrix()

matrix(intr,int c)

m=r; n=c;

a=new int*[m];

for(inti=0;i<m;i++)

a[i]=new int[n];

voidgetdata()

for(inti=0;i<m;i++)

for(int j=0;j<n;j++)

cin>>a[i][j];

void show()

for(inti=0;i<m;i++)

## ASHISH KUMAR SALVI 0827CS183D02

{

for(int j=0;j<n;j++)

cout<<a[i][j]<<" ";

cout<<endl;

~matrix()

for(inti=0;i<m;i++)

delete a[i];

delete a;

};

## matrix operator*(matrix &x,matrix&y)

matrix z(x.m,y.n);

for(inti=0;i<x.m;i++)

for(int j=0;j<y.n;j++)

z.a[i][j]=0;

for(int k=0;k<y.n;k++)

z.a[i][j]+=x.a[i][k]*y.a[k][j];

return z;

int main()

{

intm,n;

cin>>m>>n;

matrix x(m,n);

cin>>m>>n;

matrix y(m,n);

x.getdata();

## cout<<"\nEnter Matrix Y :\n";

y.getdata();

cout<<"\nMatrix X :\n";

x.show();

cout<<"\nMatrix Y :\n";

y.show();

matrix z = x*y;

z.show();

Output :

## ASHISH KUMAR SALVI 0827CS183D02

3. To perform the write operation within a file.

Program Code :
#include <iostream>

## ASHISH KUMAR SALVI 0827CS183D02

#include <fstream>

int main()

charc,fname[20];

## cout<<"Enter file name :";

cin>>fname;

ofstreamfout;

fout.open(fname);

## cout<<"\nEnter contents to store in file(Enter # at end) :\n";

while((c=getchar())!='#')

fout<<c;

fout.close();

Output :

## 4. Program for read the content of a file.

Program Code :
#include <iostream>

## ASHISH KUMAR SALVI 0827CS183D02

#include <fstream>

int main()

charc,fname[20];

## cout<<"Enter file name :";

cin>>fname;

ifstream fin(fname);

if(!fin)

## cout<<"\n! File does not exist !";

return 0;

else

while(fin.eof()==0)

fin.get(c);

cout<<c;

return 0;

Output :

## ASHISH KUMAR SALVI 0827CS183D02

ASHISH KUMAR SALVI 0827CS183D02