(Reusability using inheritance)
The postage for ordinary post is Rs. 2.00 for the first 15 grams and Rs. 1.00 for each additional 10 grams. Write a C++ program to calculate the charge of postage for a post weighting N grams. Read the weights of N packets and display the total amount of postage using multiple inheritance.
* Base class-1: Member variables are name, source and destination.
* Base class-2: Member variables are no. of packets and weight.
* Derived class: Member functions display(), displays the name, source, destination, total weight and total amount.
#include<bits/stdc++.h>
using namespace std;
class BaseClass1
{
public:
string Name,source,destination;
};
class BaseClass2
{
public:
int no_of_packets;
float weight;
};
class Derived : public BaseClass1 , public BaseClass2
{
public:
void display()
{
float total_weight;
total_weight=no_of_packets*weight;
cout<<"\nName : "<<Name;
cout<<"\nSource : "<<source;
cout<<"\nDestination : "<<destination;
cout<<"\nTotal Weight : "<<total_weight;
if(total_weight<=15)
{
cout<<"\nTotal Cost : Rs. 2";
}
else
{
if(total_weight>15)
{
total_weight=total_weight-15;
int n=total_weight/10;
if(n*10==total_weight)
{
float cost=2+(n*1);
cout<<"\nTotal Cost : Rs. "<<cost;
}
else
{
float cost=3+(n*1);
cout<<"\nTotal Cost : Rs. "<<cost;
}
}
}
}
};
int main()
{
Derived d1;
cout<<"Enter name ";
getline(cin,d1.Name);
cout<<"Enter Source ";
getline(cin,d1.source);
cout<<"Enter Destination ";
getline(cin,d1.destination);
cout<<"Enter number of packets ";
cin>>d1.no_of_packets;
cout<<"Enter weight of each packet ";
cin>>d1.weight;
d1.display();
}
Comments
Leave a comment