This type of inheritance is also known as simple inheritance,as there is only one base class and only one derived class, the simple form  of inheritance.

class A is the base class and class B the derived class.Class B inherits all the public and protected members from class A. If the derivation is public, the protected or public member of Class A are respectively protected or public in B. If the derivation is private all the protected and public members of class A are private in B, whereas if the derivation is protected, all the protected and public members of class A are protected in B.

Illustrates single inheritance using public derivation

#include<iostream>
using namespace std;
class stud
{
private:
char name[20];
char roll[8];
protected:
int age;
public:
void getstudent()
{
cout<<"Name:";
cin>>name;
cout<<"Roll:";
cin>>roll;
cout<<"Age";
cin>>age;
}
void showstudent()
{
cout<<endl<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
}
};
class student:public stud
{
private:
int rank;
public:
void inputdata()
{
cout<<"Rank:";
cin>>rank;
}
void modifydata()
{
char bool;
cout<<endl<<"Do you want to modify?(y/N):";
cin>>bool;
if(bool=='Y')
{ 
cout<<"Age:";
cin>>age;
cout<<"Rank:";
cin>>rank;
}
}
void outputdata()
{
cout<<"Rank:"<<rank;
}
};
int main()
{
student s;
s.getstudent(); //accessing base class member using derived class object
s.inputdata();
s.showstudent(); // accessing base class member using derived class object
s.outputdata();
s.modifydata();
s.showstudent();
s.outputdata();
}

OUTPUT:

Name:John
Roll:10
Age:18
Rank:3
Name:John
Roll:10
Age:18
Rank:3
Do you want to modify?(Y/N):Y
Age:28
Rank:2
Name:John
Roll:10
Age:28
Posted by Unknown On 00:28 No comments READ FULL POST

Hybrid inheritance is a combination of two or more types of inheritance. In some applications user may need to combine the features available in multilevel inheritance, hierarchical inheritance and multiple inheritance.

One may, very well observe that this is a combination of multilevel and multiple inheritance


Illustrates the implementation of hybrid inheritance.


#include<iostream>
using namespace std;
class student
{
private:
char name[20];
char roll[8];
protected:
int age;
public:
void getstudent()
{
cout<<"Name:";
cin>>name;
cout<<"Roll:";
cin>>roll;
cout<<"Age";
cin>>age;
}
void showstudent()
{
cout<<endl<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
}
};
class academic:public student
{
private:
int rank;
public:
void inputdata()
{
getstudent();
cout<<"Rank:";
cin>>rank;
}
void outputdata()
{
showstudent();
cout<<"Rank:"<<rank;
}
};
class sports
{
private:
int score;
public:
void getscore()
{
cout<<"Score:";
cin>>score;
}
void putscore()
{
cout<<"score:"<<score;
}
};
class final:public academic,public sports
{
private:
char bloodgp[4];
public:
void fininputdata()
{
coiut<<"Blood Group:";
cin>>bloodgp;
}
void finoutputdata()
{
cout<<endl<<"Blood Group:"<<bloodgp<<endl;
}
};

int main()
{
final m;
cout<<"Enter student Deatails:"<<"\n;
m.inputdata();
m.getscore();
m.fininputdata();
cout<<"Student Details:"<<"\n";
m.outputdata();
m.putscore();
m.finoutputdata();
}

OUTPUT:
Enter student Details:
Name: Stroustrup
Roll:11
Age:18
Rank:10
Score:55
Blood Group:A+

Student Details:
Name: Stroustrup
Roll:11
Age:18
Rank:10
Score:55
Blood Group:A+


Other Recommended posts



Posted by Unknown On 00:07 1 comment READ FULL POST

Access Control

C++ has a third access specifiers or visibility mode called protected. This access specifiers protected plays an important role in inheritance. A member declared as protected is accessible by member function in its class or any class derived from its own class objects can never access private and protected member of a class directly. On the other hand, objects can reach these members through the public member functions in the class.
            There are three kinds of functions accessing a class: functions implementing the class (its friends and members), functions implementing a derived class (the friends of derived class and members) and other functions. A member of a class can be private, protected or public.
If a member is private, only member functions and friends of the class in which it is declared can use its name.
If it is protected only member functions and friends of the class in which it is declares and by member functions and friends of classes derived from this class can be use its name.
If a member is public, its name can be used by any function.

Access specifiers
Member functions and friends
Derived class member functions and friends
General users
Public
Yes
Yes
Yes
Protected
Yes
Yes
No
Private
Yes
No
No
Posted by Unknown On 00:14 No comments READ FULL POST

Inheritance:

In order to maintain and reuse the class objects easily, it is required to relate different classes. Two or more classes can be related under a given relationship. Such a relationship between classes can be referred to as “has a” relationship i.e. class A “has a” relationship with class B.



Inheritance is one of the most powerful features of OOP. Reusability is the basic objective behind inheritance. When a class needs some of the features of a previous class we can easily inherit those features without rewriting them, thus avoiding code repetition. C++ strongly supports the reusability concept in several ways. A class may be derived from one or more classes or from one or more levels, if the derived class is obtained using one base class it is called single inheritance. if the derived class is obtained using more than one base class it is called multiple inheritance. On the other hand if the same base class is used to derive more number of derived classes it is referred to as hierarchical inheritance. If a class is derived from another derived class, then the process is called multilevel inheritance. In case different types are combined, then it is called hybrid inheritance. The main advantages of inheritance are


