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.