for Robot Artificial Inteligence

7.Reference and Enum-type

|

Example 1

#include <iostream>

using namespace std;
/* enum - enumeration - 0 1 2 3 4 */

enum dayOfWeek {M = 1, TU, W, TH, F, SA, SN};

string getDay(dayOfWeek);
int main()
{
    dayOfWeek d = M;

    cout << d << endl;

    cout << dayOfWeek(W) << endl;

    int i;
    cout << "Enter the day of a week " << endl;
    //....
    cin >> i;
    cout << getDay(dayOfWeek(i)) << endl;

    return 0;
}
string getDay(dayOfWeek d)
{
    switch(d)
    {
        case M:
            return "Monday";
        case TU:
            return "Tuesday";
        case W:
            return "Wednesday";
        case TH:
            return "Thursday";
        case F:
            return "Friday";
        case SA:
            return "Saturday";
        case SN:
            return "Sunday";
        default:
            return "You typed something wrong!";
    }
}

Example 2

#include <iostream>

using namespace std;
/*reference variables */
int main()
{
    string name_2 = "lala";
    string name = "Agnes";

    cout << "Value of Variable: " << name << " addres of Variable is: " << &name << endl;

    const string & nr_6 = name;


    cout << "Value of Variable: " << nr_6 << " addres of Variable is: " << &nr_6 << endl;

    //nr_6 = name_2;
    //nr_6 = "Scott";
    name = "Scott";


    cout << "Value of Variable: " << name << " addres of Variable is: " << &name << endl;

    cout << "Value of Variable: " << nr_6 << " addres of Variable is: " << &nr_6 << endl;

    /*
        Ref. var. must be initialized when created
        Ref var can't change associations / connections
        Ref var must be the same type as assocaited variable
    */

    return 0;
}

Example 3

#include <iostream>

using namespace std;

int & swap(int &, int &);

int main()
{
    int a = 10;
    int b = 20;

    int &c = swap(a, b);

    c = 100;

    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;

    return 0;
}
// when use it in class, in class they are not used in reference
// but when we call from outside of main class, it use in other class with &
// so when we called value from main to void, void use it reference
int & swap(int &x, int &y)
{
    int t;
    t = x;
    x = y;
    y = t;
    //x at the end is 20
    return x;
}

Example 4

#include <iostream>

using namespace std;

int main ()
{

    string name = "Cassandra";
    string &cassie = name;
    string &cassey = name;
    string &sandra = name;

    cout << name << " " << &name << endl;
    cout << cassie << " " << &cassie << endl;
    cout << cassey << " " << &cassey << endl;
    cout << sandra << " " << &sandra << endl;




    return 0;
}

Example 5

#include <iostream>

using namespace std;

void setValue(int&);

int main ()
{
    int a;

    setValue(a);

    cout << "Variable a is equal to: " << a;

    return 0;
}

void setValue(int &variableToSet)
{
    cout << "Input the value: ";
    cin >> variableToSet;
}


Example 6

#include <iostream>

using namespace std;

void checkIfTheSame(int&, int&);

int main ()
{

    int a = 4;
    int b = 3;
    int c = a;
    int &d = a;
    // take a reference a
    // so this d is same as a

    checkIfTheSame(a, b);
    checkIfTheSame(a, c);
    checkIfTheSame(a, d);

    return 0;
}

void checkIfTheSame(int &firstVariable, int &secondVariable)
{
    if (firstVariable == secondVariable && &firstVariable == &secondVariable)
    {
        cout << "The variables are completely the same !";
    }
    else if (firstVariable == secondVariable && &firstVariable != &secondVariable)
    {
		cout << firstVariable << " " << &firstVariable << endl;
        cout << secondVariable << " " << &secondVariable << endl;
        cout << "The variables have got same values but they are different variables !" << endl;
        // because c is have different reference
    }
    else
    {
        cout << firstVariable << " " << &firstVariable << endl;
        cout << secondVariable << " " << &secondVariable << endl;
        cout << "The variables are different !" << endl;
    }
}



Comments