Design constructors for the classes designed in Programming Exercise 5.1 through 5.5 of Chapter 5.
Study on Constructor and then see solution of chapter 5.
Define a class String that could work as a user-defined string type. Include constructors that will enable us to create an uninitialized string:
String s1; // string with length 0
And also initialize an object with a string constant at the time of creation like
String s2(“Well done!”);
Include a function that adds two strings to make a third string. Note that the statement
S2 = s1;
will be perfectly reasonable expression to copy one string to another.
Write a complete program to test your class to see that it does the following tasks:
- (a) Creates uninitialized string objects.
- (b) Creates objects with string constants.
- (c) Concatenates two strings properly.
- (d) Displays a desired string object.
#include<iostream> #include<iomanip> using namespace std; class string { char *str; int length; public: string() { length = 0; str = new char [length + 1] ; } string(char *s); void concat(string &m,string &n); string(string &x); void display(); }; string::string(string &x) { length = x.length + strlen(x.str); str = new char[length + 1]; strcpy(str, x.str); } void string:: concat(string &m,string &n) { length=m.length+n.length; delete str; str=new char[length+1]; strcpy(str,m.str); strcat(str,n.str); } void string:: display() { cout<<str<<"\n"; } string::string(char *s) { length = strlen(s); str = new char[length + 1]; strcpy(str,s); } int main() { string s1; string s2(" Well done "); string s3(" Badly done "); s2.display(); s1.concat(s2,s3); s2=s3; s2.display(); s1.display(); return 0; }
OUTPUT
Well done
Badly done
Well done Badly done
A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not. If it is not, an appropriate message is displayed. If it is, then the system displays the book details and requests for the number of copies required. If the requested copies are available, the total cost of the requested copies is displayed; otherwise “Required copies not in stock” is displayed.
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<iostream> #include<string> #include<iomanip> using namespace std; 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)<<"Author Name" <<setw(18)<<"Stock copy\n"; cout<<setw(15)<<title[i]<<setw(16)<<author[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; }
OUTPUT
Enter author name : shahani
and title of book in small latter : computer algorithm
*********book detail *********
Title Author Name Stock copy
computer algorithm shahani 80
How many copies of this book is required : ? 78
Total price = 14079 TK
Improve the system design in Exercise 6.3 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<iostream> #include<string> #include<iomanip> using namespace std; class book { static int successful,unsuccessful; 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(); void showtransaction(); void showdetail(); void edit_price(); }; int book::successful=0; int book::unsuccessful=0; book :: book() { size=5; 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"; title[4]="complete solution of balagurusamy(c++)"; author[0]="balagurusamy"; author[1]="balagurusamy"; author[2]="boyelstade"; author[3]="shahani"; author[4]="abdus sattar"; stock_copy[0]=200; stock_copy[1]=150; stock_copy[2]=50; stock_copy[3]=80; stock_copy[4]=300; price[0]=120.5; price[1]=115.75; price[2]=140; price[3]=180.5; price[4]=120; } void book::book_detail(int i) { cout<<" *********book detail **********\n"; cout<<setw(25)<<"Title"<<setw(30)<<"Author Name" <<setw(18)<<"Stock copy\n"; cout<<setw(15)<<title[i]<<setw(16)<<author[i]<<setw(15) <<stock_copy[i]<<"\n"; } int book::search() { char name[80],t[80]; cout<<"Enter author name in small letter : "; gets(name); cout<<" 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"; unsuccessful++; } else { book_detail(i); cout<<" How many copies of this book is required : ? "; int copy; cin>>copy; if(copy<=stock_copy[i]) { stock_copy[i]=stock_copy[i]-copy; float total_price; total_price=price[i]*copy; cout<<"Total price = "<<total_price<<" TK\n"; successful++; } else { cout<<" Sorry your required copies is not available \n"; unsuccessful++; } } } void book::edit_price() { cout<<" To edit price "; int i; i=search(); cout<<"Enter new price : "; float p; cin>>p; price[i]=p; } void book::showdetail() { cout<<setw(22)<<"Title"<<setw(30)<<" stock copy "<<setw(20) <<" Price per book "<<endl; for(int i=0;i<size;i++) { cout<<setw(35)<<title[i]<<setw(10)<<stock_copy[i] <<setw(18)<<price[i]<<endl; } } void book::showtransaction() { cout<<setw(22)<<"Successful transaction"<<setw(34) <<"unsuccessful transaction "<<endl<<setw(10) <<successful<<setw(32)<<unsuccessful<<endl; } int main() { book b1; int result; result=b1.search(); b1.buy(result); b1.showdetail(); b1.showtransaction(); b1.edit_price(); cout<<"************details after edit price *****************"<<endl; b1.showdetail(); return 0; }
OUTPUT
Enter author name in small letter : abdus sattar
title of book in small letter : complete solution of balagurusamy(c++)
*********book detail **********
Title Author Name Stock copy
complete solution of balagurusamy(c++) abdus sattar 300
How many copies of this book is required : ? 100
Total price = 12000 TK
Title stock copy Price per book
object oriented programming with c++ 200 120.5
programming in ANCI 150 115.75
electronic circuit theory 50 140
computer algorithm 80 180.5
complete solution of balagurusamy(c++) 200 120
Successful transaction unsuccessful transaction
1 0
To edit price Enter author name in small letter : shahani
title of book in small letter : computer algorithm
Enter new price : 200
************details after edit price*****************
Title stock copy Price per book
object oriented programming with c++ 200 120.5
programming in ANCI 150 115.75
electronic circuit theory 50 140
computer algorithm 80 200
complete solution of balagurusamy(c++) 200 120
Next Previous