0

I met a problem when i practiced code oop. my 'class vdv', which is a athlete, has a default constructor with " " name, " " sport, 0 age, 0 height and 0 weight. i wrote operator overloading cout for it too. But when i cin >> my obj and then cout << it, it only shows the default value " " name, " " sport, 0 age, 0 height and 0 weight.

here is my code

#include <string>
using namespace std;
#include "VanDongVien.h"

//------------------------------------------

#pragma once
class vdv {
private:
    int age;
    string name, sport;
    double weight, height;
public:
    vdv() {
        name = sport = "No name";
        age = 0;
        weight = height = 0;
    }
    vdv(string _name, string _sport, int _age, double _height, double _weight) :
        name(_name), sport(_sport), age(_age), height(_height), weight(_weight) {};
    ~vdv() {
        name = sport = " ";
        age = 0;
        height = weight = 0;
    }
    friend istream& operator>>(istream &is, vdv obj);
    friend ostream& operator<<(ostream &os, vdv u);
    bool operator>(vdv obj);
};

//------------------------------------------

istream &operator>>(istream &is, vdv obj) {

    cout << "Nhap ho va ten: "; fflush(stdin); getline(is, obj.name);
    cout << "Nhap mon thi dau: "; fflush(stdin); getline(is, obj.sport);
    cout << "Nhap tuoi: "; cin >> obj.age;
    cout << "Nhap chieu cao: "; cin >> obj.height;
    cout << "Nhap can nang: "; cin >> obj.weight;
    return is;
}
ostream &operator<< (ostream & os, vdv obj) {
    cout << "Ho va ten: " << obj.name << endl;
    cout << "Mon thi dau: " << obj.sport << endl;
    cout << "Tuoi: " << obj.age << endl;
    cout << "Chieu cao: " << obj.height << endl;
    cout << "Can nang: " << obj.weight << endl;`enter code here`
    return os;
}
bool vdv::operator>(vdv obj) {
    if (height > obj.height) return true;
    else if (height < obj.height) return false;
    else if (weight >= obj.weight) return true;
    else return false;
}
void swap(vdv &o1, vdv &o2) {
    vdv temp = o1;
    o1 = o2;
    o2 = temp;
}
void bubblesort(vdv *a, int n) {
    for (int i = 0; i < n - 1; i++)
        for (int j = 1; j < n; j++) {
            if (a[i] > a[j]) swap(a[i], a[j]);
        }
}

//------------------------------------------

int main() {
    vdv o1;
    cin >> o1;
    cout << o1;
    system("pause");
}

vdv(Van dong vien is athlete).

Thanks for reading my code and for your help.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
KhimCho
  • 11
  • 1
  • 3
    You are passing your objects by value, so the work done in `operator>>` is done on a copy of the original object. You want to pass a reference instead. https://stackoverflow.com/a/4421719/920069 – Retired Ninja Dec 30 '21 at 03:15
  • 1
    Not only for `operator >>`, but you are passing those objects by value in most of your functions, when they should be passed by reference. Like here: `bool vdv::operator>(vdv obj)`. The `obj` should be passed by const reference. BTW, the `~vdv()` is not necessary -- it can be declared as `=default;` or just removed entirely. – PaulMcKenzie Dec 30 '21 at 03:19
  • 1
    There are *many* things that are conceptually wrong here. In particular, you cannot reasonably give prompts for input while using `operator>>` to input; the *entire point* of writing an `operator>>` is that the input might come from *any stream*, not just the standard input - so there may not be anyone to prompt. Similarly, you have written `operator>>` in a way that it ignores the stream it's supposed to be reading from, and always tries to read from `std::cin` instead. This defeats the purpose. Similarly for `operator<<` using `std::cout`. – Karl Knechtel Dec 30 '21 at 03:24
  • Welcome to Stack Overflow. The kind of help that you need to fix this code properly is beyond the scope of a Stack Overflow question. You should instead try to use an actual *discussion forum*, such as Reddit or Quora; or talk with your instructor. – Karl Knechtel Dec 30 '21 at 03:25
  • Sorry :( i'm just new in coding – KhimCho Dec 30 '21 at 03:28
  • I would please you to minimize all your coding examples. It is quite a lot of work to read 100 lines of code to get a simple mistake. You should organize your work yourself in the same way: Find a problem by reducing it to the minimum of code which shows the problem. You can trust me, you will find most of your problems yourself simply by reducing your code as you move out the root cause sometimes even if you did not expect it. Often problems come from code parts which are not under your focus. So please help yourself and us by giving much! shorter examples. Thanks! – Klaus Dec 30 '21 at 09:34

2 Answers2

1

You should pass the second parameter as reference when overloading operator<< and operator>> as shown below. I have added comments wherever i have made modifications.

class vdv {
private:
    int age;
    std::string name, sport;
    double weight, height;
public:
    vdv() {
        name = sport = "No name";
        age = 0;
        weight = height = 0;
    }
    vdv(std::string _name, std::string _sport, int _age, double _height, double _weight) :
        name(_name), sport(_sport), age(_age), height(_height), weight(_weight) {};
    ~vdv() {
        name = sport = " ";
        age = 0;
        height = weight = 0;
    }
    friend std::istream& operator>>(std::istream &is, vdv &obj);//passed second object by reference
    friend std::ostream& operator<<(std::ostream &os, const vdv &u);//passed second object as a referece to const
    bool operator>(vdv obj);
};


std::istream &operator>>(std::istream &is, vdv &obj) {//added & on second argument

    is >> obj.name >> obj.sport >> obj.age >> obj.height >> obj.weight;
    
    //no need for cout here 
    
    //check if input succeded 
    if(is)
    {
        ;//do something here if you need to 
    }
    //otherwise leave the objec in its default state
    else 
    {
        obj = vdv();
    }
    
    
    return is;
}
std::ostream &operator<< (std::ostream & os, const vdv &obj) {//passed 2nd argument as reference to const
    os << obj.name << obj.sport << obj.age << obj.height << obj.weight;
    
    //no need for cout here
    
    return os;
}
bool vdv::operator>(vdv obj) {
    if (height > obj.height) return true;
    else if (height < obj.height) return false;
    else if (weight >= obj.weight) return true;
    else return false;
}

The output of the program can be seen here.

Some of the modifications that i made include:

  1. Second object is passed by reference for both operator<< and operator>>.
  2. Added a check to see if input succeeded inside operator>>.
  3. Removed unnecessary cout statements from inside operator<< and operator>>.
  4. Removed using namespace std; which is a recommended practice.
Jason
  • 36,170
  • 5
  • 26
  • 60
0

I think you need to pass vdv as reference in the operator>> like this:

istream &operator>>(istream &is, vdv& obj) {

    cout << "Nhap ho va ten: "; fflush(stdin); getline(is, obj.name);
    cout << "Nhap mon thi dau: "; fflush(stdin); getline(is, obj.sport);
    cout << "Nhap tuoi: "; cin >> obj.age;
    cout << "Nhap chieu cao: "; cin >> obj.height;
    cout << "Nhap can nang: "; cin >> obj.weight;
    return is;
}

When you write cin >> o1 it will behave as operator>>(cin, o1) but you are passing o1 by value so in the operator>> function, it creates another copy which is not related to the one that you are trying to read in.