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();
};
//Multiple Inheritance
Derived::Derived(string n, string s, string d, int num, int w) : Base1(n, s, d), Base2(num, w) {
}
double Derived::totalAmount() {
double w;
int n;
if (weight <= 15)
return 2;
else {
w = weight-15;
if ((int)w/10 == w/10)
n = (w/10);
else
n = (w/10 + 1);
return 2 + n;
}
}
void Derived::Display() {
cout << "Name: " << name << endl;
cout << "Source: " << source << endl;
cout << "Destinations: " << destination << endl;
cout << "Total weights: " << weight << endl;
cout << "Total amounts: " << 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 ordinaryPost (name, source, destination, number, weight);
ordinaryPost.Display();
cout << endl;
return 0;
}
Comments
Leave a comment