You are on page 1of 10

Homework Title / No.

: _______HARUN_________Course Code : ___CAP(320)___

Course Instructor : ____MISS_SUCHARU______ Course Tutor (if applicable) : ____________

Date of Allotment : _____________________ Date of submission : ____4/3/2011_______

Student’s Roll No.____RE3004B54____ Section No. : _____E30004_____

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.

Student’s Signature : __HARUN___

Evaluator’s comments:
_____________________________________________________________________

Marks obtained : ___________ out of ______________________

Content of Homework should start from this page only:


PART-A

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

cout<<”Enter the limit of array:”;

cin>>n;

cout<<”Enter the element of array:\n”;

for(i=0;i<n;i++)

cin>>array[i];

cout<<”Sorted array is:\n”;

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

Q2. Elaborate on the importance of pointers in c++.

Ans:-

• If we want our object to be modify by some function then we have only option to
pass its address or reference

• It can implement run time polymorphism(virtual...function)

• It can implement garbage collection like reference counting

• 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 can return more then 1 value from a function

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

cout<<”value of x before function call=”<<n<<”\n”;

func(n);

cout<<”value of x after function call=”<<n<<”\n”;

getch();

int func(int n)

int m;

m=n*10;

cout<<”value of x inside function =”<<m<<”\n”;

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

• A linked list is constructed using pointers

• A linked list often consists of structs or classes that contain a pointer


variable connecting them to other dynamic variables

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

cout<<”enter total no of nodes:”;

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.

Ans:- C++ virtual function is,

• A member function of a class


• Declared with virtual keyword
• Usually has a different functionality in the derived class
• A function call is resolved at run-time
• The concept of c++ virtual functions is different from C++ Function
overloading.

The difference between a non-virtual c++ member function and a virtual


member function is, the non-virtual member functions are resolved at compile time. This
mechanism is called static binding. Where as the c++ virtual member functions are
resolved during run-time. This mechanism is known as dynamic binding.

Example:-

#include<iostream.h>

class baseA{

public:

virtual void display()

cout<<”manish \t”;

};

class derivedB : public baseA

public:

virtual void display()

cout<<”kumar \t”;
}

};

class derivedC : public derivedB

public:

virtual void display()

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

};

assign::assign(int one,float two)

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;

cout<<”contents of 1st obj\n”;

o1.display();

cout<<”contents of 2nd obj\n”;

o2.display();

You might also like