Showing posts with label CPP Language. Show all posts
Showing posts with label CPP Language. Show all posts

OOPS - [C++ Random Access File]

♠ Posted by Unknown in

Random Access File Handling


In C++ I/O system, you perform random access in file by using the seekg() and seekp() functions, which are members of the input and output stream classes, respectively. Their most common forms are shown here:

OOPS - [C++ File Handling]

♠ Posted by Unknown in

File Operation Mode


The ifstream and ofstream constructors and the function open() to create new files as well as to open the existing files. In both these methods, we used only one argument that was the filename.

OOPS - [C++ Virtual Function]

♠ Posted by Unknown in

Virtual Function

When we use the same function name in both the base and derived classes, the function in base class id declared as virtual using virtual preceding its normal declaration.

OOPS - [C++ Runtime Polymorphism]

♠ Posted by Unknown in

Runtime Polymorphism


Polymorphism means ‘one name, multiple forms’. The concept of polymorphism is implemented using the overloaded functions and operators. The overloaded member functions are ‘selected’ for invoking by matching arguments, both type and number. This information is known to the compiler at the compile time and, therefore compiler is able to select the appropriate function for a particular call at the compile time itself. This is called early binding or static binding or static linking. Also known as compile time polymorphism, early binding simply means that an object is bound to its function call at compile time.

If the appropriate member function could be selected while the program is running. This is known as run time polymorphism. C++ supports a mechanism known as virtual function to achieve runtime polymorphism.

At runtime, when it is known what class objects are under consideration, the appropriate version of the function is invoked. Since the functions is linked with a particular class much later after the compilation, this process is termed as late binding. It is also known as dynamic binding because the selection of the appropriate function is done automatically at runtime. Refer following figure.

Image: Run Time Polymorphism


Dynamic binding is one of the powerful features of C++. This requires the use of pointers to objects. The object pointer and virtual functions are used to implement dynamic binding.

Example:

#include<iostream.h>
#include<conio.h>
class Account
{
 protected:
            int acc_no;
 public:
            Account(int ac)
            { acc_no = ac; }
            virtual void display(){ }    //Empty Virtual Function
};

class Saving: public Account
{
            int sav_amount;
 public:
            Saving(int ac, int s_am):Account(ac)
            { sav_amount = s_am; }
            void display();
};
void Saving::display()
{
 cout<<"The Saving Account No : " <<acc_no<<endl;
 cout<<"The Saving Account Amount : "<<sav_amount<<endl;
}

class Current: public Account
{
            int cur_amount;
 public:
            Current(int ac, int c_am): Account(ac)
            { cur_amount = c_am; }
            void display();
};

void Current::display()
{
 cout<<"The Current Account No : "<<acc_no<<endl;
 cout<<"The Current Account Amount : "<<cur_amount<<endl;
}

void main()
{
 Saving sav(01, 5000);
 Current cur(02, 10000);
 clrscr();
 Account *acc; //Base Class Pointer

 acc = &sav;
 acc->display();//display() From Saving Class

 acc = &cur;
 acc->display();//display() From Current Class.
 getch();
}

OOPS - [C++ Virtual Base Class]

♠ Posted by Unknown in

Virtual Base Class


When all the three kinds of inheritance, namely, multilevel, multiple, and hierarchical inheritance, are involved. This is illustrated in below figure. The ‘child’ has two direct base classes ‘parent1’ and ‘parent2’ which themselves have a common base class ‘grandparent’. The ‘child’ inherits the traits of ‘grandparent’ via two separate paths. It can also inherit directly as show by the broken line. The ‘grandparent’ is sometimes referred to as indirect base class.
                      
Image: Virtual Base Class
                                                 

Inheritance by the ‘child’ as shown in figure might pose some problems. All the public and protected members of ‘grandparent’ are inherited into ‘child’ twice, first via ‘parent1’ and again via ‘aprent2’. This means, ‘child’ would have duplicate sets of the members inherited from ‘grandparent’. This introduces ambiguity and should be avoided.
The duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual class.
When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance paths exists between the virtual base class and a derived class.

OOPS - [C++ Multiple Inheritance]

♠ Posted by Unknown in

Multiple Inheritance


A class can inherit the attributes of two or more classes known as multiple inheritance. Multiple inheritance allows us to combine the feature of several existing classes as a starting point for defining new classes. It is like a child inheriting the physical features of one parent and the intelligence of another.
Syntax:

            Class D: visibility B-1, visibility B-2 {……………};

Image: Multiple Inheritance

Example:

