Practicing more with Inheritance, Nested classes, and Operator overloading

 


Animals are classified into mammals, birds and fish, bats are birds and mammals. For animals in general we need to
keep their weight, age, name, and the distance they can travel. For the distance we keep its length and number of
hours needed for that to be completed. For mammals, we need number of times they give birth per year. For birds
we need to keep number of eggs they lay down per month, and for fish, we need length, width and thickness
Define a static variable to keep number of objects in each class.
Define these classes and for each class overload the input, output, comparison operator >= which compares two
objects in each class. For animals X>=Y if weight of X >= Weight of Y, for birds, X>=Y if number of eggs by X >=
those by Y, for fish X>=Y if thickness of X is >= that of Y, for mammals X>=Y if number of times C gives birth is
>= that of Y.
Write a main program to demonstrate all the functions defined above for the classes.


#include <iostream>
using namespace std;

// SAMET SARIAL - 64170037 - COE - LAB8 -

class Animals{
public:
    double weight;
    int age;
    string name;
    double distance;

    Animals(){
        double weight=0.0;
        int age = 0;
        string name;
        double distance = 0.0;
    }

    Animals(double w, int a, string n, double d) {
        weight = w;
        age = a;
        name = n;
        distance = d;
    }

    friend istream &operator>>(istream &input, Animals &A){
        input >> A.weight >> A.age >> A.name >> A.distance;
    }

    friend ostream &operator<<(ostream &output, const Animals &A){
        output << "Weight : " << A.weight << ", Age : " << A.age << " Name : " << A.name << " Distance : " << A.distance;
        return  output;
    }

    friend bool operator >= (const Animals &a1, const Animals &a2){
        return (a1.weight >= a2.weight);
    }

};

class Mammals : public Animals{
public:
    int Birth;

    Mammals(){
        double weight=0.0;
        int age = 0;
        string name;
        double distance = 0.0;
        int birth;
    }

    Mammals(double w, int a, string n, double d, int b) {
        weight = w;
        age = a;
        name = n;
        distance = d;
        Birth = b;
    }

    friend istream &operator>>(istream &input, Mammals &M){
        input >> M.weight >> M.age >> M.name >> M.distance >> M.Birth;
    }

    friend ostream &operator<<(ostream &output, const Mammals &M){
        output << "Weight : " << M.weight << ", Age : " << M.age << " Name : " << M.name << " Distance : " << M.distance << " Birth : " << M.Birth;
        return  output;
    }

    friend bool operator >= (const Mammals &m1, const Mammals &m2){
        return (m1.Birth >= m2.Birth);
    }
};

class Birds : public Animals {
public:
    int Eggs;

    Birds(){
        double weight=0.0;
        int age = 0;
        string name;
        double distance = 0.0;
        int eggs = 0;
    }

    Birds(double w, int a, string n, double d, int e) {
        weight = w;
        age = a;
        name = n;
        distance = d;
        Eggs = e;
    }

    friend istream &operator>>(istream &input, Birds &B){
        input >> B.weight >> B.age >> B.name >> B.distance >> B.Eggs;
    }

    friend ostream &operator<<(ostream &output, const Birds &B){
        output << "Weight : " << B.weight << ", Age : " << B.age << " Name : " << B.name << " Distance : " << B.distance << " Number of eggs : " << B.Eggs;
        return  output;
    }

    friend bool operator >= (const Birds &b1, const Birds &b2){
        return (b1.Eggs >= b2.Eggs);
    }

    class Bats : public Mammals{
        Bats(){
            double weight=0.0;
            int age = 0;
            string name;
            double distance = 0.0;
            int eggs = 0;
            int birth = eggs;
        }

        friend istream &operator>>(istream &input, Bats &Ba){
            input >> Ba.weight >> Ba.age >> Ba.name >> Ba.distance >> Ba.Birth;
        }

        friend ostream &operator<<(ostream &output, const Bats &Ba){
            output << "Weight : " << Ba.weight << ", Age : " << Ba.age << " Name : " << Ba.name << " Distance : " << Ba.distance << " Birth : " << Ba.Birth;
            return  output;
        }

    public:
        Bats(double w, int a, string n, double d, int b) {
            weight = w;
            age = a;
            name = n;
            distance = d;
            Birth = b;
        }
    };
};

class Fish : public Animals {
public:
    double length;
    double width;
    double thickness;
    Fish(){
        double weight=0.0;
        int age = 0;
        string name;
        double distance = 0.0;
        double length = 0.0;
        double width = 0.0;
        double thickness = 0.0;
    }

    Fish(double w, int a, string n, double d, double l, double wi, double t) {
        weight = w;
        age = a;
        name = n;
        distance = d;
        length = l;
        width = wi;
        thickness = t;
    }

    friend istream &operator>>(istream &input, Fish &F){
        input >> F.weight >> F.age >> F.name >> F.distance >> F.length >> F.width >> F.thickness;
    }

    friend ostream &operator<<(ostream &output, const Fish &F){
        output << "Weight : " << F.weight << ", Age : " << F.age << " Name : " << F.name << " Distance : " << F.distance << " Length : " << F.length << " Width : " << F.width << " Thickness : " << F.thickness;
        return  output;
    }

    friend bool operator >= (const Fish &f1, const Fish &f2){
        return (f1.thickness >= f2.thickness);
    }
};

void compareTo(Animals M1,Animals M2){
    if(M1 >= M2){
        cout << M1.name<<endl;
    }else if(M2 >=M1){
        cout << M2.name<<endl;
    }
}


int main() {
    Mammals M1(20.5, 2, "Memeli1", 25, 3);
    Mammals M2(22.5, 3, "Memeli2", 30, 2);
    compareTo(M1,M2);
    Birds B1(2.5, 2, "bird1", 200, 2);
    Birds B2(3.5, 4, "bird1", 2030, 3);
    compareTo(B1,B2);
    Fish F1(2.0, 3, "Hamsi", 500, 2, 3, 1);
    Fish F2(3.0, 4, "Palamut", 5030, 3, 4, 2);
    compareTo(F1,F2);
    Birds ::Bats bat1(12, 2, "bat1", 23, 3);
    Birds ::Bats bat2(1, 2, "bat2", 23, 3);
    compareTo(bat1,bat2);
}

Yorum Gönder

0 Yorumlar