Lecture # 14
Lecture # 14
Lecture # 14
Course Instructor:
Engr. Tariq
Outline
Array as member
Array of objects
of class
st[0] object
name
marks elements
Example
#include<iostream.h>
class Employee
{
int Id, Age;
string name;
long Salary;
public:
void GetData() {
cout<<"\n\tEnter Employee Id : ";
cin>>Id;
cout<<"\n\tEnter Employee Name : ";
cin>>Name;
cout<<"\n\tEnter Employee Age : ";
cin>>Age;
CONTD…
cout<<"\n\tEnter Employee Salary : ";
cin>>Salary; }
void PutData() { cout<<"\n"<<Id<<"\t"<<Name<<"\
t"<<Age<<"\t"<<Salary; }
}; //End of class
int main() {
Employee E[3];
for(int i=0;i<3;i++) {
cout<<"\nEnter details of "<<i+1<<" Employee";
E[i].GetData(); }
cout<<"\nDetails of Employees";
for(int i=0;i<3;i++)
E[i].PutData(); }
Passing objects as
arguments
• We can pass class’s objects as
arguments and return them from a
function the same way we pass and
return other variables.
• To pass an object as an argument we
write the object name as the argument
while calling the function the same way
we do it for other variables.
• SYNTAX:
• function_name(object_name);
EXAMPLE
#include<iostream.h>
class weight {
int kilogram, gram;
public:
void getdata ();
void putdata ();
void sum_weight (weight,weight) ;
} ;
void weight :: getdata() {
cout<<"/nKilograms:"; cin>>kilogram;
cout<<"Grams:"; cin>>gram;
}
void weight :: putdata () {
cout<<kilogram<<" Kgs. and"<<gram<<" g.\n";
}
void weight :: sum_weight(weight w1,weight w2) {
gram = w1.gram + w2.gram;
kilogram=gram/1000;
gram=gram%1000;
kilogram+=w1.kilogram+w2.kilogram;
}
int main () {
weight w1,w2,w3;
cout<<"Enter weight in kilograms and grams\n";
cout<<"\n Enter weight #1" ;
w1.getdata();
cout<<" \n Enter weight #2" ;
w2.getdata();
w3.sum_weight(wl,w2);
cout<<"Total Weight = ";
w3.putdata();
return 0;
}
Inline functions in C++
• C++ inline function is powerful concept that
is commonly used with classes. If a function
is inline, the compiler places a copy of the
code of that function at each point where the
function is called at compile time.
• Any change to an inline function could
require all clients of the function to be
recompiled because compiler would need to
replace all the code once again otherwise it
will continue with old functionality.
CONTD…
• To inline a function, place the
keyword inline before the function name and
define the function before any calls are made to
the function. The compiler can ignore the inline
qualifier in case defined function is more than a
line.
• A function definition in a class definition is an
inline function definition, even without the use of
the inline specifier.
• The main advantage of inline function is to make
program faster.
CONTD…
• Inline function should be declared
before main().
• Only shorter code should be made
inline, if longer code is made inline
then compiler ignores the request and
executes as normal function.
• It is wise to define large functions
outside class using scope resolution
operator otherwise they will become
inline automatically.
Example
#include <iostream>
using namespace std;
inline int Max(int x, int y) {
return (x > y)? x : y;
}
int main() {
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) <<
endl;
return 0;
}
THE END