xem giúp mình cái lỗi trong class này sửa thế nào:error C2248: 'heso' : cannot access private member declared in class 'donthuc'
đây là code:
#include<iostream>
using namespace std;
#include<stdio.h>
#include<math.h>
class donthuc
{
private:
float somu;
float heso;
public:
donthuc();
donthuc(float a, float b);
donthuc(const donthuc &b);
~donthuc();
void nhapdt();
void xuatdt();
int layhs();
int laysm();
void gan_hs(int a);
void gan_sm(int b);
int giatridt(int x);
void daohamdt();
void nguyenhamdt();
donthuc congdt(const donthuc &b);
donthuc trudt(const donthuc &b);
donthuc nhandt(const donthuc &b);
donthuc chiadt(const donthuc &b);
int sosanhdt(const donthuc &b);
donthuc operator+(const donthuc &a);
donthuc operator-(const donthuc &a);
donthuc operator*(const donthuc &a);
donthuc operator/(const donthuc &a);
donthuc operator=(const donthuc &a);
donthuc operator+=(const donthuc &a);
donthuc operator-=(const donthuc &a);
int operator>(const donthuc &a);
int operator<(const donthuc &a);
int operator>=(const donthuc &a);
int operator<=(const donthuc &a);
int operator==(const donthuc &a);
int operator!=(const donthuc &a);
donthuc operator ++();
donthuc operator --();
friend istream& operator>>(istream &is,donthuc &a);
friend ostream& operator<<(ostream &os,const donthuc &a);
donthuc operator !();
donthuc operator ~();
};
donthuc::donthuc()
{
heso = 0;
somu = 0;
}
donthuc::donthuc(float a, float b)
{
heso = a;
somu = b;
}
donthuc::donthuc(const donthuc &b)
{
heso = b.heso;
somu = b.somu;
}
donthuc::~donthuc(){}
void donthuc::nhapdt()
{
cout<<"nhap he so va so mu: "<<endl;
cin>> heso >>somu ;
}
void donthuc::xuatdt()
{
cout<<heso<<"x^"<<somu;
}
int donthuc::layhs()
{
return heso;
}
int donthuc::laysm()
{
return somu;
}
void donthuc::gan_hs(int a)
{
heso = a;
}
void donthuc::gan_sm(int b)
{
somu = b;
}
int donthuc::giatridt(int x)
{
return heso*pow(x,somu);
}
void donthuc::daohamdt()
{
heso = heso * somu;
somu = somu -1;
}
void donthuc::nguyenhamdt()
{
somu = somu + 1;
heso = heso/somu;
}
donthuc donthuc::congdt(const donthuc &b)
{
heso = heso + b.heso;
return *this;
}
donthuc donthuc::trudt(const donthuc &b)
{
heso = heso - b.heso;
return *this;
}
donthuc donthuc::nhandt(const donthuc &b)
{
heso = heso*b.heso;
somu = somu + b.somu;
return *this;
}
donthuc donthuc::chiadt(const donthuc &b)
{
heso = heso/b.heso;
somu = somu - b.somu;
return *this;
}
int donthuc::sosanhdt(const donthuc &b)
{
if((heso == b.heso) &&(somu == b.somu))
return 0;
else
if ((somu > b.somu)|| ((somu== b.somu)&&(heso > b.heso)))
return 1;
return 0;
}
donthuc donthuc::operator+(const donthuc &a)
{
return congdt(a);
}
donthuc donthuc::operator-(const donthuc &a)
{
return trudt(a);
}
donthuc donthuc::operator*(const donthuc &a)
{
return nhandt(a);
}
donthuc donthuc::operator/(const donthuc &a)
{
return chiadt(a);
}
donthuc donthuc::operator=(const donthuc &a)
{
heso = a.heso;
somu = a.somu;
return *this;
}
donthuc donthuc::operator+=(const donthuc &a)
{
heso += a.heso;
return *this;
}
donthuc donthuc::operator-=(const donthuc &a)
{
heso -= a.heso;
return *this;
}
int donthuc::operator>(const donthuc &a)
{
if((somu > a.somu) || (heso > a.heso && a.somu == somu ))
return 1;
else
return 0;
}
int donthuc::operator<(const donthuc &a)
{
if((somu < a.somu) || (heso < a.heso && a.somu == somu ))
return 1;
else
return 0;
}
int donthuc::operator==(const donthuc &a)
{
if((heso == a.heso) && (somu == a.somu))
return 1;
else
return 0;
}
int donthuc::operator>=(const donthuc &a)
{
if((heso >= a.heso) && (somu >= a.somu))
return 1;
else
return 0;
}
int donthuc::operator<=(const donthuc &a)
{
if((heso <= a.heso) && (somu <= a.somu))
return 1;
else
return 0;
}
int donthuc::operator!=(const donthuc &a)
{
if((heso != a.heso) || (somu != a.somu))
return 1;
else
return 0;
}
donthuc donthuc::operator++()
{
somu ++;
return *this;
}
donthuc donthuc::operator--()
{
somu --;
return *this;
}
istream& operator>>(istream &is,donthuc &a)
{
is>>a.heso>>a.somu;
return is;
}
ostream& operator<<(ostream &os,const donthuc &a)
{
os<<"He so = "<<a.heso<<endl;
os<<"So mu = "<<a.somu<<endl;
return os;
}
donthuc donthuc::operator!()
{
heso = heso*somu;
somu = somu - 1;
return *this;
}
donthuc donthuc::operator~()
{
somu = somu + 1;
heso = heso/somu;
return *this;
}
mình mới học class nên chưa rõ lắm:
Đã được chỉnh sửa lần cuối bởi mavuong2000 : 08-10-2011 lúc 02:52 PM. Lý do: sửa lại tiêu đề
mình đã chạy thử bài của bạn và mình thấy 2 toán tử << và >> hoạt động bình thường mà có bất thường nào đâu???