Anda di halaman 1dari 12

ASSIGNMENT NO:-4

SUBMITTED BY:RAJA KUMAR RALL NO:-A18 SEC:-D6912

SUBMITTED TO:G.S.M.THAKUR

PART A Q1This 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 Soln: C++ uses a unique keyword called this to represent an object that invokes a member function this is a pointer that points to the object for which this function was called. This unique pointer is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all the member functions. Consider the following simple example: Class ABC { Int a; .. }; The private variable a can be used directly inside member function, like A=123; We can also use the following statement to do the same job; this-> a=123; since C++ permits the use of shorthand form a=123, we have not been using the pointer this when overloading the operators using member function. When a binary operator is overloaded using a member function, we pass only one argument to the function. this is to return the object it points to. For example, the statement Return *this; Inside a member function will return the object that invoked the function. This statement assumes importance when .Example: Person & person :: greater(person & x) { if ( x.age >age) return x; // argument object else return *this; // invoking object }

Suppose we invoke this function by the call Max= A.greater(B); The function will return the object B(argument object) if the age of the person B is greater then that of A, A complete program to illustrate the use of this is given in the below program: #include<iostream.h> #include<string .h> Class person { Char name[30]; Float age; Public: Person(char *s, float a) { Strcpy(name,s); age=a; } Person & person:: greater (person & x) { If(x.age >= age) { Return x; } Else Return *this; } Void display(void) { Cout<<name: << name << \n; Cout<<age:<< age<<\n; } }; Int main() { Person p1(shael 37.50), P2(rj 29.0), P3(robin 0.25); Person p=p1.greater(p3); Cout<elder person is: \n; p.display(); p=p1.greater(p2); cout<<elder person is: \n;

p.display(); return 0; } Q2: Write a program to show the arithmetic operations on pointers. Soln: #include<iostream.h> #include<conio.h>

Void main() { Int num[]={56,75,22,55,66}; Int *ptr; Int I; Clrscr(); Cout<<the array values are:\n; For(i=0;i<5;i++) { Cout<<num[i]<<endl; Ptr=num; Cout<<\n value of ptr :<<*ptr; Cout<<endl; Ptr++; Cout<<\n the value of ptr++ :<<*ptr; Cout<<endl; Ptr--; Cout<<\n the value of ptr-- :<<*ptr; Cout<<endl; Ptr=ptr+2; Cout<<\n the value of ptr+2 :<<*ptr; Cout<<endl; Ptr=ptr-1; Cout<<\n the value for ptr-1 :<<*ptr; Cout<<endl; Ptr+=3; Cout<<\n value of ptr+=3 :<<*ptr; Cout<endl; Ptr-=2; Cout<<\n value of ptr-=2 :<<*ptr; Cout<<endl; Getch(); }

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

Soln: Array is a collection of similar data types. If we want to allocate the size of the array at the array concept. Example:-) #include<iostream.h> #include<conio.h> Void main() { Clrscr( ); Int *a, b; { Cout<<enter the length <<endl; Cin>>a; a= new int[n]; for(i=0;i<b;i++) { Cout<<enter the marks of the subject <<i+1<<student; } For(i=0;i<b;i++) { If(a[i]>a[i+1]) { Int t=a[i]; a[i]=a[i+1]; a[i+1]=t; } } for(i=0;i<b;i++) { Cin>>a[i]; } getch( ); } Output: Enter the length 2 Enter the marks of the student 1 Enter the marks of the student 2 Enter the marks of the student 3 Enter the marks of the student 4 55 76 56

PART B

Q4 What is the basic difference between manipulators and ios member functions implementation? Give examples. Soln: Manipulators:Manipulators are the operators used for formatting the output. Manipulators does not return such format state and so we cannot use them rapidly. Manipulators are the special types of function that changes the characteristics of the input and output. The data is manipulated by the programmers choice of display. Manipulators are: Setw( ) Setfill( ) Setf( ) Setprecision( ) Example: #include<iostream.h> #include<iomanip.h> Void main() { Int value; Clrscr(); Cout<<enter the value<<endl; Cin>>value; Cout<<value in decimal<<setbase(10)<<value<<endl; Cout<<value in octal<<setbase(12)<<value<<endl; Cout<<value in hexadecimal<<setbase(16)<<value<<endl; getch(); } Output: Enter the value:- 14 Value in decimal 14 Value in octal 16 Value in hexadecimal b In case of the ios class functions are setwidth, setprecision, setfill, setf.

