Sie sind auf Seite 1von 35

Ex No.

1
Generating the electricity bill using Default Arguments

Aim:
An electricity board charges the following rates to domestic users to discourage large
conceptions of energy.
100 units Rs 1.50 p/unit
200 units Rs 1.80 p/unit
Beyond 200 Rs 2.50 p/unit
All users are charged a minimum of Rs 50/-. If the total amount is more than 300 then an
additional surcharge of 15% is added. Write a c++ program to read the names of users,
number of units consumed and print out the charge using default.

Algorithm

Step 1: Initialise the total function with unit=83&&sur=0.15.

Step 2: Read the number of consumers.

Step 3: Read the name of the units of the entire customer.

Step 4: Call the function total(unit).

Step 5: if the unit is less than or equal to 100.

Step 6: Calculate the amount as (unit * 1.50).

Step 7: else if the unit is greater than 100 and if the unit is less than 200.

Step 8: Calculate the amount as 150+((units-100)*1.80).


Step 9: else if the unit is greater than 200.

Step 10: Calculate the amount as 330+((units-200)*2.50)

Step 11: if the calculated amount is greater than Rs 300/-

Step 12: calculate amount as amount * sur.

Step 13: if the unit is less than 83.

Step 14: display the amount as 50.

Step 15: else display the calculated amount.

2.A) CLASS WITH STATIC DATA MEMBER


Aim
To implement static data member in class.

Objective
Static data member of class is shared by all the instances of the
class.

Exercises
1. Create class ITEM with static data member as count.
2. Create a member function to increment the count.
3. Declare the static datamember using scope resolution operator.
4. Display the count value.
2.B) CLASS WITH STATIC MEMBER FUNCTION
Aim
To implement static member function in class.

Objective
Static member function of class is shared by all the instances of
the class.A static member functions cannot access auto members
of a class.
Exercises
1. Create class TEST with static member function.
2. Invoke the static member function using classname with scope
resolution operator and function name.
3. Display the value

Ex.No: 3

Implement complex number class with necessary operator overloadings.


Aim
To implement complex number class with necessary operator
overloadings.

Objective
Use the template to perform operator overloaded with complex class.

Exercises
1. Create the class name as COMPLEX.
2. Declare the data member and member function.
3. Declare the template for handling various operator overloaded
operation.
4. Display the result.
Ex.No : 4

Implement class with dynamic memory allocation.

Aim
To implement class with dynamic memory allocation using
constructor,destructor and copy constructor.

Objective
Using the classes with dynamic memory alloction.

Exercises
1. Create the class name as MATRIX.
2. Declare the data member and member function.
3. Declare constructor,destructor and copy constructor
4. Display the result.

Ex.No : 5

Overload the new and delete operators to provide custom dynamic


allocation of memory.
Aim
To implement class with dynamic memory allocation using new and
delete operator to overload.
Objective
New and delete operator is overloaded along with dynamic
memory allocation.
Exercises
1. Create the class.
2. Declare the new and delete operator.
3. Perform the overload operation
4. Display the result.

Ex.No : 6

Develope a template of standard sorting algorithms such as bubble sort.

Aim
To implement bubble sort.

Objective
Sort the element using bubble sort technique.
Exercises
1. Create the template to handle for bubble sort.
2. Get the element using for loop.
3. Call the template function for sorting.
4. Display the result.
Ex.No 7:

Develope a template of linked list calss and its methods

Aim
To implement the linked list.

Objective
Linked list to perform insert,delete and display the data.
Exercises
1. Create the class with LIST.
2. Create a member function for constructor and to insert the element
in the linked list.
3. Use the pointer to refer the next list.
4. Display the list value.

Ex.No :8

Design stack with neccessary exception handling.


Aim
To implement stack using c++.

Objective
To perform LIFO operations.
Exercises
1. Create the class for stack .
2. Declare the member function for push and pop operation.
3. Push operation to insert the element.
4. Pop operation to delete the element.
5. Provide necessary exception handling for stack
6. Display the result.
Ex:No:9
Implementation of Graph
Aim:
To implement the graph and to obtain a minimum cost spanning tree.
Objective
To know about the concept of Graph along with minimum spanning tree.
Exercise:
1.Create two classes as POINT and ARC.
2.Define a Graph class which represents the collection of Point and Arc objects.
3.Write a method to find a minimum cost spanning tree in a graph.

