4th Assignment
4th Assignment
4th Assignment
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:
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
{
Public:
cout<<”Area=”<<this->l*this->b;
int main()
clrscr();
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 i;
clrscr();
For(i=0;i<=4;i++)
cout<<a [i]<<”\n”;
p=a;
p++;
p--;
p=p+1;
p=p-2;
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()
if (numElements <= 0)
return NULL;
ptr++;
delete [] ptr;
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:
numbers
#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;
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;
cin.getline(name,size);
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”);
infile.open(“d”);//connect stream to d
…….
…….
infile.open(“d1”);//connect stream to 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.