Width

#include<iostream.h> #include<conio.h> Void main() { Int x; Cin>>x; Cout.width(5); Cout<<x; getch(); } Output = ---10 If x= 10 Precision:-) #include<iostream.h> #include<conio.h> Void main() { Float x; Cin>>x; Cout.precision(3); Cout<<x; getch( ); } Fill:-) #include<iostream.h> #include<conio.h> Void main() { Int x; Cin>>x; Cout.fill(*); Cout.width(6); Cout<<x; getch( ); } Setf is used to set the flags. OUTPUT: ***20 If x=20 Q5: Both cin and getline() can be used for reading a string. Comment.

Soln: :- getline function is the member function of the istream class i.e input class. In case of cin the cin is an object of the class istream that represents the standard input stream. #include <iostream> #include <fstream> #include <stdlib> // for exit function // This program reads values from the file 'example.dat' // and echoes them to the display until a negative value // is read. int main() { ifstream indata; // indata is like cin int num; // variable for input value indata.open("example.dat"); // opens the file if(!indata) { // file couldn't be opened cerr << "Error: file could not be opened" << endl; exit(1); } indata >> num; while ( !indata.eof() ) { // keep reading until end-of-file cout << "The next number is " << num << endl; indata >> num; // sets EOF flag if no value found } indata.close(); cout << "End-of-file reached.." << endl; return 0; } It corresponds to the stdio (output stream)stream stdin(input stream). This terminates when either space or enter is pressed. #include <fstream> #include <iostream> #include <string> int main () { string data; ofstream outfile; outfile.open("file.dat"); cout << "Writing to the file" << endl;

cout << "Enter class name: "; getline(cin, data); outfile << data<< endl; cout << "Enter your id: "; cin >> data; cin.ignore(); outfile << data<< endl; outfile.close(); ifstream infile; cout << "Reading from the file" << endl; infile.open("file.dat"); getline(infile, data); cout << data << endl;

getline(infile, data); cout << data << endl; infile.close(); return 0; } Cin example } include <iostream> using std::cout; using std::endl; using std::cin; using std::boolalpha; #include <string> using std::string; void display( const string & ); int main() { string string1; cout << "Statistics before input:\n" << boolalpha; display( string1 ); cout << "\n\nEnter a string: "; cin >> string1; // delimited by whitespace cout << "The string entered was: " << string1;

cout << "\nStatistics after input:\n"; display( string1 ); return 0; } void display( const string &stringRef ) { cout << "capacity: " << stringRef.capacity() << "\nmax size: " << stringRef.max_size() << "\nsize: " << stringRef.size() << "\nlength: " << stringRef.length() << "\nempty: " << stringRef.empty(); }

Q 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. Soln: The difference is that we can open a single file with the constructor function at a time with a single stream, We can open multiple file with the same stream using open function. Ex: - using constructor:-) //Writing on a file #include<fstream.h> Void main() { Ofstream outfile(result); Outfile<<shael<<\n; Outfile<<marks<<\n; Outfile<<500<<\n; Outfile.close(); } //Reading from the file #include<fstream.h> Void main() { Ifstream infile(result); Char name[40], name1[80], name2[80]; Infile>>name; Infile>>name1; Infile>>name2;

Cout<<name<<name1<<name2<<endl; Infile.close() } Using open function Now the multiple files can be open through a single stream using open function Ex: #include<fstream.h> Void main() { Ofstream outfile; Outfile.open(file1); Outfile<<shael<<\n; Outfile.close(); Outfile.open(file2); Outfile<<rj<<\n; Outfile<<robin<<\n; Outfile.close(); } //Reading the file:-)

Ifstream infile; Infile.open(file1); Cout<<the contents of file1 are<<endl; While(infile) { Infile.getline(line, 80); Cout<<line<<endl; } Infile.close(); Infile.open(file2); Cout<<the contents of the file2 are<<endl; While(infile) { Infile.getline(line, 80); Cout<<line<<endl; } }

Anda mungkin juga menyukai