Ex:no:10
Develop with suitable Hierarchy classes and to implement dynamic polymorphism.

Aim:
To implement Hierarchy classes with Dynamic polymorphism.
Objective
Use Virtual concept along with RTTI

Exercise:
1.Create a class MEDIA with datamember and one member function as Virtual.
2.Create a derived class BOOK with MEDIA as base class.
3.Create a derived class TAPE with MEDIA as base class.
4. Create a Object in the main function.
5. Invoke the appropriate function using object.
6.Finally display the result.
Ex.No: 11
Write a c++ program that randomly generates complex numbers.
Aim:
To implement the program that randomly generates complex numbers .
and write them two per line in a file along with an operator.The numbers are
written to file in the format(a+ib).Write another program to read one line at atime from
this file,perform the corresponding operation on the two complex numbers read and write
the result to another file.
Exercise:
1. Create a two file.
2. One file to read and another file to write.
3. Do the operation in one file and write the result in another file.
4. Display the result.
Ex.No : 1

#include<iostream.h>
#include<conio.h>
class de
{
private:
//int units;
int charges;
char name[10];
public:
/*int read()
{
//cin>>name;
cin>>units;

}*/
void unit1(int units)
{
charges=.5*units;
cout<<charges;

}
void unit2(int units)
{
charges=100+.65*(units-200);
cout<<charges;
}
void unit3(int units)
{
charges=230+.8*(units-400);
cout<<charges;
}
void unit(int units=700,float charge=100);
};
void de::unit(int u,float c)
{
charges=390+c*(u-600);
cout<<charges;
}
int main()
{
de d;
int units;
cin>>units;
if(units<=200)
d.unit1(units);
else if (units>=500&&units<600)
d.unit();
getch();
return 0;
}

2.a) CLASS WITH STATIC DATA MEMBER


Program:
#include<iostream.h>
class item
{
static int count;
int num;
public:
void getdata(int a)
{
num=a;
count++;
cout<<”Number”<<num;
}
void showcount()
{
cout<<”count”;
cout<<count<<”\n”;
}
};
int item::count;
int main()
{
item a,b,c;
a.showcount();
b.showcount();
c.showcount();
a.getdata(20);
b.getdata(30);
c.getdata(40);
a.showcount();
b.showcount();
c.showcount();
}
OUTPUT:
count 0
count 0
count 0
Number 20
Number 30
Number 40
count 3
count 3
count 3
2.b) CLASS WITH STATIC MEMBER FUNCTION

