0% found this document useful (0 votes)
7 views19 pages

Lecture # 14

Uploaded by

264 HAMNA AMIR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views19 pages

Lecture # 14

Uploaded by

264 HAMNA AMIR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Classes

Lecture # 14
Course Instructor:
Engr. Tariq
Outline
Array as member
Array of objects
of class

Passing objects as Returning objects


arguments Pointer to Objects
from functions

Inline functions Examples


Array as member of class
#include<iostream>
using namespace std;
class Sort
{ int arr[10];
public:
void get()
{
cout<<"Enter elements of array:";
for(int i=0;i<10;i++)
cin>>arr[i];
}
CONTD…
void sort()
{
int min;
for(int i=0;i<10;i++)
{
min=arr[i];
for(int j=i;j<10;j++)
{
if(min>arr[j])
{
int temp=min;
CONTD…
min=arr[j];
arr[j]=temp;
}
}
arr[i]=min;
}
}
void display()
{
cout<<"\nAfter sorting:";
CONTD…
for(int i=0;i<10;i++)
cout<<arr[i]<<" ";
}
};
int main()
{
Sort s;
s.get();
s.sort();
s.display();
}
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.
• An array of objects is declared in the same
way as an array of any built-in data type.
• Syntax:
• class-name obj[ size ];
CONTD…

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

You might also like