Tuesday, 28 June 2016

Guess the following output?

#include<iostream>

class FunctionOverloading
{

   public:
      int a;
      void callFunctionOverloading()
      {                         
         std::cout<<"Called Normal Function";
      }
 
  void callFunctionOverloading()const
      {
         std::cout<<"Called const Function Overloading";
      }
 
  void callFunctionOverloading()volatile
      {
         std::cout<<"Called Volatile Function Overloading";
      }
};

int main()
{
   FunctionOverloading b;
   b.callFunctionOverloading();
  
   return 0;
}


O/P:

Called Normal Function

Explanation:

Not only based on parameters of the function, you can overload the functions based on the const and volatile keyword.

Following functions have different meaning

void callFunctionOverloading()
void callFunctionOverloading()const
void callFunctionOverloading()volatile
void callFunctionOverloading() const volatile


1st function  will be called when  this function is called with normal objects. 2nd function will be called when this object is declared with const object and finally 3rd one will be called when the object is declared with volatile keyword.

Monday, 27 June 2016

Guess the output of the following program.

#include<iostream>

class VolatileClass
{

   public:
      int a;
      void callfunc()
      {
         std::cout<<"Called";
      }
};

int main()
{

   VolatileClass volatile a;
   a.callfunc();

   return 0;
}

Answer:

The above program will not compile because ,

Simple Answer:


Volatile objects  should call only volatile member functions.

Complex Answer:


If object is declared as volatile, then all member function which is called with that object should be declared as volatile

      void callme() volatile
      {
         std::cout<<"Called";
      }

So why it has to be volatile?

Note: Answer is based purely  on my point of view.

In, volatile is keyword which instructs the compiler like “Dear genius, don’t perform any optimization on that object”. Here for all member function this pointer is passed as hidden variable. Since object is declared as volatile, compiler should not perform any optimization on this pointer.

Consider one member function which is declared without volatile

      void callme()
      {
         this->a=1;
                // Perform some other activity.
         this->a=1;  
      }
Since compiler is genius, compile will make two statements “this->a=1;” as one statement because throughout the function value of a is 1. So second statement “this->a=1;” will be removed by the compiler.


Compiler will perform this kind of genius activity when volatile keyword is used in the member function.

Wednesday, 22 June 2016

Reading file in c++

Reading a file in c and c++ are different. In c we will use the file pointer and in c++ we use the streams.

Initializing object for file:

The file object can be intialzed by the following way

ofstream fileobj;
ofstream fileobj(“myfile.txt”,ios::in);

The first form just creates the object where as the second form creates the object as well as open the file with input operation mode.

Opening a file manually:

You can also open the file manually by calling the member function open.

fileobj.open(filename);
fileobj.open(filename,mode);

C++ supports both the  formats.  If mode is not passed then the default mode is applied

More than one mode can be  passed by using the pipe symbol.

fileobj.open(filename,ios::in | ios ::app );


Read a file using a overloaded operator <<

The << operator is overloaded to read a file.

file_obj >> buffer;

This  system reads the file line by line. You can use eof() function to check whether the system read the entire file.
Here the buffer can be a char array and also a string class object.

getline() function

Beaware of getline function here. There are two getline functions. Function which is defined under the string class and  another function is defined under the istream class. Both takes the different set of parameter.
getline() under string class

istream& getline (istream& is, string& str, char delim);
istream& getline (istream& is, string& str);


getline() under the istream class
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

Read() method of istream:

The read method reads the n number of character in the stream.

istream& read (char* s, streamsize n);

Its stores the result in the char array
file_obj.read(buffer_char,10);

Manipulation on file position:

use the seekg function to move beween the various positions of file. seekg is the member function of istream.

istream& seekg (streampos pos);
istream& seekg (streamoff offset, ios_base::seekdir way);


The first function moves the file pointer to the particular file position. whereas the second function goes to the certain position based on the offset set. The second parameter decides from where it should start.  The various value can be set to way is ios_base::beg, ios_base::cur, ios_base::end.
Caution:
seekg doesnot work when the end of file flag is set. At such cased you need to call the clear method to reset the flags.

clear() method of istream:

The clear reset the eofflag. Calling clear function doesnot make file pointer to move to the beginning of file.
File pointer postion:

streampos tellg();

The tellg returns the file pointer position. Return type is streampos but you type cast it to integer. tellg will work even if eofbit flag is set. tellg is the member function of istream class.
Here the ‘g’  in tellg represents the get and ‘p’ in tellp represents the put.

Number of characters read gcount:


The gcount returns the number of characters read at last time.
streamsize gcount() const;
The gcount is the member function of istream class.


Ignore the content up to some delimiter:

istream& ignore (streamsize n=1, int delimiter=EOF);
This will ignore the content of file up to the delimiter and the n represent the maximum no of limit to go. By default both the values are set to 1 and EOF.

 

Write a content to file:

This will write the content to file poited by ostream object.


ostream& write (const char* s, streamsize n);

Saturday, 4 June 2016

They are equivalent in syntax and semantics.

const char *ptr is same as char const *ptr;

Both form of declaration represents that they are pointer to const char type. I.e., value pointed by the address cannot be changed.

Even in declaration of normal variables const qualifier can be represented before or after the datatype.

For eg:
int const a; is same as const int a;

Friday, 3 June 2016

As you think, both user and schema in oracle database was same thing but there are few differences between them.
User is just a key to access the database resources, where as schema in oracle is collection of objects which contains the information about access to the resources.
Whenever you create user, schema is created by default with same name in oracle. 

Thursday, 2 June 2016

You may studied literal starting with 0 is octal. For Eg 07 will be treated as octal number, on other hand 7 will treated as integer by compiler.

So, now question is whether 0 will be treated as integer or octal by compiler?

It will treated as octal literal by the compiler  not as an integer since rule of thumb is every literal which starts with zero will be considered as octal by the compiler