#include<iostream.h>
#include<conio.h>
class A
{
 protected:
            int a;
 public:
            void get_a()
            {
             cout<<"Enter The Value For A : ";
             cin>>a;
            }
            void put_a()
            {
             cout<<"The Value Of A : "<<a<<endl;
            }
};

class B
{
 protected:
            int b;
 public:
            void get_b()
            {
             cout<<"Enter The Value For B : ";
             cin>>b;
            }
            void put_b()
            {
             cout<<"The Value Of B : "<<b<<endl;
            }
};

class C : public A, public B
{
            int c;
 public:
            void put_c()
            {
             c = a + b;
             cout<<"The Answer Is : "<<c<<endl;
            }
};
void main()
{
 C objc;
 clrscr();
 objc.get_a();
 objc.get_b();
 objc.put_c();
 getch();
}

OOPS - [C++ Multilevel Inheritance]

♠ Posted by Unknown in

Multilevel Inheritance


When a class is derived form another derived class is called multilevel inheritance. In the following figure the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C/ the class B is known as intermediate base class since it provides a link for the inheritance between A and C. the chain ABC is known as inheritance path.
           
            class A{…………};                         //Base Class
            class B: public A{…………};       // B derived from A
            class C: public B{…………};       // C derived from B


Image: Multi Level Inheritance
Example:

#include<iostream.h>
#include<conio.h>
class Student //Base Class
{
 protected:
            int rno;
 public:
            void get_number(int);
            void put_number(void);
};

void Student::get_number(int a)
{ rno = a; }

void Student::put_number(void)
{
 cout<<"Roll Number :"<<rno<<endl;
}

class Test: public Student //Intermediate Base Class
{
 protected:
            int sub1;
            int sub2;
 public:
            void get_marks(int , int );
            void put_marks(void);
};
void Test::get_marks(int x, int y)
{
 sub1 = x;
 sub2 = y;
}
void Test::put_marks(void)
{
 cout<<"Subject-1 : "<<sub1<<endl;
 cout<<"Subject-2 :"<<sub2<<endl;
}

class Result: public Test //Derived Class
{
            int total;
 public:
            void display(void);
};
void Result::display(void)
{
 total = sub1 + sub2;
 put_number();
 put_marks();
 cout<<"Total : "<<total<<endl;
}

void main()
{
 clrscr();
 Result stud;
 stud.get_number(111);
 stud.get_marks(56,78);
 stud.display();
 getch();
}

OOPS - [C++ Single Inheritance]

♠ Posted by Unknown in

Single Inheritance

Reusability is yet another important feature of OOP. C++ strongly supports the concept of reusability. The C++ classes can be reused in several ways. The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as the base class and the new one is called the derived class or subclass.

The derived class inherits some or all of the traits from the base class. A class can also inherit properties from more than one class or from more than one level. A derived class with only one class is called single inheritance. Following figure shows form of single inheritance. The direction of arrow indicate the direction of inheritance.

Image: Single Inheritance

Syntax :

class derived-class-name : visibility-mode base-class-name
{
            members of derived class;
};

In the above syntax the visibility-mode is optional and, if present, may be either private or public. The default visibility-mode is private.

When a base class is privately inherited by a derived class, ‘public members’ of the base class become ‘private members’ of derived class and therefore the public members of the base class can only be accessed by the member functions of the derived class. They are inaccessible to the objects of the derived class.

Example: 1

#include<iostream.h>
#include<conio.h>
//Base Class
class A
{
                        int a;
            public:
                        void getvalue();
                        void putvalue();
};
void A::getvalue()
{
            cout<<"Enter Value for a : ";
            cin>>a;
}
void A::putvalue()
{
            cout<<"The Value of a is : "<<a<<endl;
}

//Derived Class Private Derivation
class B:private A
{
                        int b;
            public:
                        void getdata();
                        void putdata();
};
void B::getdata()
{
 getvalue();
 cout<<"Enter Value for b : ";
 cin>>b;
}
void B::putdata()
{
 putvalue();
 cout<<"The Value of b is : "<<b<<endl;
}

void main()
{
 B objB;
 clrscr();
 objB.getdata();
 objB.putdata();
 getch();
}

On the other hand, when the base class is publicly inherited, ‘public members’ of the base class become ‘public members’ of the derived class and therefore they are accessible to the objects of the derived class. in both the cases, the private members are not inherited and therefore the private members of a base class will never become the members of its derived class.

Example : 2

#include<iostream.h>
#include<conio.h>
//Base Class
class A
{
                        int a;
            public:
                        void getvalue();
                        void putvalue();
};
void A::getvalue()
{
            cout<<"Enter Value for a : ";
            cin>>a;
}

