Space Aliens and C++ programming and inline classes

Aluminum foil hat time.  And inline class example.

Back story:

You have slipped between dimensions and the new dimension you are living in has found that Venus is a rain forest with the aliens living in hives and Mars is like a arid but cold desert with aliens that live in trailers parked in trailer parks. 

Your mission create a simple program that will input the number of appendages of the newly discovered Martians and Venusians using C++/CLI and Visual Studio.  You don’t have to store anything, you just have to input the numbers in software and then output it using the boring console windows.  Really boring console window that I hope you won’t to use in the future.

First start Visual Studio 2012 (similar to VS 2013) and then select C++ CLR Console App, name the app “Count Aliens” or whatever you want:

image

Now add the following code, and run the application, you will get an error.  Oh-oh, an error.  Don’t you hate that?  Scroll down below the code to see the error and fix.

ref class Aliens
{
public:
    int appendages;

//The variable AlienID is private and requires that
// must be added to the other functions to manipulate the value of AlienID
private:
    String ^AlienID;
    //method declarations or implementation lines
    void SetAlienName(String ^san)
    {
        AlienID=san;
    }
    String^ GetAlienID()
    {
        return AlienID;
    }
    //end of implementation lines
};

int main(array<System::String ^> ^args)
{
    Aliens Venus, Mars, Jupiter;
    Venus.appendages=8;
    Mars.appendages=9;
    Jupiter.appendages=50;
    Venus.SetAlienName("Queen Bee");
    Mars.SetAlienName("BooBooSonny");
    Jupiter.SetAlienName("Super Big Monster");
    Console::WriteLine("Alien 1");    
    Console::Write("Name:   ");
    Console::WriteLine(Venus.GetAlienID());
    Console::WriteLine("Appendages: ");
    Console::WriteLine(Venus.appendages);
    Console::WriteLine("Alien 2");    
    Console::Write("Name:   ");
    Console::WriteLine(Mars.GetAlienID());
    Console::WriteLine("Appendages: ");
    Console::WriteLine(Mars.appendages);
    Console::WriteLine("Alien 3");    
    Console::Write("Name:   ");
    Console::WriteLine(Jupiter.GetAlienID());
    Console::WriteLine("Appendages: ");
    Console::WriteLine(Jupiter.appendages);  

 

    return 0;
}

 

Here is the fix for the error:

The dialog box you get when you hover over one of the squiggly red lines by the period shows that you have an error with a useful statement.  The dialog says that the “SetAlienName” could be called but not considered because it is inaccessible.  What does that mean? See below the image.

 

image

The code looks like and the lines that start with void SetAlienName(String ^san) and the line with String^ GetAlienID() need to be in the public component of the inline class.  The String ^AlienID; should stay private.

Wrong (and the way the code is written)

private:
    String ^AlienID;
    //method declarations or implementation lines
    void SetAlienName(String ^san)
    {
        AlienID=san;
    }
    String^ GetAlienID()
    {
        return AlienID;
    }

Right (change your code to this)

public:
    int appendages;

    void SetAlienName(String ^san)
    {
        AlienID=san;
    }
    String^ GetAlienID()
    {
        return AlienID;
    }