0
votes

My program has the following objectives:

  • To overload the base class extraction operator in the derived class: I tried doing this using static_cast<Derived> (derived) but ended up in run time error. I know that friend won't get inherited. I had redeclared friend function in my derived class as well with different arguments to handle the derived class data members.
  • To handle static data members: I have to change the values of the static data members. I have to use them in combination with non static data members as well. I tried using static getters and setters. But couldn't succeed.

Here is my code below:

#include <iostream>

using namespace std;

class Base {
private:
    string name;
    int rollNum;
public:
    friend std::istream& operator>>(std::istream& in, Base &base);
};

std::istream& operator>>(std::istream& in, Base &base) {
    string name;
    int rollNum;
    cout << "Enter name: ";
    in >> base.name;
    cout << "Enter roll no: ";
    in >> base.rollNum;
    return in;
}

class Derived : public Base {
private:
    int myNumOne;
    static int x;
    static int y;
public:
    friend std::istream& operator>>(std::istream& in, Derived Derived);
    void add();
    static void init();
    static int getX();
    static int getY();
};

std::istream& operator>>(std::istream & in, Derived derived) {
    in >> static_cast<Derived> (derived);
    cout << "Enter a number: ";
    in >> derived.myNumOne;
    return in;
}

static void Derived::init() {
    x = 100;
    y = 100;
}   

static int Derived::getX() {
    return x;
}

static int Derived::getY() {
    return y;
}

void Derived::add() {
    myNumOne += getX() + getY();
    cout << "Number = " << myNumOne << endl;
}

int main() {
    Derived obj;
    cin>>obj;
    Derived objOne;
    objOne.add();
    Derived objTwo;
    objTwo.add();
    return 0;
}

I am getting the error as follows:

newmain.cpp:43:31: error: cannot declare member function 'static void Derived::init()' to have static linkage [-fpermissive]
     static void Derived::init() {
                               ^
newmain.cpp:48:30: error: cannot declare member function 'static int Derived::getX()' to have static linkage [-fpermissive]
     static int Derived::getX() {
                              ^
newmain.cpp:52:30: error: cannot declare member function 'static int Derived::getY()' to have static linkage [-fpermissive]
     static int Derived::getY() {
                              ^
1
Remove the static in front of the function definitions. - πάντα ῥεῖ
Now I am getting undefined reference to Derived::x in getX(), getY() and init() methods. - Praveen Vinny
You have to provide definitions for Derived::x and Derived::y as well. - πάντα ῥεῖ
Please try to limit yourself to one topic per question. - MikeMB
@MikeMB - I will take care of that in future. Thanks for the suggestion. - Praveen Vinny

1 Answers

1
votes

Remove static keyword at implementation, it's only needed at declaration:

void Derived::init() {
    x = 100;
    y = 100;
}   

You also need to implement static x and y as this:

int Derived::x = 0;
int Derived::y = 0;

Also, operator>> must take a reference to the object as it's going to modify it. Then this should work do:

std::istream& operator>>(std::istream & in, Derived& derived) 
{
    Base& base = derived;
    in >> base;
    cout << "Enter a number: ";
    in >> derived.myNumOne;
    return in;
}

In the end, this will compile, but may not do what you expected. You may rework the operator>>:

#include <iostream>

using namespace std;

class Base {
private:
    string name;
    int rollNum;
public:
    friend std::istream& operator>>(std::istream& in, Base &base);
};

std::istream& operator>>(std::istream& in, Base &base) {
    string name;
    int rollNum;
    cout << "Enter name: ";
    in >> base.name;
    cout << "Enter roll no: ";
    in >> base.rollNum;
    return in;
}

class Derived : public Base {
private:
    int myNumOne;
    static int x;
    static int y;
public:
    friend std::istream& operator>>(std::istream& in, Derived& Derived);
    void add();
    static void init();
    static int getX();
    static int getY();
};

int Derived::x = 0;
int Derived::y = 0;

std::istream& operator>>(std::istream & in, Derived& derived) 
{
    Base& base = derived;
    in >> base;
    cout << "Enter a number: ";
    in >> derived.myNumOne;
    return in;
}

void Derived::init() {
    x = 100;
    y = 100;
}   

int Derived::getX() {
    return x;
}

int Derived::getY() {
    return y;
}

void Derived::add() {
    myNumOne += getX() + getY();
    cout << "Number = " << myNumOne << endl;
}

int main() {
    Derived obj;
    cin>>obj;
    Derived objOne;
    objOne.add();
    Derived objTwo;
    objTwo.add();
    return 0;
}

Not sure you really meant to have x and y static. Why arent's they Derived objects attributes?