CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: OOP Basics

Hybrid View

  1. #1
    Join Date
    Apr 2022
    Posts
    11

    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
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.6.5)

  3. #3
    Join Date
    Apr 2022
    Posts
    11

    Re: OOP Basics

    Why is there curly braces after the token_number ?

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.6.5)

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
  •  





Click Here to Expand Forum to Full Width

Featured