Posts

Exceptional Handling

            Exceptional Handling Exception Handling Run time error is called as an exception. C++ has a mechanism to handle exceptions. The exception handling mechanism of C++ uses the following keywords: try, throw and catch. Examples of exception Divide by zero Accessing array out of bound Running out of memory or disk space Types of Exceptions Synchronous exception Asynchronous exception 1)The exceptions that are caused by events under the control of the program are called as synchronous exceptions. Example: Divide by zero, Accessing array out of bound, Running out of memory or disk space 2) The exceptions that are caused by events beyond the control of the program are called as synchronous exceptions. Example: Keyboard Interrupts. The exception handling mechanism in C++ is designed to handle only synchronous exceptions. Example of Exception Handling void main() { int a,b; cin>>a>>b; ...

Inheritance

                      Inheritance Inheritance The ability of one class, called child class, to acquire properties from another class, called as parent class is called as inheritance. The parent class is also called as base class and child class is also called as derived class. Inheritance is used to achieve reusability of code, which is an important concept of object oriented programming. Example of single inheritance class a //base class or parent class { public: void fun() { cout<<“fun”; } }; class b:public a //child class or derived class {;} void main() { b ob;   ob.fun(); /*fun of class a called with derived class b’s  object, as fun is inherited in b class*/ } Output: fun Protected class member When a class member is declared to be protected, then the protected class member can be accessed within the class and in the immediately derived class a...

Inline and Friend Operators

Inline and Friend Operators   Friend Function A function which is not a member function of a class, but can still access the private data member of a class is called as friend function. To declare a function as friend, we precede the function name with the keyword friend. Friend function may pass an object as its argument Example of Friend Function class abc { private: int x; public: friend void fun(); //fun function is now friend function of class abc }; void fun() /*friend function defined outside class without scope resolution operator (::) -        fun is not a member function of class abc*/ { abc ob; ob.x=10; //friend function accessing private data member x of class abc cout<<ob.x; } void main() { fun(); //friend function called (without object and dot operator) } Output: 10 Inline Function When a function gets called, it takes a lot of extra time in executing instruction...

Function overloading and Operator overloading

Function overloading and Operator overloading Function Overloading Doing something in excess is called as overloading. When a function is defined more than one time such that either the number of arguments passed in the functions differ or the data type of arguments passed in the functions differ or both, then this is called function overloading. Function overloading is used to achieve compile time polymorphism. Example of Function Overloading class abc { private: int x,y,s; void fun(int x1,int y1) //fun defined {    x = x1; y = y1;    cout<<x*y<<endl; } void fun(int s1) //fun redefined - fun is overloaded { s = s1;  cout<<s*s; }  }; void main() { abc ob1;  ob1.fun(2,3);  ob1.fun(4); } Output: 6 16 Operator Overloading Doing something in excess is called as overloading. When an operator is used to operate on objects, in addition to constants and varia...