C++ object(int) [on hold]


C++ object(int) [on hold]



I have an exam in 2 days with the main given and i need to make all the classes work over that main. I am stuck at a line of code and was hoping you can help me. What function do i need to overload to be able to call this line:



(Lets say i have a class A and an object a that is of type A. The class has an int array. With the next line i need to get the biggest value below the given parameter)


cout << a(1200);



I can't seem to make it work, i tried casting to int altho it didn't quite make sense but i found somewhere that idea and thought to give it a try.



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





If that is part of your exam it should be in your class notes. Look for something on the topic of operator overloading
– UnholySheep
35 mins ago





You should post your attempt...
– Isma
35 mins ago





@UnholySheep If i had notes i would have looked there... I work as a full time java/kotlin programmer and i had no time to go to classes, i can pass this exam quite easy but i don't want a 5 tho.
– Alexandru Sandu
32 mins ago





@Isma the attempt makes no sense and it was only a cast override...
– Alexandru Sandu
32 mins ago





If you were really interested in learning you would have followed my initial advice and researched operator overloading. It doesn't take much to find that you can overload the function call operator. Since you didn't do that I assume you expected someone to present you the code on a silver plater
– UnholySheep
14 mins ago




1 Answer
1



You want to overload << operator. For instance:


<<


ostream& operator<<(ostream& os, const Date& dt)
{
os << dt.mo << '/' << dt.da << '/' << dt.yr;
return os;
}



If a() isn't constructor, but only a () operator overload which I assumed changes the a field of the class A to the parameter provided inside () your code could look something like this:


a()


()


a


A


()


class A{
public:
int a;
A(int a){
this->a = a;
}
A(){}

friend ostream& operator<<(ostream& os, const A& dt);

A operator()(int n){ //changes the current a to n
A a(n);
return a;
}

};

ostream& operator<<(ostream& os, const A& objectA){
os << objectA.a<<endl;
return os;
}

int main()
{
A a(1500);
cout<<(a(1200));

return 0;
}





I will try that now, thank you very much
– Alexandru Sandu
27 mins ago





Not working, i also need another parameter (int)
– Alexandru Sandu
26 mins ago





@AlexandruSandu well, you have to modify this to suit your example. Date object is a class in your case. And in the os<<... line you should print fields from the class a. Also this method should be inside of a class.
– wdc
23 mins ago



Date


a


os<<...


a


a





Yes, i know that, for me it will be: ostream& operator<<(ostream&, A a){...} but as i said in the question i need to call cout<<a(1200); with this function i can't do that because i don't have the parameter (1200)
– Alexandru Sandu
21 mins ago





@AlexandruSandu Edited, hope that helps.
– wdc
21 secs ago

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift