Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

4th Assignment

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

Annexure

Homework type/no: 4 Course code: Cse202

Course instructor: Deepak Sir course tutor:

Date of allotment: December 2009 Date of submission: 9/12/2009

Student roll no:B48 Section no:C2801

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 been written for me another person.

Student’s signature:

Evaluator’s comments:

Marks obtained _____________________ out of ______________________________

Content of home work should start from this page only:

Question 1: “this pointer is a very useful concept in developing a program”. Justify this
statement with the help of an example implementing the usage of “this pointer”

Ans: We know that a pointer is a variable which holds the memory address of another
variable.Using pointer we can access the data of another variables directly.The this
pointer is a variable which is used to access the address of the class itself.It contains the
address of the object that generates the call.It is implicitly defined in each member
function and its data type is that of a pointer to the class type.The this pointer may have
return data items to the caller.This unique pointer is automatically passed to a member
function when it is called..

#include<iostream.h>

#include<conio.h>

class rectangle
{

int l,b; //data members

Public:

Void getdata(int i,int j) //member function declaration and definition

cout<<”Address of object that generates getdata() call:”<<this<<endl;

this->l=i; //equivalent to l=i

this->b=j; //equivalent to b=j

void area() //member function declaration and definition

cout<<”Address of object that generates area() call=”<<this<<endl;

cout<<”Area=”<<this->l*this->b;

}; //end of class rectangle

int main()

clrscr();

rectangle r; //class rectangle’s object created

r.getdata(10,15);

r.area();

getch();

return 0;

}
Question 2: Write a program to show the arithmetic operations on pointers

Ans: #include<iostream.h>

#include<conio.h>

void main()

int a[5]={5,6,7,8,9}; //array of objects

int *p; //pointer is declared

int i;

clrscr();

cout<<”the array values are: \n”;

For(i=0;i<=4;i++)

cout<<a [i]<<”\n”;

p=a;

cout<<” value of p:”<<*p<<endl;

p++;

cout<<”after increment value of p++ :”<<*p<<endl;

p--;

cout<<”Afetr decrement value of p--:”<<*p<<endl;

p=p+1;

cout<<”After addition of 1 value of p+1:”<<*p<<endl;

p=p-2;

cout<<”After subtraction of 2 value of p-2:”<<*p<<endl;

getch();

}
Question 3: Write a program that dynamically allocates memory for an array of size n,
reads elements of array and sorts them in ascending order.

Ans:

#include <iostream.h>

#include<conio.h>

int main()

int numElements; // To hold the number of elements to allocate

int *ptr; // A pointer to the array

int count; // A loop counter

cout << "Enter the array size: " << endl;

cin >> numElements;

if (numElements <= 0)

return NULL;

// Allocate the array.

ptr = new int[numElements];

cout << "Enter the value of the elements\n";

for (count = 0; count < numElements; count++)

cout << (count + 1) << ": ";

cin >> ptr[count];

//Return a pointer to the array.

cout << "The array holds the numbers we entered: \n";


for (count = 0; count < numElements; count++)

cout << *ptr << " ";

ptr++;

//Free dynamically allocated memory.

delete [] ptr;

ptr = 0; //makes elements NULL.

return 0;

PART B

Question 4: What is the basic difference between manipulators and ios member
functions implementation? Give examples.

Ans: ios class functions & flags: -It contains a large no. of member functions that would
help us to format the o/p in a no. of ways. To implement many of the manipulators,I/O
streams have a flag field that specifies the current settings.for example:

Flag name : Meaning:

Skipws skip white space during input

Dec decimal base

showbase show base for octal and hexadecimal

numbers

A program to display an integer number in different bases,namely,dec,oct,and hex


using the basefield format flag settings

#include<iostream.h>

#include<conio.h>
void main()

int num=71;

cout<<”decimal=”<<num<<endl;

cout.setf(ios::oct,ios::basefield);

cout<<”octal=”<<num<<endl;

cout.setf(ios::hex,ios::basefield);

cout<<”hexadecimal=”<<num<<endl;

cou.setf(ios::dec,ios::basefield);

cout<<”decimal=”<<num<<endl;

Manipulators:-Manipulator functions are special stream functions that change certain


characteristics of the input and output streams.They change the format flags and values
for a stream.The main advantage of using using manipulator functions is that they
facilitate the formatting of input and output streams.Some of standard manipulators are as
follows:

setw (), setprecision (), setfill (), setiosflags(), resetiosflags(),setbase()etc

A program to display the data variables using setw and setprecision and setfill
manipulator functions

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

void main()

int a,b,c;

a=300;
b=200;

c=a/b;

cout<<setfill(‘*’);

cout<<setw(5)<<a<<setw(5)<<b<<endl;

cout<<setw(6)<<a<<setw(6)<<b<<endl;

cout<<setprecision(1)<<c<<endl;

cout<<setprecision(2)<<c<<endl;

Question 5: Both cin and getline() can be used for reading a string. Comment

Ans: The getline() function is a line-oriented input function which reads a whole line of
text that ends with a newline character. This function can be invoked by using the object
cin as follows:

Cin.getline(line,size);

The getline() function is invoked by the above function call which reads character input
into the variable line. The reading is terminated as soon as either the newline character
‘\n’ is encountered or size-1 characters are read. The newline character is read but not
saved. Instead, It is replaced by the null character.

Cin can read strings that don’t contain white spaces.this means that cin can read just one
word and not a series of words with spaces in between.

#include<iostream.h>

#include<conio.h>

int main()

{
int size=15;

char name[15];

cout<<”enter name: “ ;

cin>>name;

cout<<” name:”<<name<<endl;

cout<<”enter the name again:”;

cin.getline(name,size);

cout<<”the name is now:”<<name<<endl;

return 0;

Question 6: What is the difference between opening a file with a constructor function
and opening a file with open() function? When is one method preferred over other?
Illustrate with suitable example.

Ans: The open() function negotiates with the operating system to locate the file specified as
argument on the disk and establish the connection between that file and your file stream object.

This is made by calling a constructor of the respective stream class that takes the file name as
parameter.

ifstream inp_file(“sample.txt”);

If we have to perform the same operation with constructors, then we have to create separate
objects for opening different files . The method involving open() function is preferred over one
invoking constructor as it can work both for single file and multiple files without need to recreate
object.

the function open() can be used multiple files that use the same stream object.for
example , we may want to process a set of files sequentially. In such cases,we may create
a single stream object and use it to open each file in turn.

Syntax:
File-stream-class stream-object;

Stream-object.open(“filename”);

Example: ifstream infile; // create stream

infile.open(“d”);//connect stream to d

…….

…….

infile.close();// disconnect stream from d.txt

infile.open(“d1”);//connect stream to d1

……..

……..

infile.close(); //disconnect stream from d1

……

……

Where as in constructor ,a filename is used to initialize the file stream object. Few steps
are there i.e

1) Firstly,we create a file stream object to manage the stream using the appropriate
class.

2) Then we Initialize the file object with the any filename.

You might also like