Passing Objects: CSC-210: Object Oriented Programming
Passing Objects: CSC-210: Object Oriented Programming
Passing Objects
Lecture 04
t1.setTime();
t1. getTime();
t2.setTime();
Example (Time t2. getTime();
Class) … t3.addTime(t1,t2);
cout<<"\nafter adding two objects";
t3.gettTime();
return 0;
}
t3.addTime(t1,t2);
Function Declaration
void addTime(Time x, Time y)
{
hour = x.hour + y.hour;
minute = x.minute + y.minute;
second = x.second + y.second;
}
Define class Complex with members real and
imaginary . Also define function to setdata() to
initialize the members, print() to display values and
Activity addnumber() that adds two complex objects.
Returning
Objects
class Time
{
int hour, minute, second;
public :
void setTime(){
cout<<"\nEnter hours:";cin>>hour;
cout<<"Enter Minutes:";cin>>minute;
cout<<"Enter Seconds:";cin>>second;
}
void getTime(){
Example (Time cout<<"\nhour:"<<hour;
cout<<"\tminute:"<<minute;
Class) }
cout<<"\tsecond:"<<second;
t1.setTime();
t1. getTime();
t2.setTime();
Example (Time t2. getTime();
Class) … ans = t3.addTime(t1,t2);
cout<<"\nafter adding two objects";
ans.gettTime();
return 0;
}
A member function of a class can be called by an
object of that class using dot operator.
A member function can be also called by another
member function of same class.
This is known as nesting of member functions.
Nested
Member void set_values (int x, int y)
{
Functions width = x;
height = y;
printdata();
}
class rectangle{
int w,h;
public:
void setvalue(int ww,int hh)
{
w=ww;
h=hh;
displayvalue();
}
Example void displayvalue()
{
cout<<"width="<<w;
cout<<"\t height="<<h; int main(){
} rectangle r1;
}; r1.setvalue(5,6);
r1.displayvalue();
return 0;
}
The member functions are created and placed in
the memory space only once at the time they are
defined as part of a class specification.
No separate space is allocated for member functions
Memory when the objects are created.
Allocation Only space for member variable is allocated
separately for each object because, the member
variables will hold different data values for different
objects.
Common for all objects
Member function 1
Member function 2