1) Create two classes which stores distance in feet, inches and meter, centimeter format respectively. Write a function which compares distance in object of these classes and displays the larger one.
2) Create a class which stores name, author and price of a book. Store information for n
number of books. Display information of all the books in a given price range using
friend function.
Question 1:
#include<iostream>
using namespace std;
class Book{
private:
string bookName, bookAuthor;
double price;
public:
Book (){
}
Book(string n, string a, double p){
bookName = n;
bookAuthor = a;
price = p;
}
void setDetails(string n, string a, double p){
bookName = n;
bookAuthor = a;
price = p;
}
string getName(){
return bookName;
}
string getAuthor(){
return bookAuthor;
}
double getPrice(){
return price;
}
void printBook(){
cout<<"The book name is\t"<<bookName<<endl;
cout<<"The book author is\t"<<bookAuthor<<endl;
cout<<"The book price is\t"<<price<<endl;
}
friend void display(int x);
};
void display(int x ){
string bookName, bookAuthor;
double price;
Book arr[x];
for(int i=0; i<x; i++){
cout<<"Enter the book name\n";
cin>>bookName;
cout<<"Enter the author \n";
cin>>bookAuthor;
cout<<"Enter the book price\n";
cin>>price;
arr[i].setDetails(bookName,bookAuthor,price);
}
for(int i=0; i<x; i++){
cout<<"Details of book "<<(i+1)<<" are:\n";
arr[i].printBook();
}
}
int main(){
int x;
cout<<"Enter the number of books\n";
cin>>x;
display(x);
}
Question 2
#include<iostream>
using namespace std;
class DB;
class DM
{
float meters,centimeters;
public:
DM(float m, float c){
meters = m;
centimeters = c;
}
friend void compares(DM & x,DB &y);
};
class DB
{
float inch;
float feet;
public:
DB(float i, float f){
inch = i;
feet = f;
}
friend void compares(DM & ,DB & );
};
void compares(DM &a,DB &b)
{
if((b.feet * 0.0254) < a.meters ){
cout<<"Distance in meters and centimers is larger\n";
}
else{
cout<<"Distance in inches and feet is larger\n";
}
}
int main()
{
DM a(20,30);
DB b(100,60);
compares(a,b);
}
Comments
Thanks and as always great work for Assignmentexpert team!!
Leave a comment