balaguruswamy c++ unit 5

http://www.htlwebs.com
e- balaguruswamy c++ solution
PROGRAMMING EXERCISE – 5
1. Define a class String that could work as a user defined string type. Include constructors that will
enable us to create an uninitialized string and also to initialize an object with a string constant at the
time of creation.
#include<iostream>
#include<cstring>
using namespace std;
class String
{
char*str;
int length;
public:
String()
{
length=0;
str=new char[length+1];
}
String(char*x)
{
length=strlen(x);
str= new char[length+1];
strcpy(str, x);
}
void concat(String &x,String &y)
{
length=x.length +y.length;
delete str;
str=new char[length+1];
strcpy(str,x.str);
strcat(str, y.str);
}
void disp()
{
cout<<"\nlength is\n"<<length;
cout<<"\nstring is \n"<<str;
}
};
int main()
{
char s1[30],s2[40];
gets(s1);
gets(s2);
String s(s1),t(s2),u,v;
u.concat(s,t);
u.disp();
getch();
}
2. Design a system using a class called books with suitable member functions and constructors. Use
new operator in constructors to allocate memory space required.
#include<conio.h>
#include<iostream>
#include<cstring>
using namespace std;
http://www.htlwebs.com
class book
{
char *author,*title,*publisher;
int price;
int copies;
public:
book()
{
author= new char[30];
title= new char[30];
publisher= new char[30];
}
void get_info()
{
cout<<"\nenter the author,title and publisher res."<<endl;
cin>>author;
cin>>title;
cin>>publisher;
cout<<"enter the price of book";
cin>>price;
cout<<"enter the number of copies ";
cin>>copies;
}
void disp()
{
cout<<"Author "<<author;
cout<<"\nTitle "<<title;
cout<<"\nPublisher "<<publisher;
cout<<"\nPrice is "<<price;
cout<<"\nno. of copies available"<<copies<<endl;
}
void sold(int x)
{
if(x>copies)
cout<<"out of stock";
else
{
cout<<"Thank u!!! " ;
copies-=x;
}
}
int searcher(char*x,char*y)
{
if((!strcmp(x,title))&&(!strcmp(y,author))==1)
return copies;
else
return 0;
}
};
int main()
{ int i=1,j=0,k,x,l=1;
char title[30],author[30];
book obj[10];
cout<<"enter the books in stock";
while(i==1)
e- balaguruswamy c++ solution
http://www.htlwebs.com
e- balaguruswamy c++ solution
{
obj[j].get_info();
cout<<"press 1 to enter more ";
cin>>i;
j++;
}
while(l==1)
{
cout<<"\nenter the title and author of book";
cin>>title;
cin>>author;
i=0;
while(i<j)
{
if(k=(obj[i].searcher(title,author)))
{
cout<<"book available in object "<<i+1<<endl;
break;
}
i++;
}
if(k==0)
cout<<"no copies available\n";
else
{
obj[i].disp();
cout<<"how many copies u want";
cin>>x;
obj[i].sold(x);
}
cout<<"press 1 to buy any other book";
cin>>l;
}
getch();
return 0;
}
3. Improve the system design in question 2 to incorporate the following features:a. The price of the books should be updated as and when required.
Use a private member function to implement this.
b. The stock value of each book should be automatically updated as soon as a transaction is
completed.
c. The number of successful and unsuccessful transactions should be recorded for the purpose of
statistical analysis. Use static data members to keep count of transactions.
#include<conio.h>
#include<iostream>
#include<cstring>
using namespace std;
class book
{
char *author,*title,*publisher;
int price;
int copies;
static int i;
static int j;
http://www.htlwebs.com
void update()
{ cout<<"enter new price";
cin>>price;
}
public:
book()
{
author= new char[30];
title= new char[30];
publisher= new char[30];
}
void get_info()
{
cout<<"\nenter the author,title and publisher res."<<endl;
cin>>author;
cin>>title;
cin>>publisher;
cout<<"enter the price of book";
cin>>price;
cout<<"enter the number of copies ";
cin>>copies;
}
void disp()
{
cout<<"Author "<<author;
cout<<"\nTitle "<<title;
cout<<"\nPublisher "<<publisher;
cout<<"\nPrice is "<<price;
cout<<"\nno. of copies available"<<copies<<endl;
}
void sold(int x)
{
if(x>copies)
{
cout<<"out of stock\n";
i++;
}
else
{
j++;
cout<<"Thank u!!! \n" ;
copies-=x;
}
}
int searcher(char*x,char*y)
{
if((!strcmp(x,title))&&(!strcmp(y,author))==1)
return copies;
else
return 0;
}
int searched(char*x,char*y,char*pass)
{
if(!strcmp(pass,"password"))
{
e- balaguruswamy c++ solution
http://www.htlwebs.com
if((!strcmp(x,title))&&(!strcmp(y,author))==1)
{
update();
return copies;
}
else{
return 0;
}
}
else{
return -1;
}
}
static void transaction()
{
cout<<"\nsuccessful transactions "<<j;
cout<<"\nunsuccessful transactions "<<i;
}
static void inc()
{
i++;
}
~book()
{
cout<<"\ndestructor called";
delete(author);
delete(title);
delete(publisher);
getch();
}
};
int book :: i;
int book :: j;
int main()
{
int i=1,j=0,k=0,x,l=1,t;
char title[30],author[30];
char*pass;
pass=new char[10];
book obj[10];
cout<<"enter the books in stock";
while(i==1)
{
obj[j].get_info();
cout<<"press 1 to enter more ";
cin>>i;
j++;
}
while(l==1)
{
cout<<"1. UPDATE PRICE\n";
cout<<"2.PURCHASE BOOK\n";
cout<<"3.to see transaction history\n";
cout<<"enter the desired option\n";
cin>>t ;
e- balaguruswamy c++ solution
http://www.htlwebs.com
i=0;
if(t==2)
{
cout<<"\nenter the title and author of book";
cin>>title;
cin>>author;
}
else if(t==1)
{
cout<<"enter the password";
cin>>pass;
cout<<"\nenter the title and author of book";
cin>>title;
cin>>author;
}
else if(t==3)
{
book::transaction();
}
while(i<j)
{
if(t==2)
{
if(k=(obj[i].searcher(title,author)))
{
cout<<"book available in object "<<i+1<<endl;
break;
}
}
else if(t==1)
{
if(k=obj[i].searched(title,author,pass))
{
if(k==-1)
cout<<"wrong password";
else
cout<<"book available in object "<<i+1<<endl;
break;
}
}
i++;
}
if(k==0&&t<3)
{
if(t==2){
book::inc();}
cout<<"no copies available\n";
}
else
{ if(t==2)
{
obj[i].disp();
cout<<"how many copies u want";
cin>>x;
obj[i].sold(x);
e- balaguruswamy c++ solution
http://www.htlwebs.com
e- balaguruswamy c++ solution
}
}
cout<<"\npress 1 to manuplate more";
cin>>l;
}
cout<<"Thanku for visiting";
getch();
return 0;
}
4. Modify the program of question 3 to demonstrate the use of pointers to access the members.
#include<conio.h>
#include<iostream>
#include<cstring>
using namespace std;
class book
{
char *author,*title,*publisher;
int price;
int copies;
static int i;
static int j;
void update()
{ cout<<"enter new price";
cin>>price;
}
public:
book()
{
author= new char[30];
title= new char[30];
publisher= new char[30];
}
void get_info()
{
cout<<"\nenter the author,title and publisher res."<<endl;
cin>>this->author;
cin>>this->title;
cin>>this->publisher;
cout<<"enter the price of book";
cin>>this->price;
cout<<"enter the number of copies ";
cin>>this->copies;
}
void disp()
{
cout<<"Author "<<this->author;
cout<<"\nTitle "<<this->title;
cout<<"\nPublisher "<<this->publisher;
cout<<"\nPrice is "<<this->price;
cout<<"\nno. of copies available"<<this->copies<<endl;
}
void sold(int x)
{
if(x>copies)
http://www.htlwebs.com
{
cout<<"out of stock\n";
i++;
}
else
{
j++;
cout<<"Thank u!!! \n" ;
copies-=x;
}
}
int searcher(char*x,char*y)
{
if((!strcmp(x,title))&&(!strcmp(y,author))==1)
return copies;
else
return 0;
}
int searched(char*x,char*y,char*pass)
{
if(!strcmp(pass,"password"))
{
if((!strcmp(x,title))&&(!strcmp(y,author))==1)
{
update();
return copies;
}
else{
return 0;
}
}
else{
return -1;
}
}
static void transaction()
{
cout<<"\nsuccessful transactions "<<j;
cout<<"\nunsuccessful transactions "<<i;
}
static void inc()
{
i++;
}
~book()
{
cout<<"\ndestructor called";
delete(author);
delete(title);
delete(publisher);
getch();
}
};
int book :: i;
int book :: j;
e- balaguruswamy c++ solution
http://www.htlwebs.com
int main()
{
int i=1,j=0,k=0,x,l=1,t;
char title[30],author[30];
char*pass;
pass=new char[10];
book* obj;
obj=new book[10];
cout<<"enter the books in stock";
while(i==1)
{
obj[j].get_info();
cout<<"press 1 to enter more ";
cin>>i;
j++;
}
while(l==1)
{
cout<<"1. UPDATE PRICE\n";
cout<<"2.PURCHASE BOOK\n";
cout<<"3.to see transaction history\n";
cout<<"enter the desired option\n";
cin>>t ;
i=0;
if(t==2)
{
cout<<"\nenter the title and author of book";
cin>>title;
cin>>author;
}
else if(t==1)
{
cout<<"enter the password";
cin>>pass;
cout<<"\nenter the title and author of book";
cin>>title;
cin>>author;
}
else if(t==3)
{
book::transaction();
}
while(i<j)
{
if(t==2)
{
if(k=(obj[i].searcher(title,author)))
{
cout<<"book available in object "<<i+1<<endl;
break;
}
}
else if(t==1)
{
if(k=obj[i].searched(title,author,pass))
e- balaguruswamy c++ solution
http://www.htlwebs.com
{
if(k==-1)
cout<<"wrong password";
else
cout<<"book available in object "<<i+1<<endl;
break;
}
}
i++;
}
if(k==0&&t<3)
{
if(t==2){
book::inc();}
cout<<"no copies available\n";
}
else
{ if(t==2)
{
obj[i].disp();
cout<<"how many copies u want";
cin>>x;
obj[i].sold(x);
}
}
cout<<"\npress 1 to manuplate more";
cin>>l;
}
cout<<"Thanku for visiting";
getch();
return 0;
}
e- balaguruswamy c++ solution