Professional Documents
Culture Documents
Pds - Manual 1 To 11
Pds - Manual 1 To 11
Pds - Manual 1 To 11
Ai m:
To implement a simple C++ program for addition of two two numbers using class.
Algorithm:
Step 1: Start
Step 4: Declare and define display() to display the two numbers and sum value.
Step 8:Stop.
Program
Result:
Thus the above Simple C++ program for addition of two numbers using class was created
successfully and verified.
Aim:
To implement a simple C++ program to print the minimum value among the given set of
values.
Algorithm:
Step 1:Start
Step3:check if (a<=b).
Step 4:Print the Minimum value is a.
Step 6:Stop.
Program
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"Enter a and b";
cin>>a>>b;
if(a<=b)
{
cout<<"minimum value is "<<a;
}
else
{
cout<<"minimum value is "<<b;
}
getch();
}
Result:
Thus the above Simple C++ program to print the minimum value was created successfully and
verified.
Aim:
To implement a Simple C++ program to display the name and number.
Algorithm:
Step 2:Declare the class as person with data members and Number.
Step 5:Access the data member using object and display the output.
Step 6:Stop.
Program
#include<iostream.h>
class student
{
private:
char name[20];
int no;
public:
void input();
void display();
};
void student::input()
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Reg no.:";
cin>>no;
}
void student::display()
{
cout<<"\nName:"<<name;
cout<<"\nRegno.:"<<no;
}
int main()
{
student s;
s.input();
s.display();
}
Result:
Thus the above simple C++ program to display the name and number was created successfully
and verified.
Aim:
Algorithm:
Step 1:Start.
Step 7:stop.
Program
#include<iostream.h>
#include<conio.h>
class prime
{
int a,k,i;
public:
prime(int x)
{
a=x;
}
void calculate()
{
k=1;
for(i=2;i<=a/2;i++){
if(a%2==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<<"\n"<<a<<"\t"<<"Prime Number"<<endl;
else
cout<<"\n"<<a<<"\t"<<"Not prime"<<endl;
}
};
void main()
{
int a;
clrscr();
cout<<"Enter the number";
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
getch();
}
Result:
Thus the above C++ program for Parameterized Constructor was created successfully and
verified.
Aim:
Algorithm:
Step 1:start
Step 5:Stop.
Program
#include<iostream.h>
#include<conio.h>
class box
{
public:
box(){
cout<<"Constructor called"<<endl;
}
~box(){
cout<<"Destructor called"<<endl;
}
};
int main()
{
clrscr();
box *myboxarray=new box[4];
delete[] myboxarray;
return 0;
}
Result:
Thus the above C++ program for Dynamic Constructor was created successfully and verified.
2.c)COPY CONSTRUCTOR.
Aim:
To write a C++ program to find factorial of a given number using Copy Constructor.
Algorithm:
Step 2: Declare the class name as copy with data members and member functions.
Program
#include<iostream.h>
#include<conio.h>
class copy
{
int var,fact;
public:
copy(int temp)
{
var = temp;
}
double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
clrscr();
int n;
cout<<"\n\tEnter the Number : ";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate();
cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate();
getch();
}
Result:
Thus the above C++ program for to find factorial of a given number using Copy Constructor
was implemented successfully.
2.d) DESTRUCTOR
Aim:
Algorithm:
Step 6: Display The Name, Roll Number, And Address Of The Student
Step 7:Stop.
Program
#include<iostream.h>
#include<conio.h>
class stu
{
private:
char name[20],add[20];
int roll,zip;
public:
stu();
~stu();
void read();
void disp();
};
stu::stu(void)
{
cout<<"This is student details"<<endl;
}
void stu::read()
{
cout<<"Enter student name";
cin>>name;
cout<<"Enter student rollno";
cin>>roll;
cout<<"Enter student address";
cin>>add;
cout<<"Enter student zip code"<<endl;
cin>>zip;
}
void stu::disp()
{
cout<<"student name:"<<name<<"\n";
cout<<"student roll no:"<<roll<<"\n";
cout<<"student address:"<<add<<"\n";
cout<<"student zipcode:"<<zip<<"\n";
}
stu::~stu()
{
cout<<"Student details is closed";
}
void main()
{
stu s;
s.read();
s.disp();
getch();
}
Result:
Thus the above C++ program to print student details using destructor was created successfully
and verified.
Aim:
To implement a C++ program to find the mean value of a given number using friend function.
Algorithm:
Program
#include<iostream.h>
#include<conio.h>
class base
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:"<<"\n";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\nMean value is:"<<mean(obj);
getch();
}
Result:
Thus the above C++ program to find the mean value of a given number using friend function was
created successfully and verified.
Aim:
Algorithm:
Step 1:Start.
Step 2: Create a class named read int with data members as two floating point variables a and b.
Step 3:Define the member function named read() inside read int class that gets the input for the two
variables.
Step 4:Create a new class named sum with data members as float variables C.
Step 6:Define member function named add() inside sum class that adds the data members of read int
class and stores the result in sum class.
Step 7:Create object for both classes inside main() as r and s for read int and sum.
Step 8: Call the r.read() and s.add() and display the result.
Program
#include<iostream.h>
#include<conio.h>
class readint
{
float a,b;
public:
void read()
{
cout<<"\nEnter a and b:"<<endl;
cin>>a>>b;
}
friend class sum;
};
class sum
{
public:
float c;
void add(readint rd)
{
c=rd.a+rd.b;
cout<<"Sum="<<c;
}
};
void main()
{
readint rd;
sum s;
clrscr();
rd.read();
s.add(rd);
getch();
}
Result:
Thus the above program for friend class was created successfully and verified.
4.a) SINGLE INHERITENCE
Aim:
To implement a C++ program to print the student details using single inheritance
Algorithm:
Program
#include<iostream.h>
#include<conio.h>
class student
{
public:
char name[10],collname[10],dept[5];
void getfun()
{
cout<<"enter name ,college ,department\n";
cin>>name >>collname>>dept;
}
};
class it :public student {
public :
char sub1[5],sub2[5],sub3[5];
void getdet()
{
getfun();
cout<<"enter subject 1,2,3";
cin>>sub1>>sub2>>sub3;
}
void putdet()
{
cout<<"name\t"<<name<<"\n";
cout<<"college name\n"<<collname<< dept<<"subject1,2,3\t"<<sub1<<sub2<<sub3;
}
};
void main()
{
clrscr();
it obj;
obj.getdet();
obj.putdet();
getch();
}
Result:
Thus a C++ program was print the student details using single inheritance was implemented
successfully.
Program
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
inttot,avg;
13
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
RESULT:
Thus a C++ program to find out the student details using multiple inheritance is implemented
successfully.
4 d) HIERARCHIAL INHERITANCE
AIM:
To write a c++ program to illustrate area of square and circle by hierarchial inheritance.
ALGORITHM:
Step6: create the base class object and the pointer variable.
Step7: call the functions display () and show () using the base class object and the pointer.
Step8: create the derived class object and call the function display () and show () using the derived
class object and pointer.
Program
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
float l;
public:
void get_data()
{
cin>>l;
}
virtual float area()=0;
};
class square:public shape
{
public:
float area()
{
return l*l;
}
};
class circle:public shape
{
public:
float area()
{
return 3.14*l*l;
}
};
int main()
{
clrscr();
square s;
circle c;
cout<<"Enter length to calculate area of square:"<<endl;
s.get_data();
cout<<"Area of square:"<<s.area()<<endl;
cout<<"Enter radius to calculate the area of circle:";
c.get_data();
cout<<"Area of circle is:"<< c.area();
getch();
return 0;
}
RESULT:
Thus a C++ program to find out the area of the circle and square using hierarchical inheritance
was implemented successfully.
5) FUNCTION OVERLOADING
AIM:
ALGORITHM:
STEP1: start the program.
STEP3: Declare class name as function with data members and member functions.
STEP6: The function area() to find area of circle with one integer argument.
STEP8: The function area() to find area of triangle with three arguments,two as integer and one as
float.
PROGRAM:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(inta,int b)
{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,inta,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}
void main()
{
intch;
inta,b,r;
23
clrscr();
fnobj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
cout<<”Enter your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radious of the Circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
SAMPLE OUTPUT:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
RESULT:
Thus a C++ program to calculate the area of circle, rectangle and triangle using function overloading is
implemented successfully.
6.) VIRTUAL FUNCTION.
AIM:
ALGORITHM:
Step6: create the base class object and the pointer variable.
Step7: call the functions display () and show () using the base class object and the pointer.
Step8: create the derived class object and call the function display () and show () using the derived
class object and pointer.
RESULT:
Thus the c++ program for virtual function was implemented successfully.
AIM:
To implement c++ program to find complex numbers using unary operator overloading.
ALGORITHM:
Step10: Call function operator ++ () by incrementing the class object and call the function
display.
Step11: Call function operator - - () by decrementing class object and call the function display.
Program
#include<iostream.h>
#include<conio.h>
class unary
{
private:
int x;
public:
unary()
{
x=0;
}
void display()
{
cout<<"value of x is"<<x<<endl;
}
void operator ++();
};
void unary::operator ++()
{
++x;
}
void main()
{
clrscr();
unary obj1,obj2;
cout<<"before increment:"<<endl;
cout<<"object.obj1:";
obj1.display();
cout<<"object.obj2:";
obj2.display();
++obj1;
++obj2;
cout<<"after increment:"<<endl;
cout<<"object.obj1:";
obj1.display();
cout<<"object.obj2:";
obj2.display();
getch();
}
RESULT:
Thus the c++ program for unary operator overloading was implemented successfully.
AIM:
To implement a c++ program to add two complex numbers using binary operator overloading.
ALGORITHM:
Step9: Call the function get value using obj1 and obj2.
Step10:Calculate value for object result by calling function operator + and operator -.
getvalue()
{
cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
29
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
SAMPLE OUTPUT:
Enter the value of Complex Numbers a, b
45
Enter the value of Complex Numbers a, b
22
Input Values
4 + 5i
2 + 2i
Result
6 + 7i
2 + 3i
RESULT:
Thus the c++ program to add two complex numbers using binary operator overloading was
implemented successfully.
AIM:
To write a c++ program to implement class templates.
ALGORITHM:
Step2: create a class name test and write a constructor to the assign the value of parameter
passed.
Step4: In main function, create an integer abject, float object ,char object and pass integer ,float ,
char value to constructor.
Step5: For this purpose we need not to write two separate constructor functions instead of we
can use class templates
RESULT:
Thus the c++ program for class template was implemented successfully.
Aim:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
template<class t>
void swap(t &x,t&y)
{
t temp=x;
x=y;
y=temp;
}
void fun(inta,intb,floatc,float d)
{
cout<<"\na and b before swaping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swaping :"<<a<<"\t"<<b;
cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swaping :"<<c<<"\t"<<d;
}
void main()
{
inta,b;
float c,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
fun(a,b,c,d);
getch();
}
SAMPLE OUTPUT:
Enter A, B values (integer): 10 20
Enter C, D values (float): 2.50 10.80
A and B before swapping: 10 20
A and B after swapping: 20 10
C and D before swapping: 2.50 10.80
C and D after swapping: 10.80 2.50
RESULT:
Thus the c++ program for function template was implemented successfully.
AIM:
To write a c++ program to perform exception handling for divide by zero exception.
ALGORITHM:
Step4: Check if (c !=0) within try block. if it is not equal to zero then print a/c value.
Step5: Else throw c value.
Program
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
cout<<"Enter value for A and B:"<<endl;
cin>>a>>b;
c=a-b;
try
{
if(c!=0)
{
cout<<"The value of a/c is:"<<a/c<<endl;
}
else
{
throw(c);
}
}
catch(int c)
{
cout<<"Divide by zero"<<endl;
}
getch();
}
RESULT:
Thus the c++ program to perform exception handling for divide by zero exception was
implemented successfully.
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
if(c!=0)
{
cout<<"the value of a/c is "<<a/c<<"\n";
}
else
{
throw(c);
}
}
void main()
{
try
{
divide(20,10);
divide(10,10);
}
catch(int c)
{
cout<<"it is divided by zero \n";
}
getch();
}
RESULT:
Thus the c++ program for invoking a function in exception handling was implemented
successfully.
9c) MULTI-CATCH STATEMENT
AIM:
ALGORITHM:
Program
#include<iostream.h>
#include<conio.h>
void main()
{
test(1);
test(0);
test(2);
}
void test(int x)
{
float y;
{
try
{
if(x==1) throw (x);
catch(int x)
{
cout<<"catch a int element \n";
}
catch(char x)
{
cout<<"catch a char element \n";
}
catch(float y)
{
cout<<"catch a float element";
}
}
getch();
}
RESULT:
Thus the c++programformulitcatch statement in exception handling was executed successfully
and verified.
AIM: To implement a C++ program to illustrate the concept of standard template library.
ALGORITHM:
Step 1: Include the header files
PROGRAM:
#include <iostream.h>
#include <vector>
using namespace std;
int main()
{
vector<int>vec;
int i;
cout<< "vector size = " <<vec.size() <<endl;
for(i = 0; i < 5; i++)
{
vec.push_back(i);
}
cout<< "extended vector size = " <<vec.size() <<endl;
for(i = 0; i < 5; i++)
{
cout<< "value of vec [" << i << "] = " <<vec[i] <<endl;
}
vector<int>::iterator v = vec.begin();
while( v != vec.end()) {
cout<< "value of v = " << *v <<endl;
v++;
}
return 0;
}
SAMPLE OUTPUT:
vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4
RESULT:
Thus a C++ program to illustrate the concept of standard template libraryis implemented
successfully.
Ex.No 11 a)
FILE STREAM CLASSES
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char c,fname[10];
clrscr();
cout<<"Enter file name:";
cin>>fname;
ifstream in(fname);
if(!in)
{
cout<<"File Does not Exist";
getch();
return;
}
cout<<"\n\n";
while(in.eof()==0)
{
in.get(c);
cout<<c;
}
getch();
}
SAMPLE OUTPUT:
Enter File name: one.txt
Master of Computer Applications
RESULT:
Thus a C++ program to read the content of a file is implemented successfully.
11)b.
TO PERFORM THE WRITE OPERATION WITHIN A FILE
AIM: To implement a C++ program to perform the write operation within a file.
ALGORITHM:
Step 1: Include the header files
Step 2: Declare the variables.
Step 3: Read the file name.
Step 4: open the file to write the contents.
Step 5: writing the file contents up to reach a particular condition.
PROGRAM:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char c,fname[10];
ofstream out;
cout<<"Enter File name:";
cin>>fname;
out.open(fname);
cout<<"Enter contents to store in file (Enter # at end):\n";
while((c=getchar())!='#')
{
out<<c;
}
out.close();
getch();
}
SAMPLE OUTPUT:
Enter File name: one.txt
Enter contents to store in file (enter # at end)
Master of Computer Applications#
RESULT:
Thus a C++ program to perform the write operation within a file is implemented successfully