Professional Documents
Culture Documents
2. Templates are a feature of the C++ programming language that allows CO1 / C2
functions and classes to operate with generic types. This allows a function or
class to work on many different data types without being rewritten for each one.
Enlist the function adaptors.
A function adaptor is an instance of a class that adapts a global or member function so
3. CO1 / C2
that the function can be used as a function object. A function adaptor may also be
used to alter the behavior of a function
List down any two manipulators in C++.
Setw()
4. CO1 / C2
Setfill()
PART - B (4 × 6= 24 Marks)
Answer all questions
Debug in the following program.
(a) ifstream.infile(“DATA”);
(b) finl.getline(); //finl is input stream
(c) if(finl.eof() == 0) exit(1);
(d) close(f1);
(e) infile.open(argc);
(g) sfinout.open(file,ios::in |ios::out| ios::ate);
(f) The argument file must write as ” file” because this is string type.
Correction :
fstream sfinout;
sfinout.open (“file”, ios::inios::out, ios::ate);
#include <iostream.h>
void main()
{
cout.width(5);
cout << "99" << endl;
cout.setf(ios::left);
cout.width(5);
cout << "99" << endl;
cout.setf(ios::right);
cout << "99" << endl;
}
Write a program to read 10 numbers. Generate the exceptions for negative and
even numbers. Provide handlers to display appropriate messages.
#include <iostream>
using namespace std;
int main()
{
double a, b, c;
cout << "\n" << a << " * " << b << " = " << c << "\n\n";
return 0;
}
Apply class template concept for finding the scalar product for int and float type
vector.
#include <iostream>
#include <vector>
class Scalar_product
{
public:
Scalar_product(vector<double> a, vector<double> b);
};
double scalar_product(vector<double> a, vector<double> b)
9. { CO1 /C3
double product = 0;
for (int i = 0; i <= a.size()-1; i++)
for (int i = 0; i <= b.size()-1; i++)
product = product + (a[i])*(b[i]);
return product;
}
int main() {
cout << product << endl;
return 0;
}
PART - C (2 × 8 = 16 Marks)
Answer any Two Questions
10. Consider an example of book shop which sells books and video tapes. It is CO1 / C6
modeled by book and tape classes. These two classes are inherited from the
base class called media. The media class has common data members such as
title and publication. The book class has data members for storing a number of
pages in a book and tape class has the playing time in a tape. Each class will
have the member functions such as read ( ) and show ( ). In a base class, these
members have to be defined as virtual functions. Write a program which models
this class hierarchy and processes their objects.
#include
#include
#include
#include
#include
class book
{
char **author;
char **title;
float *price;
char **publisher;
int *stock_copy;
int size;
public:
book();
void book_detail(int i);
void buy(int i);
int search();
};
book :: book()
{
size=4;
author=new char*[80];
title=new char*[80];
publisher=new char*[80];
for(int i=0;i<size;i++)
{
author[i]=new char[80];
title[i]=new char[80];
publisher[i]=new char[80];
}
stock_copy=new int[size];
price=new float[size];
title[0]="object oriented programming with c++";
title[1]="programming in ANCI";
title[2]="electronic circuit theory";
title[3]="computer algorithm";
author[0]="balagurusamy";
author[1]="balagurusamy";
author[2]="boyelstade";
author[3]="shahani";
stock_copy[0]=200;
stock_copy[1]=150;
stock_copy[2]=50;
stock_copy[3]=80;
price[0]=120.5;
price[1]=115.75;
price[2]=140;
price[3]=180.5;
}
void book::book_detail(int i)
{
cout<<" *********book detail **********\n";
cout<<setw(12)<<"Title"<<setw(25)<<"Auth
or Name"
<<setw(18)<<"Stock copy\n";
cout<<setw(15)<<title[i]<<setw(16)<<auth
or[i]<<setw(15)
<<stock_copy[i]<<"\n";
}
int book::search()
{
char name[80],t[80];
cout<<"Enter author name : ";
gets(name);
cout<<"and title of book in small letter : ";
gets(t);
int count=-1;
int a,b;
for(int i=0;i<size;i++)
{
a=strcmp(name,author[i]);
b=strcmp(t,title[i]);
if(a==0 && b==0)
count=i;
}
return count;
}
void book::buy(int i)
{
if(i<0)
cout<<" This book is not available \n";
else
{
book_detail(i);
cout<<" How many copies of this book is required : ? ";
int copy; cin>>copy;
int remaining_copy;
if(copy<=stock_copy[i])
{
remaining_copy=stock_copy[i]-copy;
float total_price;
total_price=price[i]*copy;
cout<<"Total price = "<<total_price<<" TK\n";
}
else
cout<<" Sorry your required copies is not available \n";
}
}
int main()
{
book b1;
int result;
result=b1.search();
b1.buy(result);
return 0;
}
11. Create a class FLOAT that contains one float data member. The user requires CO1 / C6
all the four arithmetic operators so that they operate on the objects of FLOAT.
#include<iostream.h>
class FLOAT
{
float data;
public:
FLOAT(){};
FLOAT(float d)
{ data=d;}
FLOAT operator+(FLOAT f1);
FLOAT operator-(FLOAT f2);
FLOAT operator*(FLOAT f3);
FLOAT operator/(FLOAT f4);
void display();
};
FLOAT FLOAT::operator+(FLOAT f1)
{
FLOAT temp;
temp.data=data+f1.data;
return (temp);
}
FLOAT FLOAT::operator-(FLOAT f2)
{
FLOAT temp;
temp.data=data-f2.data;
return temp;
}
FLOAT FLOAT::operator*(FLOAT f3)
{
FLOAT temp;
temp.data=data*f3.data;
return temp;
}
FLOAT FLOAT::operator/(FLOAT f4)
{
FLOAT temp;
temp.data=data/f4.data;
return (temp);
}
void FLOAT:: display()
{
cout<<data<<"\n";
}
int main()
{
FLOAT F1,F2,F3,F4,F5,F6;
F1=FLOAT(2.5);
F2=FLOAT(3.1);
F3=F1+F2;
F4=F1-F2;
F5=F1*F2;
F6=F1/F2;
cout<<" F1 = ";
F1.display();
cout<<" F2 = ";
F2.display();
cout<<" F1+F2 = ";
F3.display();
cout<<" F1-F2 = ";
F4.display();
cout<<" F1*F2 = ";
F5.display();
cout<<" F1/F2= ";
F6.display();
return 0;
}