Oops Manual
Oops Manual
Output:
Aim:
To write a C++ program with overloaded functions.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Display the choices.
Step 4: If the choice is 1, get the side of the square and print its area, using the function
area(float).
Step 5: If the choice is 2, get the length and width of the rectangle, and print its area, using
the function area(float,float).
Step 6: If the choice is default, prompt that the choice is invalid.
Step 7: End.
Program:
# include <iostream>
using namespace std;
float area(float side)
{
return side*side;
}
float area(float length,float width)
{
return length*width;
}
int main()
{
int c;
float l,w,s;
cout<<"1. Square"<<endl;
cout<<"2. Rectangle"<<endl;
cout<<"Enter the shape of your choice : ";
cin>>c;
switch(c)
{
case 1: cout<<"\nEnter the side of the square : ";
cin>>s;
cout<<"\n The area of square is "<<area(s);
break;
case 2: cout<<"\nEnter the length of the rectangle : ";
cin>>l;
cout<<"\nEnter the width of the rectangle : ";
cin>>w;
cout<<"\nThe area of rectangle is "<<area(l,w);
break;
default:cout<<"\n Invalid choice!!!";
break;
}
}
Output 1:
1. Square
2. Rectangle
Enter the shape of your choice : 1
Output 2:
1. Square
2. Rectangle
Enter the shape of your choice : 2
Result:
The C++ programs using functions and overloaded functions were implemented
successfully.
Ex.No: 2
Simple C++ program using classes and objects
Aim:
To write a C++ program with class and objects.
Algorithm:
Step 1: Start
Step 2: Declare the headers.
Step 3: Get the number of students.
Step 4: Get the details of the students, one by one.
Step 5: Print the details of students who have an overall percentage above 75%.
Step 6: End.
Program:
# include <iostream>
# include <iomanip>
using namespace std;
class Student
{
string name,branch;
char sex;
int roll,marks[5];
public:
float gpercent();
void getDetails();
void display();
}s[51];
float Student::gpercent()
{
int s=0,i;
for(i=0;i<5;i++)
s+=marks[i];
return (s/5.0);
}
void Student::getDetails()
{
cout<<"\n Enter name : ";
cin>>name;
cout<<"\n Enter branch : ";
cin>>branch;
cout<<"\n Enter sex(M/F) : ";
cin>>sex;
cout<<"\n Enter roll no. : ";
cin>>roll;
cout<<"\n Enter the marks in 5 subjects : ";
for(int i=0;i<5;i++)
cin>>marks[i];
}
void Student::display()
{
cout<<"\n "<<setw(10)<<name<<" ";
cout<<" "<<setw(6)<<branch<<" ";
cout<<" "<<setw(3)<<sex<<" ";
cout<<" "<<setw(4)<<roll<<" ";
cout<<" "<<setw(5)<<marks[0]<<" ";
cout<<setw(5)<<marks[1]<<" ";
cout<<setw(5)<<marks[2]<<" ";
cout<<setw(5)<<marks[3]<<" ";
cout<<setw(5)<<marks[4]<<" ";
cout<<" "<<fixed<<setprecision(2)<<gpercent();
}
void above70(int n)
{
int i;
cout<<"\n Name ";
cout<<" Branch ";
cout<<" Sex ";
cout<<" Roll ";
cout<<" Mark1 Mark2 Mark3 Mark4 Mark5 ";
cout<<" Percent";
for(i=0;i<n;i++)
{
if(s[i].gpercent()>=70.0)
s[i].display();
}
}
int main()
{
int n,i;
cout<<"\n Enter the no. of students : ";
cin>>n;
cout<<"\n Enter the student details : ";
for(i=0;i<n;i++)
{
cout<<"\n Student "<<i+1<<" :";
s[i].getDetails();
}
cout<<"\n Displaying record of students who scored above 70% :";
above70(n);
return 0;
}
Output:
Enter sex(M/F) : M
Enter roll no. : 17
Student 2 :
Enter name : Rishab
Enter sex(M/F) : M
Result:
The simple C++ program with class and objects was implemented successfully.
Ex.No: 3
Execute C++ programs with constructors and destructors
Aim:
To write a C++ program with constructors and destructors.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the real and imaginary parts of the complex number from the user.
Step 4: Create an object for the class complex, by passing the real and imaginary parts of
the complex number.
Step 5: Display the complex number, in a formatted way.
Step 6: End.
Program:
# include <iostream>
# include <stdio.h>
using namespace std;
class complex
{
int real,image;
public:
complex(int m,int n)
{
real=m;
image=n;
cout<<"\nObject is created successfully";
}
string putcno()
{
char ret[101];
sprintf(ret,"%d+%di",real,image);
return ret;
}
~complex()
{
cout<<"\nObject is deleted successfully.";
}
};
int main()
{
int r,i;
cout<<"Enter the real part of the complex no. : ";
cin>>r;
cout<<"\nEnter the imaginary part of the complex no. : ";
cin>>i;
complex c(r,i);
cout<<"\nThe complex no. is "<<c.putcno();
return 0;
}
Output:
Result:
The C++ program using constructors and destructor was implemented successfully.
Ex.No: 4
Programs using operator overloading
Aim:
To write a C++ program using operator overloading.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the times to be added from the user.
Step 4: Determine the sum of the times.
Step 5: Display the sum of times.
Step 6: End.
Program:
# include <iostream>
using namespace std;
class TIME
{
int hr,min,sec;
public:
void gettime()
{
cout<<"\n Enter the hours : ";
cin>>hr;
cout<<"\n Enter the minutes : ";
cin>>min;
cout<<"\n Enter the seconds : ";
cin>>sec;
}
void showtime()
{
printf(" %02d:%02d:%02d",hr,min,sec);
}
TIME operator+(const TIME& b)
{
TIME t;
int s,m;
t.sec=(this->sec+b.sec)%60;
s=(this->sec+b.sec)/60;
t.min=(this->min+b.min+s)%60;
m=(this->min+b.min+s)/60;
t.hr=this->hr+b.hr+m;
return t;
}
};
int main()
{
TIME t1,t2,res;
cout<<"\n Enter time1 :";
t1.gettime();
cout<<"\n Enter time2 :";
t2.gettime();
res=t1+t2;
cout<<"\n Time 1 : ";
t1.showtime();
cout<<"\n Time 2 : ";
t2.showtime();
cout<<"\n The added time : ";
res.showtime();
return 0;
}
Output:
Enter time1 :
Enter the hours : 11
Enter time2 :
Enter the hours : 20
Time 1 : 11:32:16
Time 2 : 20:00:48
The added time : 31:33:04
Result:
The C++ program using operator overloading was implemented successfully.
Ex.No: 5
Test type conversions for various data types
Aim:
To write a C++ program that implements implicit type conversion.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the radius of the circle from the user.
Step 4: Calculate the area of the circle.
Step 5: Print the area of the circle.
Step 6: End.
Program:
# include <iostream>
using namespace std;
int main()
{
float rad;
double area;
cout<<"Enter the radius of the circle : ";
cin>>rad;
area=3.14*rad*rad;
cout<<"\nThe area of the circle is "<<area;
return 0;
}
Output:
Aim:
To write a C++ program that implements explicit type conversion.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the principle amount, time period and the rate of interest from the user.
Step 4: Calculate the simple interest.
Step 5: Print the simple interest.
Step 6: End.
Program:
# include <iostream>
using namespace std;
int main()
{
int t;
float p,r;
cout<<"Enter the priciple amount : ";
cin>>p;
cout<<"\nEnter the time period(in years) : ";
cin>>t;
cout<<"\nEnter the interest rate : ";
cin>>r;
double si=double(p*t*r)/100;
cout<<"\nThe simple interest is "<<si;
return 0;
}
Output:
Result:
The C++ programs testing type conversion were implemented successfully.
Ex.No: 6
Design programs using all the types of inheritances
Aim:
To write a C++ program to implement Single inheritance.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the student details.
Step 4: Get the marks of the student in three subjects.
Step 5: Display the student details.
Step 6: End.
Program:
# include <iostream>
using namespace std;
class Student
{
string name;
int roll;
public:
void getStud()
{
cout<<"\nEnter the name of the student : ";
cin>>name;
cout<<"\nEnter the roll no. of the student : ";
cin>>roll;
}
void putStud()
{
cout<<"\nNAME : "<<name;
cout<<"\nROLL NO. : "<<roll;
}
};
class Exam:public Student
{
int marks[3];
public:
void getMarks()
{
cout<<"\nEnter the marks in 3 subjects : ";
for(int i=0;i<3;i++)
cin>>marks[i];
}
void putMarks()
{
cout<<"\nMarks : ";
for(int i=0;i<3;i++)
cout<<marks[i]<<" ";
}
};
int main()
{
Exam e;
cout<<"Enter student details : ";
e.getStud();
e.getMarks();
cout<<"\nStudent Details :";
e.putStud();
e.putMarks();
return 0;
}
Output:
Student Details :
NAME : Ashish
ROLL NO. : 3
Marks : 89 81 92
Aim:
To write a C++ program to implement Hierarchical inheritance.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the details of the students and their marks for Exam class.
Step 4: Display the marks with the details.
Step 5: Get the details of the student and sport marks for the Sports class.
Step 6: Display the sport marks with details.
Step 7: End.
Program:
# include <iostream>
using namespace std;
class Student
{
string name;
int roll;
public:
void getStud()
{
cout<<"\nEnter the name of the student : ";
cin>>name;
cout<<"\nEnter the roll no. of the student : ";
cin>>roll;
}
void putStud()
{
cout<<"\nNAME : "<<name;
cout<<"\nROLL NO. : "<<roll;
}
};
class Exam:public Student
{
int marks[3];
public:
void getMarks()
{
cout<<"\nEnter the marks in 3 subjects : ";
for(int i=0;i<3;i++)
cin>>marks[i];
}
void putMarks()
{
cout<<"\nMarks : ";
for(int i=0;i<3;i++)
cout<<marks[i]<<" ";
}
};
class Sports:public Student
{
int sport;
public:
void getSport()
{
cout<<"\nEnter the marks in sports(on 10) : ";
cin>>sport;
}
void putSport()
{
cout<<"Sport marks :"<<sport;
}
};
int main()
{
Exam e;
Sports s;
cout<<"Enter student details for Exam : ";
e.getStud();
e.getMarks();
cout<<"\nStudent Details for Exam :";
e.putStud();
e.putMarks();
cout<<"\nEnter student details for Sports : ";
s.getStud();
s.getSport();
cout<<"\nStudent Details for Sports :";
s.putStud();
s.putSport();
return 0;
}
Output:
Aim:
To write a C++ program that implements Multilevel inheritance.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the student details and marks from the user.
Step 4: Display the student details, marks and total & average computed.
Step 5: End.
Program:
# include <iostream>
using namespace std;
class Student
{
string name;
int roll;
public:
void getStud()
{
cout<<"\nEnter the name of the student : ";
cin>>name;
cout<<"\nEnter the roll no. of the student : ";
cin>>roll;
}
void putStud()
{
cout<<"\nNAME : "<<name;
cout<<"\nROLL NO. : "<<roll;
}
};
class Exam:public Student
{
protected:
int marks[3];
public:
void getMarks()
{
cout<<"\nEnter the marks in 3 subjects : ";
for(int i=0;i<3;i++)
cin>>marks[i];
}
void putMarks()
{
cout<<"\nMarks : ";
for(int i=0;i<3;i++)
cout<<marks[i]<<" ";
}
};
class Result:public Exam
{
int tot;
float avg;
public:
void compute()
{
tot=0;
for(int i=0;i<3;i++)
tot+=marks[i];
avg=tot/3.0;
}
void get()
{
getStud();
getMarks();
}
void display()
{
compute();
putStud();
putMarks();
cout<<"\nTotal : "<<tot;
cout<<"\nAverage : "<<avg;
}
};
int main()
{
Result r;
cout<<"Enter student details : ";
r.get();
cout<<"\nStudent Details : ";
r.display();
return 0;
}
Output:
Student Details :
NAME : Sunil
ROLL NO. : 4
Marks 91 89 95
Total 275
Average : 91.6667
Aim:
To write a C++ program that implements Multiple inheritance.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the subject marks and sport marks from the user.
Step 4: Display the consolidated list of marks.
Step 5: End.
Program:
# include <iostream>
using namespace std;
class Exam
{
protected:
int marks[3];
public:
void getMarks()
{
cout<<"\nEnter the marks in 3 subjects : ";
for(int i=0;i<3;i++)
cin>>marks[i];
}
void putMarks()
{
cout<<"\nMarks : ";
for(int i=0;i<3;i++)
cout<<marks[i]<<" ";
}
};
class Sports
{
int sport;
public:
void getSport()
{
cout<<"\nEnter the marks in sports(on 10) : ";
cin>>sport;
}
void putSport()
{
cout<<"Sport marks :";
}
};
class Result:public Exam,public Sports
{
int tot;
float avg;
public:
void compute()
{
tot=0;
for(int i=0;i<3;i++)
tot+=marks[i];
avg=tot/3.0;
}
void get()
{
getMarks();
getSport();
}
void display()
{
compute();
putMarks();
cout<<"\nTotal : "<<tot;
cout<<"\nAvereage : "<<avg;
putSport();
}
};
int main()
{
Result r;
cout<<"Enter the marks : ";
r.get();
cout<<"\nThe computed data : ";
r.display();
return 0;
}
Output:
Aim:
To write a C++ program that implements Hybrid inheritance.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the student details and subject marks for class Exam from the user.
Step 4: Display the student details along with the subject marks.
Step 5: Get the student details, fitness marks, and sport marks for class Sports from the
user.
Step 6: Display the student details, fitness marks, and th sport marks.
Step 7: End.
Program:
# include <iostream>
using namespace std;
class Student
{
string name;
int roll;
public:
void getStud()
{
cout<<"\nEnter the name of the student : ";
cin>>name;
cout<<"\nEnter the roll no. of the student : ";
cin>>roll;
}
void putStud()
{
cout<<"\nNAME : "<<name;
cout<<"\nROLL NO. : "<<roll;
}
};
class Fitness
{
int fitness;
public:
void getFitness()
{
cout<<"\nEnter the fitness marks(1-5) : ";
cin>>fitness;
}
void putFitness()
{
cout<<"\nFitness : "<<fitness;
}
};
class Exam:public Student
{
int marks[3];
public:
void getMarks()
{
getStud();
cout<<"\nEnter the marks in 3 subjects : ";
for(int i=0;i<3;i++)
cin>>marks[i];
}
void putMarks()
{
putStud();
cout<<"\nMarks : ";
for(int i=0;i<3;i++)
cout<<marks[i]<<" ";
}
};
class Sports:public Student,public Fitness
{
int sport;
public:
void getSport()
{
getStud();
getFitness();
cout<<"\nEnter the marks in sports(on 10) : ";
cin>>sport;
}
void putSport()
{
putStud();
putFitness();
cout<<"\nSport marks :"<<sport;
}
};
int main()
{
Exam e;
Sports s;
cout<<"Enter student details for Exam : ";
e.getMarks();
cout<<"\nStudent Details for Exam :";
e.putMarks();
cout<<"\nEnter student details for Sports : ";
s.getSport();
cout<<"\nStudent Details for Sports :";
s.putSport();
return 0;
}
Output:
Student Details :
NAME : Dev
ROLL NO. : 16
Marks 91 90 95
Total 276
Average 92
Sport marks : 9
Result:
The C++ programs to implement all types of inheritance were implemented successfully.
Ex.No: 7
Construct dynamic binding using virtual functions, pure virtual functions
Aim:
To write a C++ program that implements dynamic binding using virtual functions.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the brand name, display type and the price of an Android smartphone, and
assign “Android” for the OS variable.
Step 4: Display the detailed information of the Android smartphone.
Step 5: Get the display type and the price of an Apple smartphone and assign “Apple” and
“iOS” for the brand and OS variables.
Step 6: Display the detailed information of the Apple smartphone.
Step 7: End.
Program:
# include <iostream>
using namespace std;
class SmartPhone
{
public:
string Brand, OS, Display_type;
int Price;
virtual void PrintPhoneDetails()
{
cout<<"\n Brand : "<<Brand;
cout<<"\n OS : "<<OS;
cout<<"\n Display : "<<Display_type;
cout<<"\n Price : $ "<<Price;
}
};
class Android: virtual public SmartPhone
{
public:
void setdetails_An()
{
cout<<"\n Enter the details of Android Smartphone :";
cout<<"\n Enter the Brand name : ";
cin>>Brand;
cout<<"\n Enter the Display type : ";
cin>>Display_type;
cout<<"\n Enter the price(in $) : ";
cin>>Price;
OS="Android";
}
void PrintPhoneDetails()
{
setdetails_An();
cout<<"\n Brand : "<<Brand;
cout<<"\n OS : "<<OS;
cout<<"\n Display : "<<Display_type;
cout<<"\n Price : $ "<<Price;
}
};
Output:
Brand : Oneplus
OS : Android
Display : OLED
Price : $ 799
Brand : Apple
OS : iOS
Display : OLED
Price : $ 999
Aim:
To write a C++ program that implements dynamic binding using pure virtual function.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the brand name, display type and the price of an Android smartphone, and
assign “Android” for the OS variable.
Step 4: Display the detailed information of the Android smartphone.
Step 5: Get the display type and the price of an Apple smartphone and assign “Apple” and
“iOS” for the brand and OS variables.
Step 6: Display the detailed information of the Apple smartphone.
Step 7: End.
Program:
# include <iostream>
using namespace std;
class SmartPhone
{
public:
string Brand, OS, Display_type;
int Price;
virtual void PrintPhoneDetails()=0;
};
class Android: virtual public SmartPhone
{
public:
void setdetails_An()
{
cout<<"\n Enter the details of Android Smartphone :";
cout<<"\n Enter the Brand name : ";
cin>>Brand;
cout<<"\n Enter the Display type : ";
cin>>Display_type;
cout<<"\n Enter the price(in $) : ";
cin>>Price;
OS="Android";
}
void PrintPhoneDetails()
{
setdetails_An();
cout<<"\n Brand : "<<Brand;
cout<<"\n OS : "<<OS;
cout<<"\n Display : "<<Display_type;
cout<<"\n Price : $ "<<Price;
}
};
class Apple: virtual public SmartPhone
{
public:
void setdetails_Ap()
{
cout<<"\n Enter the details of Apple Smartphone :";
cout<<"\n Enter the Display type : ";
cin>>Display_type;
cout<<"\n Enter the price(in $) : ";
cin>>Price;
OS="iOS";
Brand="Apple";
}
void PrintPhoneDetails()
{
setdetails_Ap();
cout<<"\n Brand : "<<Brand;
cout<<"\n OS : "<<OS;
cout<<"\n Display : "<<Display_type;
cout<<"\n Price : $ "<<Price;
}
};
int main()
{
Android obj1;
Apple obj2;
SmartPhone *C1,*C2;
C1=&obj1;
C2=&obj2;
C1->PrintPhoneDetails();
cout<<"\n\n";
C2->PrintPhoneDetails();
return 0;
}
Output:
Brand : Samsung
OS : Android
Display : OLED
Price : $ 1299
Brand : Apple
OS : iOS
Display : OLED
Price : $ 1399
Result:
The C++ programs to perform dynamic binding were implemented successfully.
Ex.No: 8
Programs using class and function templates
Aim:
To write a C++ program with function templates.
Algorithm:
Step 1: Start
Step 2: Declare the headers.
Step 3: Initialize the variables and print the values.
Step 4: Swap the values using the template function.
Step 5: Display the variable after swap.
Step 6: Repeat 3-5 for other data types.
Step 7: End.
Program:
# include <iostream>
using namespace std;
template <typename T>
void Swap(T x, T y)
{
T t=x;
x=y;
y=t;
cout<<"\n Inside swap(): x="<<x<<" y="<<y;
}
int main()
{
char x='A',y='B';
cout<<"\n Before swap(): x="<<x<<" y="<<y;
Swap<char>(x,y);
cout<<"\n After swap() : x="<<x<<" y="<<y;
int X=10,Y=20;
cout<<"\n Before swap(): x="<<X<<" y="<<Y;
Swap<int>(X,Y);
cout<<"\n After swap() : x="<<X<<" y="<<Y;
return 0;
}
Output:
Aim:
To write a C++ program with class template.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Show the list of choices, and get the choice.
Step 4: Get the array elements of required data types.
Step 5: Get the search element.
Step 6: Perform the search and print the index of the search element in the array.
Step 7: End.
Program:
# include <iostream>
using namespace std;
template <class T>
class Search
{
public:
T arr[50];
T s_ele;
int n;
void Search_ele()
{
int c=0;
for(int i=0;i<n;i++)
{
if(arr[i]==s_ele)
{
cout<<"\n Index of "<<s_ele<<" is "<<i;
c=1;
break;
}
}
if(c==0)
cout<<"\n "<<s_ele<<" is not present in the array.";
}
void get()
{
cout<<"\n Enter no. of elements : ";
cin>>n;
cout<<"\n Enter the all the values(space separated prefered) : ";
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"\n Enter search element : ";
cin>>s_ele;
}
};
int main()
{
int n,ch;
cout<<"\n Enter the type(1.int 2.float 3.char 4.double) : ";
cin>>ch;
switch(ch)
{
case 1: Search<int> a;
a.get();
a.Search_ele();
break;
case 2: Search<float> b;
b.get();
b.Search_ele();
break;
case 3: Search<char> c;
c.get();
c.Search_ele();
break;
case 4: Search<double> d;
d.get();
d.Search_ele();
break;
}
return 0;
}
Output:
Index of w is 3
Result:
The C++ programs with class and function templates were implemented successfully.
Ex.No: 9
Experiment with sequential file access
Aim:
To write a C++ program with sequential file access.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Get the name of the file and the contents to be written.
Step 4: Write the contents to the file.
Step 5: Read the file, and display the contents in it.
Step 6: End.
Program:
# include <fstream>
# include <iostream>
using namespace std;
void put(char to[])
{
ofstream fout(to);
char s[1000];
cout<<"\n Enter the string to written to the file :\n ";
cin.getline(s,1000,'\n');
fout<<s;
fout.close();
}
void display(char from[])
{
ifstream fin(from);
char s[1000];
fin.getline(s,1000);
cout<<"\n The contents of file "<<from<<" are :";
cout<<"\n\n "<<s;
fin.close();
}
int main()
{
char file[100];
cout<<"\n Enter the file name : ";
cin.getline(file,100,'\n');
put(file);
display(file);
return 0;
}
Output:
File.txt:
Result:
The C++ program with sequential file access was implemented successfully.
Ex.No: 10
Experiment with random access file
Aim:
To write a C++ program with random access file.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Show the list of options, and get the user’s choice.
Step 4: If the choice is 1, add a record to the file.
Step 5: If the choice is 2, display a record from the file.
Step 6: If the choice is 3, update a record in the file.
Step 7: If the choice is 4, display all the records in the file.
Step 8: Repeat 3-7, if the user wants to continue.
Step 9: End.
Program:
# include <iostream>
# include <fstream>
# include <iomanip>
# include <stdlib.h>
using namespace std;
class student
{
int id;
char grade;
public:
void getv()
{
cout<<"\n Enter the ID : ";
cin>>id;
cout<<"\n Enter the Grade : ";
cin>>grade;
}
int p_id()
{
return id;
}
void putv()
{
cout<<"\n ID : "<<id;
cout<<"\n GRADE : "<<grade;
}
void up_grade()
{
cout<<"\n Enter the new grade : ";
cin>>grade;
}
void disp()
{
cout<<"\n "<<setw(2)<<id;
cout<<" "<<setw(3)<<grade;
}
};
void add_con()
{
student s;
ofstream fout("student.dat",ios::app);
cout<<"\n Enter student details : ";
s.getv();
fout.write((char*)&s,sizeof(s));
cout<<"\n Record added successfully";
fout.close();
}
void read_con()
{
student s;
int t_id,c=0;
ifstream fin;
fin.open("student.dat",ios::in);
cout<<"\n Enter the ID of the student : ";
cin>>t_id;
while(!fin.eof())
{
fin.read((char*)&s,sizeof(s));
if(t_id==s.p_id())
{
cout<<"\n Student details :";
s.putv();
c++;
break;
}
}
if(c==0)
cout<<"\n No record found";
fin.close();
}
void up_con()
{
student s;
int t_id,c=0,pos;
fstream f("student.dat",ios::in|ios::out);
cout<<"\n Enter the ID of the student : ";
cin>>t_id;
while(!f.eof())
{
f.read((char*)&s,sizeof(s));
if(t_id==s.p_id())
{
s.up_grade();
f.seekg(-sizeof(s),ios::cur);
f.write((char*)&s,sizeof(s));
cout<<"\n Record updated successfully";
c++;
break;
}
}
if(c==0)
cout<<"\n No record found";
f.close();
}
int count()
{
ifstream fin("student.dat",ios::ate);
int pos,c;
student s;
pos=fin.tellg();
c=(pos+1)/sizeof(s);
return c;
}
void disp_con()
{
student s;
int i,n;
ifstream fin("student.dat");
cout<<"\n Showing "<<count()<<" records : ";
cout<<"\n ID GRADE";
n=count();
for(i=0;i<n;i++)
{
fin.read((char*)&s,sizeof(s));
s.disp();
}
fin.close();
}
int main()
{
int n,ch;
do
{
cout<<"\t\t\t\t MENU";
cout<<"\n\t\t\t\t ";
cout<<"\n\n 1. Add Record";
cout<<"\n 2. Display a Record(using ID)";
cout<<"\n 3. Update a Record";
cout<<"\n 4. Display all Records";
cout<<"\n !!!NOTE:- Make sure that atleast one record has been added before
performing operations 2-4.!!!";
cout<<"\n\n Enter your choice : ";
cin>>n;
switch(n)
{
case 1: add_con();
break;
case 2: read_con();
break;
case 3: up_con();
break;
case 4: disp_con();
break;
default:cout<<"\n Invalid command. ...Returning to menu\n ";
}
cout<<"\n Do you want to continue(1-yes) : ";
cin>>ch;
}while(ch==1);
return 0;
}
Output:
MENU
1. Add Record
2. Display a Record(using ID)
3. Update a Record
4. Display all Records
!!!NOTE:- Make sure that atleast one record has been added before performing operations 2-
4.!!!
1. Add Record
2. Display a Record(using ID)
3. Update a Record
4. Display all Records
!!!NOTE:- Make sure that atleast one record has been added before performing operations 2-
4.!!!
1. Add Record
2. Display a Record(using ID)
3. Update a Record
4. Display all Records
!!!NOTE:- Make sure that atleast one record has been added before performing operations 2-
4.!!!
1. Add Record
2. Display a Record(using ID)
3. Update a Record
4. Display all Records
!!!NOTE:- Make sure that atleast one record has been added before performing operations 2-
4.!!!
Showing 2 records :
ID GRADE
1 O
2 A
Do you want to continue(1-yes) : 0
Result:
The C++ program with random access file was implemented successfully.
Ex.No: 11
Programs using exception handling mechanisms
Aim:
To write a C++ program to demonstrate exception handling.
Algorithm:
Step 1: Start.
Step 2. Declare the headers.
Step 3: Get the dividend and divisor from the user.
Step 4: Compute the quotient within a try block.
Step 5: Display the quotient, if there was no exception.
Step 6: Print the message if there was any exception.
Step 7: End.
Program:
# include <iostream>
# include <exception>
using namespace std;
int main()
{
int dividend,divisor;
float quotient;
cout<<"\n Enter the dividend : ";
cin>>dividend;
cout<<"\n Enter the divisor : ";
cin>>divisor;
try
{
if(divisor==0)
throw " Divide by zero";
quotient=dividend/(divisor*1.0);
cout<<"\n The quotient is "<<quotient;
}
catch(const char* c)
{
cout<<c<<endl;
}
return 0;
}
Output:
Output:
Result:
The C++ programs demonstrating exception handling were implemented successfully.
Ex.No: 12
Develop programs using STL
Aim:
To write C++ program using Standard Template Library.
Algorithm:
Step 1: Start.
Step 2: Declare the headers.
Step 3: Declare a vector of integer type.
Step 4: Get the number of values to be added to the vector.
Step 5: Get the array values and append it into the array.
Step 6: Perform sort on the intefer array.
Step 7: Display the sorted array.
Step 8: End.
Program:
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
int main()
{
vector<int> arr;
int n,i;
cout<<"Enter the no. of elements : ";
cin>>n;
cout<<"\nEnter the elements : ";
for(i=0;i<n;i++)
{
int val;
cin>>val;
arr.push_back(val);
}
sort(arr.begin(),arr.end());
cout<<"\nSorted array : ";
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
Output:
Result:
The C++ program using Standard Template Library was implemented successfully.