I m new in C++, and I want to improve myself but I can not understand it.
I create a class
PersonHeight{
int meter=0; // I hold the meter value
int centimeter=0; // I hold the centimeter value ( do I need to create constructor & destructor? )
}
// get/set methods for the my data members
void get m_value() {
return meter;}
void get c_value() {
return centimeter;}
void set m_value(int m) {
meter=m;}
void set m_value(int c) {
centimeter=c;}
how can overload - operator for the difference of two instances of PersonHeight can be calculated and overload > operator to compute if a PersonHeight object is greater than another one.
Also how can I test my functions in the main? is it with the - and > operands?
Could you help me?
class CPersonHeight
{
private:
int meter; // I hold the meter value
int centimeter;
public:
CPersonHeight() { meter = 0 ; centimeter = 0 ; }
~CPersonHeight() { }
void get m_value();
void get c_value();
void set m_value(int m);
void set m_value(int c);
}
void CPersonHeight::get m_value()
{
return meter;
}
void CPersonHeight::get c_value()
{
return centimeter;
}
void CPersonHeight::set m_value(int m)
{
meter=m;
}
void CPersonHeight::set m_value(int c)
{
centimeter=c;
}
Could you give me an example with the operator overloading? For example, overloading + operator for the difference of two instances of PersonHeight can be calculated
The overload operators such as + , - , * , += , -= , *=
should return an object of the class itself!
But if it is a special case, than I have said nothing :-)
And in all cases the overload function must take object of kind const className& ob , const reference.
I'll try to explain it as simple as possible,
so when you overload = operator you say the operator =, hey you
take two arguments,
the first one is the current object itself, the object that stands at the left side of you ( targetObject= )
the second one, the other object, the object that stands at the right side of you ( =sourceObject )
For instance I am an object being at the left side ( a )= (b ) I am other object being at the right side
or
targetObejct = sourceObject
so you says the operator , dont change the other object(source), that is the reason you writeconst CPersonHeight &rh, const tells the function you cant change me
and you tell the overload operator= to send a reference, for this reason you define the return type as CPersonHeight&
#include <iostream>
usingnamespace std;
class CPersonHeight
{
private:
int meter;
int centimeter;
public:
CPersonHeight() { meter = 0 ; centimeter = 0 ; }
CPersonHeight(int m, int c) { meter = m+c/100 ; centimeter = c%100 ; }
~CPersonHeight() { }
int get_meter() { return meter; }
int get_centimeter() { return centimeter; }
void set_meter(int m) { meter = m; }
void set_centimeter(int c) { centimeter = c; }
CPersonHeight operator- (const CPersonHeight &x)
{
int m,c;
m=x.meter-meter;
centimeter=x.centimeter-centimeter;
CPersonHeight difference(m,c);
return difference;
}
void print_difference()
{
cout<<"The difference is:"<<meter << "m" <<centimeter<< "cm"<< endl;
}
};
CPersonHeight:: operator> (const CPersonHeight & x)
{
int x1, x2;
x2=x.meter*100+x.centimeter;
x1=meter*100+centimeter;
if(x1>x2)
// what can ı do there? also what can ı do in main?
//my operator is compuing if a CPersonHeight object is greater than another one.
}
CPersonHeight:: operator== (const CPersonHeight & x)
{
int x1, x2;
x2=x.meter*100+x.centimeter;
x1=meter*100+centimeter;
if(x1==x2)
// what can ı do there? also what can ı do in main?
//operator to compute whether two CPersonHeight objects are equal or not. is it acceptable that writing bool func?
}
int main()
{
CPersonHeight h1(1,80);
CPersonHeight h2(1,50);
CPersonHeight h3= h1-h2;
h1.print_difference();
system("pause");
return 0;
}
I wrote this ones but I need a help, I wrote as a comment. Could you help me? Thank you so much.
operator such as < , > , == , != should be declare as class methods, they may be declared as friend or normal function as well. However I do it as class method.
Each overload operator( < , > , == , != ) should return a type of bool, either true or false
class CPersonHeight
{
//do some thing
// do other things as usual
// now declare your overload operator
booloperator>(const CPersonHeight& x);
booloperator<(const CPersonHeight& x);
booloperator==(const CPersonHeight& x);
booloperator!=(const CPersonHeight& x);
};
//now you can define them here
// for example I will take your implemnetation
bool CPersonHeight:: operator== (const CPersonHeight & x)
{
int x1, x2;
x2=x.meter*100+x.centimeter;
x1=meter*100+centimeter;
return x1==x2 ;
}
bool CPersonHeight:: operator!= (const CPersonHeight & x)
{
return !(*this ==x) ;
}