Answer to Question #174188 in C++ for Harini.M

Question #174188


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



1
Expert's answer
2021-03-28T14:37:29-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS