Q:

C++ private variable access in main function

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Movie{
    string title;
    string genre;
    int rating;
public:
    Movie(string t, string g);
    void printDetail();
};

Movie :: Movie(string t, string g){
    title = t;
    genre = g;
    rating = rand()%10;      
}

void Movie :: printDetail(){
    cout <<" Title  : " <<title<<endl;
    cout <<"    Genre  : " <<genre<<endl;
    cout <<"    Rating : " <<rating<<endl;
}

And in the main I have the following:
int main()
{
   Movie M[5]={
        Movie("The Avengers","Action"),
        Movie("Infinity War","Action"),
        Movie("Gupigain Baghabain","Comedy"),
        Movie("Anonymous 616","Horror"),
        Movie("Sara's Notebook","Thriller")
        };

        for(int i=0;i<5;i++){
            cout <<" "<<i+1<<".";
            M[i].printDetail();
            cout<<endl;
        }

        M[0].rating; // Here i need to access to rating...!!

       // Here I need to compare which movies rating is highest..!! 
       // So I need to access rating variable...!! 
       // But how can I do this...!! 
       // There has a condition... That is I can't declare any other member function..!!!
}

Here I have to show which one's rating is highest and which one's rating is lowest..!!

A:

you can make getter and setter functions to access the private members indirectly
There is another hack which isn't standard : get the offest of the private member to the class head then get the address of the class and add the offest to it thus you have the address of the private member and can access it directly
This isn't supported by the standard c++ and you need to do the calculations yourself , in your case it isn't difficult
This method can be used when you want to get a structure or class from a dll to obtain a value of particular member and you don't have the definition of the structure but you can obtain the rva offest from the dll using various methods like disassembling some bytes of the dll 

