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 <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Decribe
{
public:
Decribe(string name_, string source_, string destination_);
protected:
string name;
string source;
string destination;
};
Decribe::Decribe(string name_, string source_, string destination_) : name(name_), source(source_), destination(destination_)
{}
class Value
{
public:
Value(int number_, int weight_);
protected:
int number;
int weight;
};
Value::Value(int number_, int weight_) : number(number_), weight(weight_)
{}
class Information : public Decribe, public Value
{
public:
Information(string name_, string source_, string destination_, int number_, int weight_);
void Display();
private:
double Amount();
};
Information::Information(string name_, string source_, string destination_, int number_, int weight_) : Decribe(name_, source_, destination_), Value(number_, weight_)
{}
double Information::Amount()
{
if (weight <= 15) return 2;
else
{
double tempWeight = weight;
tempWeight -= 15;
int additionalValue;
if ((int)tempWeight / 10 == tempWeight / 10) additionalValue = (tempWeight / 10);
else additionalValue = (tempWeight / 10 + 1);
return 2 + additionalValue;
}
}
void Information::Display()
{
cout << "Name is " << name << endl;
cout << "Source is " << source << endl;
cout << "Destination is " << destination << endl;
cout << "Total weight is " << weight << " grams." << endl;
cout << fixed << setprecision(2);
cout << "Amount is Rs." << Amount() << endl;
}
int main()
{
string name, source, destination;
int weight, number;
cout << "Enter name(string): ";
cin >> name;
cout << "Enter source(string): ";
cin >> source;
cout << "Enter destination(string): ";
cin >> destination;
cout << "Enter number post(int): ";
cin >> number;
cout << "Enter weight(int): ";
cin >> weight;
cout << "*************************************************" << endl;
Information Note(name, source, destination, number, weight);
Note.Display();
cout << endl;
return 0;
}
Comments
Leave a comment