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

Thread: code problem

  1. #1
    Join Date
    May 2013
    Posts
    1

    code problem

    Need help pointing out where/if these are applied:
    The heap
    Constructor overloading
    Private/protected data members/functions
    Functions, parameters, return values

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    class RandomNumberGenerator
    {
    	int secretNumber;
    	int tries;
    	int guess;
    
    public:
    	RandomNumberGenerator()
    	{
    		tries=0;
    		secretNumber=randomNumber(100); 
    	}
    
    	int randomNumber(int max)
    	{
    		return (rand() % max + 1); // random number between 1 and 100
    	}
    
    	void guessprocedure()
    	{
    		cout << "\tWelcome to Guess My Number\n\n";
    		do
    		{
    			cout << "Enter a guess: ";
    			cin >> guess;
    			++tries;
    			if (guess > secretNumber)
    			{
    				cout << "Too high!\n\n";
    			}
    			else if (guess < secretNumber)
    			{
    				cout << "Too low!\n\n";
    			}
    			else
    			{
    				cout << "\nThat's it! You got it in " << tries << "guesses!\n";
    			}
    		} while (guess != secretNumber);
    	}
    };
    
    int main()
    {
    	RandomNumberGenerator rng;		
    	rng.guessprocedure();
    	return 0;
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: code problem

    What have you got so far?

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

    Re: code problem

    Constructor overloading is where several class constructors are defined that take different type/number parameters. In the code the class is RandomNumberGenerator, so what are the class constructors and how many constructors are there?

    In a class, by default all entries are private (unlike a struct where all are public). So in a class unless a member is specified as public or protected, they are private. So for the class, which are the private, public and protected members?

    A function can have none or multiple parameters. It can also return a value or not. If the function doesn't return a value then void is used for the return type. So what are the functions, their parameters and return types?

    Sorry, but as this is homework we won't provide you with the answers directly, but give you some guidance. You may like to look at

    http://www.cplusplus.com/doc/tutorial/
    http://www.learncpp.com/
    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)

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