OOPs With C++ Laboratory Manual 230516 123658
OOPs With C++ Laboratory Manual 230516 123658
3. Write a C++ Program to call a C function using an extern “C” linkage directive.Use
compound statement linkage directive for #include<math.h>.
4. Write a C++ Program to accept a line of text and count the number of
words,characters and digits in it.
5. Write a C++ Program to store two binary numbers in arrays and perform bitwise
AND, OR and XOR operations on these two numbers.
6.Create two classes D1 and D2 which store the value of distances. D1 stores distances in
meters and centimeters and D2 in feet and inches. Write a program to add objects of
two classes D1 and D2 and the display the results in feet and inches using friend
function.
7. Given that an EMPLOYEE class contains the data members like E_Number, E_Name,
Basic_salary, DA, HRA, Net_salary and the member functions like Read(),
Calculate_Net_Sal(), and Display(). Write a C++ Program to read the data of N
Employees and Compute the Net_Salary of each employee.
8. Write a C++ program to overload the function Search() to search an integer key value
and a key value of type double.
11. Write a C++ Program to create a class called DATE. Accept two valid dates in the
form of DD/MM/YYYY. Implement the following by overloading the operators –
(minus) and + (Plus). Display the results by overloading the operator << after every
operation.
a) No_of _days = D1-D2
b) D1=D1 + No_of _days
where d1 and d2 are date objects, d1>d2 and No_of_days is an integer.
12. Write a C++ program to create a class called MATRIX using a two-dimensional array
of integers. Implement the following by overloading the operator == which checks the
compatibility of two matrices to be added and subtracted. Perform the following by
overloading + and - operators. Display the result by overloading the operator <<.
13. Write a C++ program to create a class called STUDENT with data members
USN,Name and Age. Using inheritance, create the classes UGSTUDENT and
PGSTUDENT having fields as Semester,Fees and Stipend. Enter the data for at least
5 students. Find the semester wise average age for all UG and PG students
separately.
14. Write a C++ program to create a base class Student, from this inherit a new class
called Exams, containing Marks1, Marks2 and Marks3 as its data members. Also
create another class called Sports having Sports_grade as its data member. Now,
create yet another class but a derived class of Exams and Sports classes and call it
as Awards. Use appropriate member functions in all classes. You can use the Regno,
Name, Semester and Branch as the members of the base class.
15. Write a C++ program to create a file called File1.txt and print its contents in the
reverse order to the File2.txt.
1. Raising a number n to a power p is the same as multiplying n by itself p times. Write
a function called power ( ) that takes a double value for n and an int value for p, and
returns the result as double value. Use a default argument of 2 for p, so that if this
argument is omitted, the number will be squared. Write a main ( ) function that gets
values from the user to test this function.
#include<iostream>
using namespace std;
int main()
{
double n,result;
int p;
cout<<"enter n and p value"<<endl;
cin>>n>>p;
result=power(n,p);
cout<<n<<" raised to the power "<<p<<" is "<<result<<endl;
result=power(n);
cout<<" By default argument"<<n<<" power 2 is "<<result<<endl;
return 0;
}
2.Write a C++ Program to accept an alphabet and check whether it is a vowel or a
consonant. If it is a vowel, return its predecessor, else its successor. Use
call-by-reference with reference arguments.
#include <iostream>
using namespace std;
int main()
{
char alphabet, result;
cout << "Enter an alphabet: ";
cin >> alphabet;
if(!isalpha(alphabet))
cout<<"Entered character is not a alphabet"<<endl;
else
checkAlphabet(alphabet, result);
return 0;
}
OUTPUT
3. Write a C++ Program to call a C function using an extern “C” linkage directive.Use
compound statement linkage directive for #include<math.h>.
#include <iostream>
#include <math.h>
Using namespace std;
extern "C" {
double my_sqrt(double x) {
return sqrt(x);
}
}
int main() {
double x = 100;
double y = my_sqrt(x);
cout << "Square root of " << x << " is " << y << endl;
return 0;
4. Write a C++ Program to accept a line of text and count the number of
words,characters and digits in it.
#include <iostream>
#include <string>
#include <cctype>
int main()
{
string line;
int wordCount = 0, charCount = 0, digitCount = 0;
return 0;
}
OUTPUT
5. Write a C++ Program to store two binary numbers in arrays and perform bitwise
AND, OR and XOR operations on these two numbers.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of bits: ";
cin >> n;
return 0;
}
6.Create two classes D1 and D2 which store the value of distances. D1 stores distances
in meters and centimeters and D2 in feet and inches. Write a program to add objects
of two classes D1 and D2 and the display the results in feet and inches using friend
function.
#include <iostream.h>
using namespace std;
class D1 {
private:
int meters;
int centimeters;
public:
void get_data()
{
cout<<"Enter distance in meters and centimeters: ";
cin>>meters>>centimeters;
}
friend void add(D1 obj1, D2 obj2);
};
class D2 {
private:
int feet;
int inches;
public:
void get_data()
{
cout<<"Enter distance in feet and inches: ";
cin>>feet>>inches;
}
friend void add(D1 obj1, D2 obj2);
};
int main() {
D1 d1;
D2 d2;
d1.get_data();
d2.get_data();
// Add the two objects and display the result
add(d1, d2);
return 0;
}
7. Given that an EMPLOYEE class contains the data members like E_Number,
E_Name, Basic_salary, DA, HRA, Net_salary and the member functions like Read(),
Calculate_Net_Sal(), and Display(). Write a C++ Program to read the data of N
Employees and Compute the Net_Salary of each employee.
#include <iostream.h>
using namespace std;
class employee
{
int emp_number;
char emp_name[20];
float emp_basic;
float emp_da;
float emp_hra;
float emp_net_sal;
public:
void read();
float calculate_net_salary();
void display();
};
int main()
{
int num_employees;
int i;
cout << "How many employees do you want to enter? ";
cin >> num_employees;
//employee* emp = new employee[num_employees];
employee emp[num_employees];
#include <iostream.h>
using namespace std;
int main() {
int intArr[] = { 10, 20, 30, 40, 50 };
double doubleArr[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
return 0;
}
9. Write a C++ program to find the following using Function Template
a) Successor value of any input of type integer, float, char and double.
b) Sum of all the elements of an array of integers or floats or doubles.
#include <iostream.h>
using namespace std;
int main() {
// Finding the successor value of any input
cout << "Successor of 5: " << successor(5) << endl;
cout << "Successor of 5.5: " << successor(5.5) << endl;
cout << "Successor of 'a': " << successor('a') << endl;
return 0;
}
OUTPUT
10.Write a C++ Program to create a class as COMPLEX and implement the following by
overloading the function ADD() which returns the Complex numbers
a) ADD(C1, C2); C1 is an integer ; C2 is a Complex number.
b) ADD(C1, C2); C1 and C2 are Complex numbers.
#include<iostream>
#include <math.h>
using namespace std;
class COMPLEX {
private:
float real;
float imag;
public:
COMPLEX() { // default constructor
real = 0;
imag = 0;
}
int main() {
COMPLEX C1(4, 5);
COMPLEX C2(3, -2);
COMPLEX C3;
#include<iostream.h>
#include<conio.h>
#include<process.h>
class date
{
int flag,dd,mm,yy;
public:
date(int d,int m,int y)
{
dd=d;mm=m;yy=y;
if((y%4)==0)
flag=1;
else
{
flag=0;
if(m==2 && d>28)
{
cout<<"Error since not a leap year";
getch();
exit(0);
}
}
}
int return_date(date d);
int operator -(date d2);
date operator +(int n);
friend ostream& operator <<(ostream& out,date d1);
};
int a[20]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[20]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int date::operator -(date d2)
{
int a1,a2,x=0;
date d1(dd,mm,yy);
if(d1.dd==d2.dd&&d1.mm==d2.mm&&d1.yy==d2.yy)
return x;
if(d1.dd<d2.dd&&d1.mm==d2.mm&&d1.yy==d2.yy)
{
cout<<"Error since 2nd date is greater than 1st date";
getch();
exit(0);
}
if(d1.mm<d2.mm&&d1.yy==d2.yy)
{
cout<<"Error since 2nd date is greater than 1st date";
getch();
exit(0);
}
if(d1.yy<d2.yy)
{
cout<<"Error since 2nd date is greater than 1st date";
getch();
exit(0);
}
a1=return_date(d1);
a2=return_date(d2);
for(int i=d1.yy-1;i>d2.yy;i--)
{
if(i%yy==0)
x=x+366;
else
x=x+365;
}
if(d1.yy==d2.yy)
x=x+a1-a2;
else
{
x=x+a1;
if(d2.yy%4==0)
x=x+(366-a2);
else
x=x+(365-a2);
}
return x;
}
int date::return_date(date d)
{
int int_val=d.dd;
if((d.flag==1)&&(d.mm>2))
{
for(int i=0;i<d.mm;i++)
int_val=int_val+a[i];
int_val++;
}
else
{
for(int i=0;i<d.mm;i++)
int_val=int_val+a[i];
}
return int_val;
}
date date::operator +(int n)
{
date d(dd,mm,yy);
for(int i=0;i<n;i++)
{
d.dd++;
if(d.yy%4==0)
{
d.flag=1;
if(d.dd>b[d.mm])
{
d.dd=1;
d.mm++;
}
}
else
{
d.flag=0;
if(d.dd>a[d.mm])
{
d.dd=1;
d.mm++;
}
}
if(d.mm>12)
{
d.mm=1;
d.yy++;
}
}
return d;
}
ostream& operator <<(ostream& out,date d1)
{
out << d1.dd<<"-"<<d1.mm<<"-"<<d1.yy;
return out;
}
int main()
{
int d,m,y,no_of_days;
clrscr();
cout<<"Enter a valid date(dd mm yyyy):\n";
cin>>d>>m>>y;
if(d>b[m]||m>12)
{
cout<<"Error";
getch();
exit(0);
}
date d1(d,m,y);
cout<<"Enter a valid date which is lesser than the first date\n";
cin>>d>>m>>y;
if(d>b[m]||m>12)
{
cout<<"Error";
getch();
exit(0);
}
date d2(d,m,y);
no_of_days=d1-d2;
cout<<"Difference b/w two dates in days :"<<no_of_days<<endl;
cout<<"Enter a no of days to be added to first date:\n";
cin>>no_of_days;
d1=d1+no_of_days;
cout<<"The new date is:"<<d1<<endl;
getch();
return 0;
}
12. Write a C++ program to create a class called MATRIX using a two-dimensional
array of integers. Implement the following by overloading the operator == which
checks the compatibility of two matrices to be added and subtracted. Perform the
following by overloading + and - operators. Display the result by overloading the
operator <<.
#include<iostream>
using namespace std;
class matrix
{
private:long m[5][5];
int row;int col;
public:void getdata();
int operator ==(matrix);
matrix operator+(matrix);
matrix operator-(matrix);
friend ostream & operator << (ostream &,matrix &);
};
/* function to check whether the order of matrix are same or not */
int matrix::operator==(matrix cm)
{
if(row==cm.row && col==cm.col)
{
return 1;
}
return 0;
}
/* function to read data for matrix*/
void matrix::getdata()
{
cout<<"enter the number of rows\n";
cin>>row;
cout<<"enter the number of columns\n";
cin>>col;
cout<<"enter the elements of the matrix\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>m[i][j];
}
}
}
/* function to add two matrix */
matrix matrix::operator+(matrix am)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]+am.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to subtract two matrix */
matrix matrix::operator-(matrix sm)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]-sm.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to display the contents of the matrix */
ostream & operator <<(ostream &fout,matrix &d)
{
for(int i=0;i<d.col;i++)
{
for(int j=0;j<d.col;j++)
{
fout<<d.m[i][j];
cout<<" ";
}
cout<<endl;
}
return fout;
}
/* main function */
int main()
{
matrix m1,m2,m3,m4;
m1.getdata();
m2.getdata();
if(m1==m2)
{
m3=m1+m2;
m4=m1-m2;
cout<<"Addition of matrices\n";
cout<<"the result is\n";
cout<<m3;
cout<<"subtraction of matrices\n";
cout<<"The result is \n";
cout<<m4;
}
else
{
cout<<"order of the input matrices is not identical\n";
}
}
13. Write a C++ program to create a class called STUDENT with data members
USN,Name and Age. Using inheritance, create the classes UGSTUDENT and
PGSTUDENT having fields as Semester,Fees and Stipend. Enter the data for at
least 5 students. Find the semester wise average age for all UG and PG students
separately.
#include<iostream>
using namespace std;
class student
{
public: int reg,age;
char name[20];
void read_data();
};
/* main function */
int main()
{
ugstudent ug[20];
pgstudent pg[20];
int i,n,m;
float average;
cout<<"\nEnter the no. of entries in the ugstudent class:";
cin>>n;
for(i=1;i<=n;i++)
ug[i].read_data();
for(int sem=1;sem<=8;sem++)
{
float sum=0;
int found=0,count=0;
for(i=1;i<=n;i++)
{
if(ug[i].sem==sem)
{
sum=sum+ug[i].age;
found=1;
count++;
}
}
if(found==1)
{
average=sum/count;
cout<<"\nAverage of age of sem "<<sem<<" is "<<average;
}
}
cout<<"\nEnter the no. of entries of pgstudent class:";
cin>>n;
for(i=1;i<=n;i++)
pg[i].read_data();
for(sem=1;sem<=8;sem++)
{
float sum=0;
int found=0,count=0;
for(i=1;i<=n;i++)
{
if(pg[i].sem==sem)
{
sum=sum+pg[i].age;
found=1;
count++;
}
}
if(found==1)
{
average=sum/count;
cout<<"\nAverage of age of sem "<<sem<<" is "<<average;
}
}
}
14. Write a C++ program to create a base class Student, from this inherit a new
class called Exams, containing Marks1, Marks2 and Marks3 as its data members.
Also create another class called Sports having Sports_grade as its data member.
Now, create yet another class but a derived class of Exams and Sports classes and
call it as Awards. Use appropriate member functions in all classes. You can use the
Regno, Name, Semester and Branch as the members of the base class.
#include <iostream>
using namespace std;
class Student {
public:
int Regno;
char Name[20];
int Semester;
char Branch[20];
void GetData() {
cout << "Enter Regno: ";
cin >> Regno;
cout << "Enter Name: ";
cin >> Name;
cout << "Enter Semester: ";
cin >> Semester;
cout << "Enter Branch: ";
cin >> Branch;
}
};
int main() {
Awards a1;
a1.GetData();
a1.GetMarks();
a1.GetSports();
a1.Display();
return 0;
}
15. Write a C++ program to create a file called File1.txt and print its contents in the
reverse order to the File2.txt.
#include<bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
// Input file stream object to
// read from file.txt
ifstream in("file1.txt");
// Output file stream object to
// write to file2.txt
ofstream f("file2.txt");
// Reading file.txt completely using
// END OF FILE eof() method
while(!in.eof())
{
// string to extract line from
// file.txt
string text;
// extracting line from file.txt
getline(in, text);
// Writing the extracted line in
// file2.txt
f<<text<<endl;
}
return 0; }