void A::putvalue()
{
            cout<<"The Value of a is : "<<a<<endl;
}

//Derived Class Public Derivation
class B:public A
{
                        int b;
            public:
                        void getdata();
                        void putdata();
};
void B::getdata()
{
 cout<<"Enter Value for b : ";
 cin>>b;
}
void B::putdata()
{
 cout<<"The Value of b is : "<<b<<endl;
}

void main()
{
 B objB;
 clrscr();
 objB.getvalue();
 objB.getdata();
 objB.putvalue();
 objB.putdata();
 getch();
}

OOPS - [C++ Operator Overloading]

♠ Posted by Unknown in

Operator Overloading

To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function, called “operator overloading”, which describes the task. The general form of an operator function is :

                        return_type classname :: operator op(arglist)
                        {
                          function_body  //task defined
                        }

where return type is a type of value is returned by the specified operation and op is the operator being overloaded. The op is preceded by the keyword “operator”. “operator op” is the function name.

Operator functions must be either member functions (non-static) or friend functions. A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, while a member function has no arguments for unary operators and only one argument for binary operators. This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. This is not the case with friend functions. Arguments may be passed either by value or by reference.

The process of overloading involves the following steps:

1.     Creates a class that defines the data type that is to be used in the overloading operations.
2.     Declares the operator function operator op() in the public part of the class. It may be either a member function or a friend function.
3.     Define the operator function to implement the required operations.

Example of Operator Overloading :

1.     Unary Member Operator Function.

#include<iostream.h>
#include<conio.h>
class Example
{
            int cnt;
 public:
            Example(int x)
            { cnt = x; }
            void operator -()//Unary Member Operator Function
            {
             cnt = -cnt;
            }
            void display()
            {
             cout<<cnt<<endl;
            }
};
void main()
{
 Example S1(-10), S2(45);
 clrscr();
 -S1;
 S1.display();
 -S2;
 S2.display();
 getch();
}

2.     Unary Friend Operator Function.

#include<iostream.h>
#include<conio.h>
class Example
{
            int cnt;
 public:
            Example(int x)
            { cnt = x; }
            friend void operator -(Example &ob)//Unary Friend Operator Function
            void display()
            {
             cout<<cnt<<endl;
            }
};

void operator -(Example &ob)//Unary Friend Operator Function
{
 ob.cnt = -ob.cnt;
}
void main()
{
 Example S1(-10), S2(45);
 clrscr();
 -S1;
 S1.display();
 -S2;
 S2.display();
 getch();
}

3.     Binary Member Operator Function.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class Example
{
            char *p;
            int length;
 public:
            Example()
            {
                        length = 0;
                        p = new char[length + 1];
            }
            Example(char *str)
            {
                        length = strlen(str);
                        p = new char[length + 1];
                        strcpy(p, str);
            }
            void operator +(Example &);
            void display();
};
void Example::operator +(Example &ob)
{
 length = strlen(p);
 ob.length = strlen(ob.p);
 Example tmp;
 tmp.p = new char[length + 1];
 strcpy(tmp.p, p);
 delete p;
 p = new char[length + ob.length + 1];
 strcpy(p, tmp.p);
 strcat(p, ob.p);
 delete tmp.p;
}
void Example::display()
{
            cout<<"The String is :"<<p<<endl;
}

void main()
{
 Example S1("Vakratund"), S2("Computer");
 clrscr();
 S1 + S2;
 S1.display();
 getch();
}

4.     Define a class string. Use overloaded + operator to concatenate two strings.
                                                                                          
  #include<iostream.h>
#include<conio.h>
#include<string.h>
class Example
{
            char *p;
            int length;
 public:
            Example()
            {
                        length = 0;
                        p = new char[length + 1];
            }
            Example(char *str)
            {
                        length = strlen(str);
                        p = new char[length + 1];
                        strcpy(p, str);
            }
            friend Example operator +(Example &, Example &);
            void display();
};

Example operator +(Example &ob1, Example &ob2)
{
 ob1.length = strlen(ob1.p);
 ob2.length = strlen(ob2.p);
 Example tmp;
 tmp.p = new char[ob1.length + 1];
 strcpy(tmp.p, ob1.p);
 delete ob1.p;
 ob1.p = new char[ob1.length + ob2.length + 1];
 strcpy(ob1.p, tmp.p);
 strcat(ob1.p, ob2.p);
 delete tmp.p;
 return ob1;
}

void Example::display()
{
            cout<<"The String is :"<<p<<endl;
}

void main()
{
 Example S1("Vakratund"), S2("Computer"),S3;
 clrscr();
 S3 = S1 + S2;
 S3.display();
 getch();
}