Sunday, April 4, 2021

Passing object as the function arguments (OOP) (12)

                              /*passing object as function arguments

                                  ************************************/

#include<iostream>

using namespace std;

class time //making the class

{

private: //private part of the class that mostly consist of data 

int h,m; //declear the two integer h for hours and m for minuite

public: // this is the public part of the that mostly consist of the member function

void gettime(int,int);

void puttime();

void sum(time,time); //in this function we pass the object as a arguments and when we pass 

//the object as the function arguments then the at the place of data type we write the class name

};

void time::gettime(int x,int y) //here we defining our class function

{

h=x;

m=y;

}

void time::puttime() //here we define our class function

{

cout<<"hours"<<h<<endl;

cout<<"minutes"<<m<<endl;

}

void time::sum(time t1,time t2) //method of passing object as the function arguments

{

m=t1.m+t2.m;

h=m/60;

m=m%60;

h=h+t1.h+t2.h;

}

int main()

{

time t1,t2,t3;

t1.gettime(3,40); //pass the two integer argument in this function

t2.gettime(4,30); //pass the two integer argument in this function

t3.sum(t1,t2); //passing object as the function arguments

t1.puttime();

t2.puttime();

t3.puttime();

}

No comments:

Post a Comment