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>
using namespace std;
class Base1
{
public:
Base1(string n, string s, string d);
protected:
string name;
string source;
string destination;
};
Base1::Base1(string n, string s, string d) : name(n), source(s), destination(d)
{}
class Base2
{
public:
Base2(int num, int w);
protected:
int number;
int weight;
};
Base2::Base2(int num, int w) : number(num), weight(w)
{}
class Derived : public Base1, public Base2
{
public:
Derived(string n, string s, string d, int num, int w);
void Display();
private:
double TotalAmount();
};
Derived::Derived(string n, string s, string d, int num, int w) : Base1(n, s, d), Base2(num, w)
{}
double Derived::TotalAmount()
{
if (weight <= 15) return 2;
else
{
double temp = weight;
temp -= 15;
int multi;
if ((int)temp / 10 == temp / 10)
{
multi = (temp / 10);
}
else
{
multi = (temp / 10 + 1);
}
return 2 + multi;
}
}
void Derived::Display()
{
cout << "Name is : " << name << endl;
cout << "Source is " << source << endl;
cout << "Destination is: " << destination << endl;
cout << "Total weight is: " << weight << endl;
cout << "Total amount is: " << TotalAmount() << endl;
}
int main()
{
string name, source, destination;
int weight, number;
cout << "Enter name: ";
cin >> name;
cout << "Enter source: ";
cin >> source;
cout << "Enter destination: ";
cin >> destination;
cout << "Enter number post: ";
cin >> number;
cout << "Enter weight: ";
cin >> weight;
cout << endl;
Derived Book(name, source, destination, number, weight);
Book.Display();
cout << endl;
return 0;
}
Comments
Leave a comment