Saturday, April 19, 2025

C++ OO Objects As Parameters

We can pass objects as parameters to functions. When an object is passed as a parameter to a function, a copy of the object is created and passed to the function. This copy is then used within the function and any modifications made to the object will not affect the original object passed as the argument. 


#include <iostream>

using namespace std;

class Prim{
    public:
        void printMe() {
            cout << "From Prim Cls" << endl;
        }
};

class Sec : public Prim{
    public:
        void printMe() {
            cout << "From Sec Cls" << endl;
        }
};

void caller(Prim x) {
    x.printMe();
}

int main() {

    Sec Object;
    caller(Object);

    return 0;
}

Ours C++ code defines two classes, Prim and Sec, where Sec is a derived class of Prim.

Prim has a public member function called printMe() that simply outputs a message saying "From Prim Cls". Sec also has a public member function called printMe(), but it overrides the implementation of printMe() in the Prim class and outputs a different message, saying "From Sec Cls".

The code also defines a function called caller() that takes an object of type Prim as a parameter and calls its printMe() method.

In the main() function, an instance of Sec called Object is created, and the caller() function is called with Object as an argument. Since Sec is a subclass of Prim, an instance of Sec can be treated as an instance of Prim.

However, since the caller() function takes an object of type Prim as a parameter, the printMe() method of the Prim class is called, not the printMe() method of the Sec class. 


#include <iostream>

using namespace std;

class Prim{
    public:
        void printMe() {
            cout << "From Prim Cls" << endl;
        }
};

class Sec : public Prim{
    public:
        void printMe() {
            cout << "From Sec Cls" << endl;
        }
};

class Third : public Sec{
    public:
        void printMe() {
            cout << "From Third Cls" << endl;
        }
};

void caller(Prim x) {
    x.printMe();
}

void callerT(Sec y) {
    y.printMe();
}

int main() {

    Third Object;
    callerT(Object);

    return 0;
}

This C++ code defines three classes, Prim, Sec, and Third, where Sec is a derived class of Prim and Third is a derived class of Sec.

Prim has a public member function called printMe() that outputs a message saying "From Prim Cls". Sec also has a public member function called printMe(), but it overrides the implementation of printMe() in the Prim class and outputs a different message, saying "From Sec Cls". Third also has a public member function called printMe(), but it overrides the implementation of printMe() in both the Prim and Sec classes and outputs a different message, saying "From Third Cls".

The code defines two functions called caller() and callerT(), each of which takes an object of a different class as a parameter and calls its printMe() method.

In the main() function, an instance of Third called Object is created, and the callerT() function is called with Object as an argument. Since Object is an instance of Third, it can be treated as an instance of both Sec and Prim, so the printMe() method of Sec is called.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...