You are on page 1of 10

OBJECT-ORIENTED lecture 8

PARADIGM Instructor : Syeda Hira Fatima


Array of objects
Like array of other user-defined data types, an array of type class can also be
created. The array of type class contains the objects of the class as its
individual elements. Thus, an array of a class type is also known as an array
of objects.
Class
MARKED LAB TASK 3
Write a program in C++ to maintain the record of 10 employees using array of
objects.
Your communication with the user should be proper.
This stores the following information about employees and displays accordingly.
i. employee id
ii. Employee name
iii. Employee CNIC
iv. Status married/unmarried
v. Age
Objects as function arguments in c++

The objects of a class can be passed as arguments to member functions as well as


nonmember functions either by value or by reference.
When an object is passed by value, a copy of the actual object is created inside the
function.
This copy is destroyed when the function terminates.
Moreover, any changes made to the copy of the object inside the function are not
reflected in the actual object.
On the other hand, in pass by reference, only a reference to that object (not the entire
object) is passed to the function.
Thus, the changes made to the object within the function are also reflected in the actual
object.
#include<iostream.h>
class weight { int main () {
int kilogram; weight w1,w2 ,w3;
int gram; cout<<"Enter weight in kilograms and grams\n";
public:
void getdata ();
cout<<"\n Enter weight #1" ;
void putdata ();
w1.getdata();
void sum_weight (weight,weight) ; cout<<" \n Enter weight #2" ;
}; w2.getdata();
void weight :: getdata() { w3.sum_weight(wl,w2);
cout<<"/nKilograms:"; cout<<"/n Weight #1 = ";
cin>>kilogram;
cout<<"Grams:"; w1.putdata();
cin>>gram; cout<<"Weight #2 = ";
} w2.putdata();
void weight :: putdata () { cout<<"Total Weight = ";
cout<<kilogram<<" Kgs. and"<<gram<<" gros.\n"; w3.putdata();
}
void weight :: sum_weight(weight w1,weight w2) { return 0;
gram = w1.gram + w2.gram; }
kilogram=gram/1000;
gram=gram%1000;
kilogram+=w1.kilogram+w2.kilogram;
}
Pass by reference
RETURNING OBJECTS FROM A
FUNCTION
int main()
Class {
Example E1, E2, E3;

// C++ program to show passing // Values are initialized


// of objects to a function // for both objects
E1.a = 50;
#include <bits/stdc++.h> E2.a = 100;
using namespace std; E3.a = 0;
class Example {
public: cout << "Initial Values \n";
int a; cout << "Value of object 1: " << E1.a
<< ", \nobject 2: " << E2.a
// This function will take << ", \nobject 3: " << E3.a
// object as arguments and
// return object << "\n";
Example add(Example Ea, Example
Eb) // Passing object as an argument
{ // to function add()
Example Ec; E3 = E3.add(E1, E2);
Ec.a = Ea.a + Eb.a;
// returning the object // Changed values after
return Ec; // passing object as an argument
} cout << "New values \n";
}; cout << "Value of object 1: " << E1.a
<< ", \nobject 2: " << E2.a
<< ", \nobject 3: " << E3.a
<< "\n";

return 0;
}

You might also like