Program:
#include<iostream.h>
class test
{
int code;
static int count;
public:
void setcode()
{
cout<<”Object Number:”<<code<<”\n”;
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcount();
t2.setcount();
test::showcount();
test t3;
t1.showcode();
t2.showcode();
t3.showcode();
return(0);
}

OUTPUT:

count 2
count 3
Object Number 1
Object Number 2
Object Number 3

Ex:no:3
#include<iostream.h>
template<class T>
class complex
{
private:
T real;
T imag;
Public:
complex()
{
real=imag=0;
}
void getdata()
{
cout<<”real part?”;
cin>>real;
cout<<”imag part”;
cin>>imag;
}
complex operator +(complex c2);
void outdata(char *msg)
{
cout<<msg<<”(“<<real;
cout<<”,”<<img<<”)”<<endl;
}
};
Template<class T>
complex<T> complex<T>::operator +(complex<T>c2)
{
Complex<T>temp;
temp.real=real+c2.real;
temp.img=imag+c2.imag;
return(temp);
}
void main()
{
complex<int>c1,c2,c3;
cout<<”Addition of integercomplexobjects….”<<endl;
cout<<”Enter complex number c1..”<<endl;
c1.getdata();
cout<<”Enterthecomplexnumberc2”<<endl;
c2.getdata();
c3=c1+c2;
c3.outdata(“c3=c1+c2:”);
complex<float>c4,c5,c6;
cout<<”Additionof float complexobjects….”<<endl;
cout<<”Enter complex number c4..”<<endl;
c4.getdata();
cout<<”Enterthecomplexnumberc5”<<endl;
c5.getdata();
c6=c4+c5;
c6.outdata(“c6=c4+c5:”);

Output:
Addition of integer complexobjects…
Enter complex number c1..
Real part?1
Imag part?2
Enter complex number c2..
Real part?3
Imag part?4
C3=c1+c2:(4,6)

Addition of float complexobjects…


Enter complex number c4..
Real part?1.5
Imag part?2.5
Enter complex number c5..
Real part?2.4
Imag part?3.7
C6=c4+c5:(3.9,6.2)

Ex:no:4
#include<iostream.h>
#include<process.h>
const int TRUE=1;
const int FALSE=0;
class matrix
{
private:
int row;
int col;
int **p;
public:
matrix()
{
row=col=0;
p=NULL;
}
matrix(int r,int c);
~matrix();
void read();
void show();
void add(matrix &a,matrix &b);
void sub(matrix &a,matrix &b);

};
matrix::matrix(int r,int c)
{
row=r;
col=c;
p=new int *[row];
for(int i=0;i<row;i++)
p[i]=new int[col];
}
matrix::~matrix()
{
for(int i=0;i<row;i++)
delete p[i];
delete p;
}

void matrix::add(matrix &a,matrix &b)


{
int i,j;
row=a.row;
col=b.col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
p[i][j]=a.p[i][j]+b.p[i][j];
}
void matrix::sub(matrix &a,matrix &b)
{
int i,j;
row=a.row;
col=b.col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
p[i][j]=a.p[i][j]-b.p[i][j];
}
Void matrix::read()
{
int i,j;
for(inti=0;i<row;i++) for(intj=0;j<col;j++)
{
cout<<”Matrix[“<<i<<”,”<<j<<”]=?”;
cin>>p[i][j];
}

Void matrix::show()
{
int i,j;
for(inti=0;i<row;i++)
{
cout<<endl;
for(intj=0;j<col;j++)
{
cout<<p[i][j]<<” “;
}

void main()
{
int m,n,p,g;
cout<<”Enter the A matrix…”<<endl;
cout<<”Howmany rows?”;
cin>>m;
cout<<”How many col?”;
cin>>n;
matrix a(m,n);
a.read();

cout<<”Enter the B matrix…”<<endl;


cout<<”Howmany rows?”;
cin>>p;
cout<<”How many col?”;
cin>>q;
matrix b(p,q);
b.read();

cout<<”Matrix A is ..”;
a.show();
cout<<endl<<”Matrix B is…”;
b.show();
matrix c(m,n);
c.add(a,b);
cout<<endl<<”c=a+b..”;
c.show();
matrix d(m,n);
d.add(a,b);
cout<<endl<<”d=a-b..”;
d.show();

Output:
Enter the A matrix…
Howmany rows?2
How many col?2
Matrix[0,0]=?1
Matrix[0,1]=?1
Matrix[1,0]=?1
Matrix[1,1]=?1

Enter the B matrix…


Howmany rows?2
How many col?2
Matrix[0,0]=?1
Matrix[0,1]=?1
Matrix[1,0]=?1
Matrix[1,1]=?1

Matrix A is
1 1
1 1
Matrix B is
1 1
1 1
c=a+b
2 2
2 2
d=a-b
0 0
0 0

Ex:no:5
#include<iostream.h>
const int SIZE=10;
class vector
{
private:
int *array;
public:
void *operator new(size_t s)
{
vector *my_vector;
my_vector=::new vector;
my_vector->array=new int[SIZE];
return my_vector;
}
void operator delete(void *vec)
{
Vector *my_vect;
my_vect=(vector *) vec;
delete(int *)my_vect->array;
::delete vec;
}
void read();
int sum();
};
Void vector::read()
{
for(int i=0;i<SIZE;i++)
{
cout<<”vector[“<<i<<”]=?”;
cin>>array[i];
}
}
int vector::sum()
{
int sum=0;
for(int i=0;i<SIZE;i++)
sum=sum+array[i];
return sum;
}
void main()
{
vector *my_vector=new vector;
cout<<”enter vector data..”<<endl;
My_vector->read();
cout<<”sum of vector=”<<my_vector->sum();
delete my_vector;
}

Output:
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:2
sum of vector=10

Ex.NO: 6
#include<iostream>
#define max 10
using namespace std;
template<class t1,class t2>
void sort(t1 a[max],t2 temp,int n)
{
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
cout<<"\nElement in sorted order is : \n";
for(int m=0;m<n;m++)
cout<<a[m]<<" ";
}
int main()
{
int i,n,ch,a[max];
char choice,c[max];
float f[max];
do
{
cout<<"\n1.Sort integer\n2.Sort float\n3.Sort character\n";
cout<<"\n";
cout<<"\nEnter choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the number of integer :";
cin>>n;
cout<<"\nEnter elements :\n";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,0,n);
break;
case 2:
cout<<"\nEnter the number of float :";
cin>>n;
cout<<"\nEnter elements :\n";
for(i=0;i<n;i++)
cin>>f[i];
sort(f,0,n);
break;
case 3:
cout<<"\nEnter the number of character :";
cin>>n;
cout<<"\nEnter elements :\n";
for(i=0;i<n;i++)
cin>>c[i];
sort(c,0,n);
break;
default:
cout<<"\nInvalid\n";
break;
}
cout<<"\nDo you want to continue ? : ";
cin>>choice;
}
while(choice=='y');
return 0;
}

Ex:no:7
#include<iostream.h>
#include<process.h>
class list
{
private:
int data;
list *next;
public:
list()
{
data=0;
next=NULL;
}
ist(int dat)
{
data=dat;next=null;
}
~list(){}
int get()
{
return data;
}
void insert(list *node)
friend void display(list *);
};
void list::insert(list *node)
{
list *last=this;
while(last->next)
last=last->next;last->next=node;
}
void display(list *first)
{
list *traverse;
cout<<” list traversal yields:”;
for(traverse=first;traverse;traverse=traverse->next)
cout<<traverse->data<<”,”;
cout<<endl;
}
void main()
{
int choice, data;
list *first=null;
list *node;
while(1)
{
cout<<”Linked list…”;
cout<<”1.Insert”<<endl;
cout<<”2.Display”<<endl;
cout<<”3..quit”<<endl;
cout<<”Enter the choice”;
cin>>choice;
switch(choice)
{
case 1:cout<<”Enter the data:”;
cin>>data;
node=new list(data);
if(first==null)
first=node;
elsefirst->insert(node)
break;
case 2:
display(first);
break;
case 3:exit(1);

default:cout<<”bad options”<<endl;
continue;
}
}
}

Output:

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:2

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:2
list traversal yields:2

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:3

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:4

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:2
list traversal yields:2,3,4

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:3

Ex:no:8

#include<iostream>
#include<iomanip>
#include<process>
using namespace std;
class stack
{
private:
int *s;
int max;
int top;
public:
class FULL{}; //for exception handling
class EMPTY{}; //for exception handling
stack(int);
void push(int);
int pop(void);
void display(void);
};
stack::stack(int m)
{
s=new int[m];
top=-1;
max=m;
}
void stack::push(int item)
{
if(top<max-1)
s[++top]=item;
else
throw FULL(); //FULL object is thrown
}
int stack::pop(void)
{
if(top>=0)
return s[top--];
else
throw EMPTY(); //EMPTY object is thrown
}
void stack::display(void)
{
if(top>=0)

for(int i=top; i>=0;i–)


cout<<”\n\t|”<<setw(4)<<s[i]<<”|\n\t——”;
else
throw EMPTY();
}
int main()
{
int item, size;
int ch=1;
cout<<”\nEnter the size of stack…”;
cin>>size;
stack s1(size);
cout<<”\nStack with Exception Handling”;
cout<<”\n\n\tMENU\n1.PUSH\n2.POP\n3.SHOW STACK\n4.EXIT”;
cout<<”\nEnter your choice…”;
cin>>ch;
do
{
switch(ch)
{
case 1:
cout<<”\nEnter the item to push…”;
cin>>item;
try
{
s1.push(item);
}
catch(stack::FULL) //FULL object is caught
{
cout<<”\n***Stack Overflow***\n”;
}
break;
case 2:
try
{
cout<<”\nPoped Item is…”<<s1.pop();
}
catch(stack::EMPTY) //EMPTY object caught
{
cout<<”\n***Stack Empty***\n”;
}

break;
case 3:
cout<<”\nThe Stack is…\n”;
try
{ s1.display(); }
catch(stack::EMPTY)
{
cout<<”\n***Stack Empty***\n”;
}
break;
case 4:
exit(0);
}
cout<<”\nEnter your choice…”;
cin>>ch;
}
while(ch<5);
return 0;
}

Ex:no:9
//Program using prims algorithm to find the minimum cost spanning tree
# include<iostream>
//# include <conio.h>
# define infi 5000
using namespace std;

class Point
{
public:
int x;
int y;
void getpoint()
{
cin>>x>>y;
}
};
class Arc
{
public:
int x1,y1,x2,y2;
void setarc(int a,int b,int c,int d)
{
x1=a;
y1=b;
x2=c;
y2=d;
}
};
class Graph
{
int Cost[10][10];
int N_POINTS,N_ARCS;
Point *P;
Arc *A;
public:
Graph(int p=5,int a=10)
{
N_POINTS=p;
N_ARCS=a;
}
int findpoint(int x1,int y1);
void getcost();
void showcost();
void primsAlgo();
};
int Graph::findpoint(int x1,int y1)
{
for(int i=1;i<=N_POINTS;i++)
{
if(P[i].x==x1&&P[i].y==y1)
return i;
}
return 0;
}
void Graph::getcost()
{
int x1,y1,x2,y2,wt;
P=new Point[N_POINTS];
A=new Arc[N_ARCS];
cout<<"Enter the points(x,y):";
for(int i=1;i<=N_POINTS;i++)
P[i].getpoint();
cout<<"Enter the arc coordinates and weight"<<endl;
for(i=1;i<=N_POINTS;i++)
for(int j=1;j<=N_POINTS;j++)
Cost[i][j]=0;
for(i=1;i<=N_ARCS;i++)
{
cout<<"Arc x1 y1 x2 y2 weight "<<endl;
cin>>x1>>y1>>x2>>y2>>wt;
A[i].setarc(x1,y1,x2,y2);
int a=findpoint(x1,y1);
int b=findpoint(x2,y2);
Cost[a][b]=wt;
Cost[b][a]=wt;
}
}
void Graph::showcost()
{
for(int i=1;i<=N_POINTS;i++)
{
for(int j=1;j<=N_POINTS;j++)
cout<<Cost[i][j]<<" ";
cout<<endl;
}
}
void Graph::primsAlgo()
{
int *selected = new int[N_POINTS],i,j,ne;
int false =0,true=1,min,x,y;
for(i=1;i<=N_POINTS;i++)
{
for(j=1;j<=N_POINTS;j++)
{
if(Cost[i][j]==0&&i!=j)
Cost[i][j]=infi;
}
}
for(i=1;i<=N_POINTS;i++)
selected[i]=false;
selected[1]=true;
ne=0;
cout<<"MINIMUM COST SPANNING TREE:"<<endl;
while(ne<N_POINTS-1)
{
min=infi;
for(i=1;i<=N_POINTS;i++)
{
if(selected[i]==true)
{
for(j=1;j<=N_POINTS;j++)
{
if(selected[j]==false)
{
if(min>Cost[i][j])
{
min=Cost[i][j];
x=i;
y=j;
}
}
}
}
}
selected[y]=true;
cout<<"\n"<<x<<"-->"<<y;
ne=ne+1;
}
int main()
{

int np,na;
cout<<"Enter the no.of points";
cin>>np;
cout<<"Enter the no.of arcs";
cin>>na;
Graph G(np,na);
G.getcost();
cout<<endl<<" COST MATRIX"<<endl;
G.showcost();
G.primsAlgo();

}
Ex.No: 10
#include<iostream.h>

#include<conio.h>

#include<string.h>

class media

protected:

char title[50];

float price;

public:

media(char *s,float a)

strcpy(title,s);

price=a;

virtual void display()

{
}

};

class book:public media

int pages;

public:

book(char *s,float a,int p):media(s,a)

pages=p;

void display();

};

class tape:public media

float time;

public:

tape(char *s,float a,float t):media(s,a)

time=t;

void display();

};

void book::display()
{

cout<<"\n\n Title : "<<title;

cout<<"\n Pages : "<<pages;

cout<<"\n Price : "<<price;

void tape::display()

cout<<"\n\n Title : "<<title;

cout<<"\n Playtime : "<<time<<"min";

cout<<"\n Price : "<<price;

void main()

char *title=new char[30];

float time,price;

int pages;

clrscr();

cout<<"\t\t VIRTUAL FUNCTION USING POLYMORPHISM";

cout<<"\n\t\t ~~~~~~~ ~~~~~~~~ ~~~~~ ~~~~~~~~~~~~";

cout<<"\n\n Enter the book details :-";

cout<<"\n Title : ";

cin>>title;

cout<<"\n Price : ";


cin>>price;

cout<<"\n Pages : ";

cin>>pages;

book book1(title,price,pages);

cout<<"\n Enter the tape details:-";

cout<<"\n Title : ";

cin>>title;

cout<<"\n Price : ";

cin>>price;

cout<<"\n Playtime : ";

cin>>time;

tape tape1(title,price,time);

media *list[2];

list[0]=&book1;

list[1]=&tape1;

cout<<"\n\n Media details:-";

cout<<"\n —– ——-";

cout<<"\n—Book";

list[0]->display();

cout<<"\n\n—Tape";

list[1]->display();

getch();

}
Ex.No : 11
# include<iostream.h>
# include<stdlib.h>
# include<conio.h>
# include<fstream.h>
# include<iomanip.h>

class complex
{
public:
int real;
int img;
complex()
{
real=0;
img=0;
}
complex(int x,int y)
{
real=x;
img=y;
}
complex operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;
}
complex operator-(complex c)
{
complex temp;
temp.real=real-c.real;
temp.img=img-c.img;
return temp;
}
complex operator*(complex c)
{
complex temp;
temp.real=(real*c.real)-(img*c.img);
temp.img=(real*c.img)+(img*c.real);
return temp;
}
void show()
{
cout<<real;
if(img<0)
cout<<"-i"<<abs(img);
else
cout<<"+i"<<img;
cout<<endl;
}
};
int ctoi(char c)
{
int i=0;
switch(c)
{
case '0': i=0; break;
case '1': i=1;break;
case '2': i=2;break;
case '3':i=3;break;
case '4': i=4;break;
case '5':i=5;break;
case '6':i=6;break;
case '7':i=7;break;
case '8':i=8;break;
case '9':i=9;break;
}
return i;
}
void main()
{
clrscr();
char line[20];
char c;
ifstream fin("outputfile.txt");
ofstream fout("Result.txt");
while(!fin.eof())
{
fin.getline(line,20);
cout<<line<<endl;
c=line[0];
complex c1(ctoi(line[5]),ctoi(line[8]));
complex c2(ctoi(line[13]),ctoi(line[16]));
complex c3;
switch(c)
{
case '+':
fout<<setw(10)<<"SUM";
c3=c1+c2;
break;
case '-':
fout<<setw(10)<<"DIFF";
c3=c1-c2;
break;
case '*':
fout<<setw(10)<<"PRODUCT";
c3=c1*c2;
break;
}
fout<<setw(5)<<c3.real<<"+i"<<c3.img<<endl;
}
fin.close();
fout.close();
getch();
}

Das könnte Ihnen auch gefallen