C++: Assessing class members from main

This entry is part 52 of 61 in the series C++

We have always say that a public member of a class can by assessed by any functions.

A good way to look at it is in accessing it from main function.

In main, only public members can be accessed.  It would generate an error if you try to access a private or a protected member.

#include <iostream>

using namespace std;

class Music {
    
public:
    int x;
protected:
    int y;
private:
    int z;
    
};

int main() {
  
    Music music;
    
    music.x = 6;
    cout << music.x;
    
    // Main will not be able to access y & z.
    // music.y = 8;   //error
    // music.z = 10; //error
    
}

public

C++: The main function

This entry is part 3 of 61 in the series C++

We look at a simple program that basically does nothing.

In all C++ program, you need a starting main function similar to C.

The operating system such as your Windows OS will first call the main function when the program is executed.

The program below contains only one return statement wrapped inside 2 curly braces.

int main()
{
	return 0;
}

 

The main function is required to return type of int, which is a type that represents integers.  In this simple program, we simply return a value of 0 to it.

C#: Main method and its arguments args

This entry is part 34 of 71 in the series C#

Each time we generate a Console Application using Visual Studio, it will provide us with a template similar to below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainApp
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

You might be wondering what is args in the Main input parameter.

As in the declaration, args is a string array.  To find out what is args, you can add 2 lines to the above template.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(args[0]);
            Console.ReadLine();
        }
    }
}

After compiling and build the program, we run the MainApp in the console.

Type in the console MainApp Hello and you will get an output similar to the screenshot below.

mainapp

When you type MainApp Hello, the word Hello will be the first argument in the args array.

Since we are printing args[0] which is the first argument, the word Hello is printed out.

 

C#: The Main function

This entry is part 12 of 71 in the series C#

The Main method is the entry point of a C# console application or windows application. 

When the application is started, the Main method is the first method that is invoked.

using System;

namespace Hello
{
    class Program
    {
        static void Main()
        {
            string name;

            Console.WriteLine("Hello, what is your name?");
            name = Console.ReadLine();
            Console.WriteLine("Your name is " + name);
            Console.ReadLine();
        }
    }
}

Main must be static and it should not be public.

Static members are not tied to class instances.  Rather, they can be called without having an instance.

For example, to access the Review method, we create an instance called book.

Book book = new Book();
book.Review();

Now for the Main method, we do not have to create an instance to access it.  It will be invoked first and will be done so automatically.

hello