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;
// Base class-1
class Base1 {
public:
// parameterized constructor
Base1(string name, string source, string destination) {
Name = name;
Source = source;
Destination = destination;
}
protected:
string Name;
string Source;
string Destination;
};
// Base class-2
class Base2 {
public:
// parameterized constructor
Base2(int number, int weight){
Number = number;
Weight = weight;
}
protected:
int Number;
int Weight;
};
//Multiple Inheritance
class Derived : public Base1, public Base2 {
public:
Derived(string name, string source, string destination, int number, int weight)
: Base1(name, source, destination), Base2(number, weight) {
}
void Display(){
cout<<"\nPostage information:"<<endl;
cout<<"______________________"<<endl;
cout << "Name: " << Name << endl;
cout << "Source: " << Source << endl;
cout << "Destinations: " << Destination << endl;
cout << "Total weights: " << Weight << endl;
cout << "Total amounts: " << totalAmount() << endl;
}
private:
double totalAmount(){
double weight;
int number;
if (Weight <= 15)
return 2;
else {
weight = Weight-15;
if ((int)weight/10 == weight/10)
number = (weight/10);
else
number = (weight/10 + 1);
return 2 + number;
}
}
};
int main() {
Derived postage01 ("Leo", "London", "Paris", 785, 100);
postage01.Display();
Derived postage02 ("Alex", "Poma", "Mexiko", 154, 10);
postage02.Display();
cout << endl;
return 0;
}
Comments
Leave a comment