0% found this document useful (0 votes)
7 views

Passing Objects: CSC-210: Object Oriented Programming

This document discusses object oriented programming concepts like passing objects, multiple constructors, returning objects from functions, nested member functions, and memory allocation for classes and objects. It provides examples using a Time class to demonstrate passing and returning objects, and nested member functions.

Uploaded by

Bilal Asad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Passing Objects: CSC-210: Object Oriented Programming

This document discusses object oriented programming concepts like passing objects, multiple constructors, returning objects from functions, nested member functions, and memory allocation for classes and objects. It provides examples using a Time class to demonstrate passing and returning objects, and nested member functions.

Uploaded by

Bilal Asad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CSC-210: Object Oriented Programming

Passing Objects
Lecture 04

By: Ms. Zupash Awais


Bahria University Lahore Campus
 C + + permits to use more than one constructors in a
single class.
Multiple
Constructors  Add( ) ; // No arguments
 Add (int, int) ; // Two arguments
class add
{
int m, n ; • The first constructor receives no
public : arguments.
add ( )
{m = 0 ; n = 0 ;} • The second constructor receives two
add (int a, int b) integer arguments.
{m = a ; n = b ;}
add (add i) • The third constructor receives one add
{m = i.m ; n = i.n ;} object as an argument.
};
Passing Objects
in Function/
Constructor
Time
- hour : int
- minute : int
- second : int

Class Design + setTime() : void


+ getTime() : void
+ addTime(Time x, Time y) : void
class Time
{
int hour, minute, second;
public :
void setTime(){
cout<<"\nEnter hours:";cin>>hour;
cout<<"Enter Minutes:";cin>>minute;
cout<<"Enter Seconds:";cin>>second;
}
Example (Time void getTime(){
cout<<"\nhour:"<<hour;
Class) cout<<"\tminute:"<<minute;
cout<<"\tsecond:"<<second;
}
void addTime(Time x, Time y){
hour = x.hour + y.hour;
minute = x.minute + y.minute;
second = x.second + y.second;
}
};
int main()
{
Time t1,t2,t3;

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);

Here, hour, minute and second represents data of object t3


because this function is called using code 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;

Time addTime(Time x, Time y){


Time t;
t.hour = x.hour + y.hour;
t.minute = x.minute + y.minute;
t.second = x.second + y.second;
return t;
}
};
int main()
{
Time t1,t2,t3,ans;

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

Memory created when, Functions defined

Object 1 Object 2 Object 3


Member variable 1 Member variable 1 Member variable 1

Member variable 2 Member variable 2 Member variable 2

Memory created when Object created

You might also like