Object Oriented Programming: Chapter 2: Classes and Objects

Objects as Function Arguments

C++ Programming | Object Oriented Programming

The objects can be explicitly passed as an argument to the function.

Objects as Function Arguments

•  The objects can be explicitly passed as an argument to the function.

•  Note that in this case, we have to write the constructor explicitly. Because when an object is created as an instance of a class then the constructor is invoked.

•  Following is a simple program in which we are passing the objects as parameter and returning the object.

#include<iostream>

using namespace std;

class point

{

private:

int x,y;

public:

point(int a,int b)

{

    x=a;

    y=b;

}

point()

{

    x=0;

    y=0;

}

point add(point px,point py);

void display();

};

point point::add(point px,point py)

{

    point temp;

    temp.x=px.x+py.x;

    temp.y=px.y+py.y;

    return temp;

}

void point::display()

{

    cout<<"x= "<<x<<" y= "<<y;

}

void main()

{

    point p1(10,20);

    point p2(100,200);

    point p3;

    p3=p3.add(p1,p2);   //Passing two objects p1 and p2

     p3.display();

}

Output

x= 110 y= 220

Program Explanation :

• In above program the class named point is created. In this class the two integers  namely x and y represent the coordinates of the point.

• In the main() function, we create the two instances(objects) of the class point ‒ p1 and p2 respectively. When these objects are created then the constructor point(int a,int b) is invoked. Due to which the values (10,20) and (100,200) gets assigned to two points.

•  The function add is for making the addition of co‒ordinates of the points. For that purpose the two objects p1 and p2 are passed as parameters to the function add. This function returns the addition in the third object p3. Hence finally the values x and y of object p3 are displayed using display function.

 

Object Oriented Programming: Chapter 2: Classes and Objects : Tag: : C++ Programming | Object Oriented Programming - Objects as Function Arguments


Object Oriented Programming: Chapter 2: Classes and Objects



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation