-
April 5th, 2022, 03:10 AM
#1
OOP Basics
Hi, I am a first semester student, learning the OOPs basics. I need help for a program:
Code:
class Message{
public:
void input();
int output();
private:
long token_number;// next token to be given to client
};
void Message::input()
{
token_number = 1;
}
int Message::output()
{
return token_number;
}
I dont know the right way to call these elements in the main()
int main(){
Message obj;
std::cout<< out_buffer<<obj.output()<<std::endl;//write the token number in the buffer
std::cout<<"Response received"<<++obj.input();//To update the input value and display
}
-
April 5th, 2022, 03:31 AM
#2
Re: OOP Basics
What is out_buffer?
I'm not sure what you're trying to achieve. Perhaps you could be more explicit in the requirements? However consider:
Code:
#include <iostream>
class Message {
public:
void input();
int output() const;
Message& operator++() {
++token_number;
return *this;
}
private:
long token_number {};
};
void Message::input() {
token_number = 1;
}
int Message::output() const {
return token_number;
}
int main() {
Message obj;
std::cout << "Response received " << (++obj).output() << '\n';
std::cout << "Response received " << (++obj).output() << '\n';
}
which displays:
Code:
Response received 1
Response received 2
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.5.3)
-
April 5th, 2022, 04:37 AM
#3
Re: OOP Basics
Why is there curly braces after the token_number ?
-
April 5th, 2022, 05:35 AM
#4
Re: OOP Basics
This is default value initialisation. If there's no value between the {} the variable is initialised to the default value for the type of the variable - which is 0 for long. It's good practice to initialise variables when they are defined. Variables of a class type should be initialised via their default constructor/constructors.
See https://en.cppreference.com/w/cpp/la...initialization
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.5.3)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|