Posted by Unknown On 22:37 No comments READ FULL POST
The constructor can also be used to allocate memory while creating objects. This will enable the system to allocate the right amount of memory for each object. Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. The memory is allocated with the help of the new operator.

Program: illustrate the working of dynamic constructors:

#include<iostream.h>
 class  Sample
     {
               char  *name;
               int  length; 
       public:
                Sample( )
                  {
                        length = 0;
                        name = new char[ length + 1];
                   }


               Sample ( char  *s )
                  {
                        length = strlen(s);
                        name = new char[ length + 1 ];
                        strcpy( name , s );
                   }
               void  display( )
                 {
                        cout<<name<<endl;
                  }
             };
          int  main( )
            {
                        char  *first = “ C++ “;
                        Sample  S1(first), S2(“ABC”), S3(“XYZ”);
                        S1.display( );
                        S2.display( );
                        S3.display( );
                        return 0;
             }
           
RESULT :                    
                    C++
                    ABC
                    XYZ

Other recommended posts of Constructor and Destructor

Posted by Unknown On 23:31 No comments READ FULL POST
Destructors  are used to distroy the objects that have been created. when an object goes out of scope, destructor automatically removes from the memory the workspace occupied by the object. Like constructor, destructor is also a member function, which has the same name as its class_name, but preceded by a tilde sign (~). futuer it has no return type.for example the destrucvtor for the class code is defined as follows.
 
    syntax:        ~code( )
                               {  body destructor }

The following are the characteristics of destructors

  • Destructors is used to destroy the work space of the object created
  • Destructors neither takes any arguments nor it returns any value
  • Destructors cannot be overloaded
  • It should have public access in the class declaration
  • It will be invoked implicitly by the compiler whenever an instances of the class to which it belongs goes out of existence
  • The primary  usage of a destructor is to clean up and release memory space for future use
  • Whenever 'new' operator is used in the constructors to allocate memory, we should use 'delete' to free that memory space.


Program:

#include <iostream>

using namespace std;

class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();   // This is the constructor declaration
      ~Line();  // This is the destructor: declaration

   private:
      double length;
};

// Member functions definitions including constructor
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
Line::~Line(void)
{
    cout << "Object is being deleted" << endl;
}

void Line::setLength( double len )
{
    length = len;
}

double Line::getLength( void )
{
    return length;
}
// Main function for the program
int main( )
{
   Line line;

   // set line length
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;

   return 0;

}

Result:

Object is being created
Length of line : 6

Object is being deleted

Other recommended posts of Constructor and Destructor

Posted by Unknown On 02:28 No comments READ FULL POST
Like other functions in C++, constructors can also be defined with default arguments. If an arguments are initialized at the time of constructor declaration/definition, then that type of constructors are called default argument constructor.
            The initialization of arguments at the time of constructor declaration must be in trailing order. And the missing arguments must be the trailing ones.

Program: Illustrate the default argument constructor
#include<iostream>
usage namespace std;

class  power
     {   
                        int  num;
                        int  pwr;
                        int  ans;
            public:
                        power(int   n = 3 , int  p = 2 );
                        void  show( )
                          {
                                    cout<< num <<”power”<< pwr <<”is:”<< ans ;
                          }
       };
    power :: power( int  n,int p)
       {
            num = n;
            pwr = p;
            ans = pow( n , p )   // math pow function
        }
    int  main( )
      {
              clrscr( );        
              power  p1, p2(5);
              p1.show( );
              p2.show( );
              return 0;
        }
    
  Result:
                        3 power 2 is 9

                        5 power 2 is 25


Other recommended posts of Constructor and Destructor

Posted by Unknown On 02:28 No comments READ FULL POST
Just like function overloading, constructors can be overloading. Since C++ permits function overloading and constructors are special functions in a class, they also overloaded. This implies that within a class we can have more than one constructor. These overloaded constructors are distinguished either by the number of arguments or by the data types of the arguments.

      example:   number();   //no arguments
                     number(int);   //one arguments
                     number(int, int);   //two arguments
                     number(int, float);   //differed by the datatype of arguments

program:

#include<iostream>
usage namespace std;

class student
{
     char *name;
     char rollno;
public:
     student() // constructor with no arguments
     {
          cout<<"Enter the Name and Rollno:";
          cin>>name;
          cin>>rollno;
     }
     student(char *s, char r)
     {
          strcpy(name s);  //overloaded constructor
          rollno=r;
     }
     student(student &p)  //overloaded Constructor
     {
          strcpy(name,p.name);
          rollno=p.rollno;
     }
     void didplay();
};
void student::display()
{
     cout<<"Name="<<name;
     cout<<"Rollno="<<rollno;
}
void main()
{   
     student s;
     s.display();
     student s1("ABC","A101");
     s1.display();
     student s2("xyz","A102");
     s2.display();
}

Result :

Enter the Name and Rollno: pqr A103
Name = pqr
Rollno =A103

Name = ABC
Rollno =A101

Name = xyz
Rollno =A102


Other recommended posts of Constructor and Destructor

Posted by Unknown On 02:27 No comments READ FULL POST
  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *