Professional Documents
Culture Documents
Declaration:
I declare that this assignment is my individual work. I have not copied from any other student’s work or
from any other source except where due acknowledgment is made explicitly in the text, nor has any part
been written for me by another person.
Evaluator’s comments:
_____________________________________________________________________
Q1. Wap to sort the given array using bubble sort technique.
Ans:-
#include<iostream.h>
#include<conio.h>
void main();
int array[20],i,j,temp,n;
clarscr();
cin>>n;
for(i=0;i<n;i++)
cin>>array[i];
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(ar[j]>ar[j+1])
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
for(i=0;i<n;i++)
cout<<ar[i]<<”\n”;
getch();
Ans:-
• If we want our object to be modify by some function then we have only option to
pass its address or reference
• If want to share some object among classes we can have the pointer of that
object
in each interested classes.
• In C++ the use of pointers allows to hold the address of the variable or the literal.
This address can then be passed from one function to another function. This
whole process is done without the help of multiple variable declarations.
• Using pointer we do not need to pass the entire object to a function. We have to
pass just the address
•
Example:-
#include<iostream.h>
#include<conio.h>
void main();
int n;
int func(int);
n=10;
func(n);
getch();
int func(int n)
int m;
m=n*10;
return(m);
}
Q3. Explain with example how pointers are useful in linked lists.
Ans:-
• A linked list is a list that can grow and shrink while the program is
running
• Every nodes of linked list contain the data item(s) and a pointer that
can point to another node of the same type
• The pointers point to the entire node, not an individual item that might
be in the node
Example:-
#include<iostream.h>
#include<conio.h>
struct node
int info;
node *link;
};
int main();
int I,n,item;
node *ptr,*start;
cin>>n;
ptr=new node;
start=ptr;
for(i=1;i<=n;i++)
cout<<”enter value:”;
cin>>item;
ptr->info=item;
if(i==n)
ptr->link=NULL;
else
ptr->link=new node;
ptr=ptr->link;
ptr=start;
while(ptr!=NULL)
cout<<ptr->info<<”\n”;
ptr=ptr->link;
getch();
return(0);
PART-B
Q4. What are virtual functions? Explain with programming example.
Example:-
#include<iostream.h>
class baseA{
public:
cout<<”manish \t”;
};
public:
cout<<”kumar \t”;
}
};
public:
cout<<”singh\t”;
};
void main()
baseA o1;
derivedB o2;
derivedC o3;
base *ptr[3];
ptr[0]=&o1;
ptr[1]=&o2;
ptr[2]=&o3;
for(int i=0;i<=2;i++)
ptr[i]->display();
}
Q5.WAP to show the overloading of assignment operator.
Ans:-
#include<iostream.h>
class assign{
private:
int x;
float y;
public:
assign(int,float);
void display();
};
x=one;
y=two;
void assign::display()
cout<<”integer number(x)=:”<<x<<”\n”;
cout<<”floating number(y)=:”<<y<<”\n”;
void main()
assign o1(5,-5.5);
assign o2(10,-10.5);
o2=o1;
o1.display();
o2.display();