My Blog List

Thursday, January 2, 2020

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++.
On the basis of Inheritance concept the another type of inheritance is the Multiple Inheritance. 

  This type inheritance use to create a new class from multiple base class.In this technique two or more class use to create new class.
By this you can use the code or data from the different class in derived class.

Syntax:

    class base-class1
    {
        ............;
        ............;
    }
    class base-class2
    {
        ...........;
        ...........;
    }
    class derived-class:base-class1,base-class2
    {
        ...........;
        ...........;
    } 

 Suppose the student is the derived class.And the Teacher and subject is the two different base.Then the Student class is derived form other two base class is known as Multiple inheritance.

Here following code shows the multiple inheritance.


#include<iostream.h>
#include<conio.h>
class area
{
    public:
        int area;
        void cal_area(int l,int b);
};
class perimeter
{
    public:
        int peri;
        void cal_peri(int l,int b);
};
class rectangle:public area,public perimeter
{
    public:
        void display();
};
void area::cal_area(int length, int breath)
{
    area=length*breath;
}
void perimeter::cal_peri(int length,int breath)
{
    peri=2*length+2*breath;
}
void rectangle::display()
{
    cout<<"\nArea="<<area<<"\nPerimeter="<<peri;
}
void main()
{
    rectangle r;
    int l,b;
    clrscr();
    cout<<"\nEnter length & breath:";
    cin>>l>>b;
    r.cal_area(l,b);
    r.cal_peri(l,b);
    r.display();
    getch();
}

Output:

You May Also Like:

Single Inheritance in c++.






Single Inheritance in c++

In this tutorial we learn the concept of Inheritance in c++.
    Inheritance is the most powerful feature in the c++ language that allows us to the code re usability. That is better for every programmer when we are required the data again and again  then instead of writing that code again use inheritance. This feature save the time as well as money and also increases the reliability.

Que:What is mean by Inheritance?
Ans: The inheritance is the creating a new class form already exiting class. The old or existing class is known as Base class (super class or parent class) and the new class is known as Derived class(child class or sub class.

Types of Inheritance:


1)Single Inheritance

2)Multiple Inheritance
 
3)Multilevel Inheritance
4)Hierarchical Inheritance
 
5)Hybrid Inheritance


Here we learn first Single Inheritance:
 This type of Inheritance is done when the new class is creating only a one Base class. The Inheritance feature is allows us to use the data of the parent class.



Syntax:
 
 Class base-Class
{
                …………………
                ………………..
};
Class derived-class:base-class
                …………………….;
                ……………………;
}

The following code shows the single inheritance of student and teacher class in this code Teacher is the super class and the Student is the drived class. The Student class is uses the feature of the Teacher class.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Teacher
{
                char name[20];
                public:
                                char sub[20];
                                void getdata()
                                {
                                                cout<<"\nFor Teacher:";
                                                cout<<"\nEnter Teacher Name:";
                                                gets(name);
                                                cout<<"\nEnter subject:";
                                                gets(sub);
                                }
                                void display()
                                {
                                                cout<<"\n*****Teacher Information*****";
                                                cout<<"\nName:"<<name;
                                                cout<<"\nSubject:"<<sub;
                                }
};
class Student:public Teacher
{
                char sname[20],cls[20];
                int rno;
                public:
                                void getinfo()
                                {
                                                getdata();
                                                cout<<"\nFor Student:";
                                                cout<<"\nEnter Student Name:";
                                                gets(sname);
                                                cout<<"\nEnter class of Student:";
                                                gets(cls);
                                                cout<<"\nEnter roll no of Student:";
                                                cin>>rno;
                                }
                                void show()
                                {
                                                display();
                                                cout<<"\n*****Student Information*****";
                                                cout<<"\nName:"<<sname;
                                                cout<<"\nClass:"<<cls;
                                                cout<<"\nRoll No.:"<<rno;
                                }
};
void main()
{
                Student s1;
                clrscr();
                s1.getinfo();
                s1.show();
                getch();
}

The following code shows the OUTPUT:


 You may also like:

Multiple Inheritance in C++.

Saturday, December 21, 2019

Function overloding in cpp.

 Function overloading in cpp.
       In this tutorial we learn how to overload the function in c++.
Firstly,

 What is mean by function overloading?

Ans:Function overloading is the feature of cpp language.It means that the two or more function having same name.It can be one of the example of Polymorphism.The function name is same but the return type and parameter given to the function is different.

Syntax:
     function-return-type function name(parameter)
    declare another function as same name but different parameter
 
For Example:
      suppose we have a function for calculate addition of two number and three number.
     int add(int num1,num2);
and another is,
     int add(int num1,int num2 int num3);

Here following code shows a area function overloading concept to calculate the different area for different shape. 

#include<iostream.h>
#include<conio.h>
int area(int s);
int area(int l,int b);
double area(double bs,double hg);
double area(double r);
int main()
{
    clrscr();
    cout<<"\nArea of square="<<area(5)<<endl;
    cout<<"\nArea of Rectangle="<<area(10,5)<<endl;
    cout<<"\nArea of Triangle="<<area(4.0,10.0)<<endl;
    cout<<"\nArea of circle="<<area(6.0);
    getch();
    return(0);
}
int area(int s)
{
    return(s*s);
}
int area(int l,int b)
{
    return(l*b);
}
double area(double bs,double hg)
{
    return(bs*hg/2);
}
double area(double r)
{
    return(3.14*r*r);
}


OUTPUT:



Monday, December 16, 2019

Reverse number using cpp.

Reverse number using cpp.

        In this tutorial we learn how to print reverse number of any digit using cpp programming language.
A number contained any digit then by following technique we convert its into reverse order.
        suppose we take a 3 digit integer number and we want to print its into the reverse order then,the number is mod,add reminder and divide operation until number is 0.If number reach to 0 then terminate the execution.

example:

      number is 123
step 1:
       //here we taking a mod of number i.e 3
       mod=123%10
step 2:
       //here we add the reminder into mod that multiplied by 10
       reminder=reminder*10+mod
step 3:
       //finally number dividation.
       number=number/10

code:

#include<iostream.h>
#include<conio.h>
void main()
{
    int num,mod,reminder=0,i;
    clrscr();
    cout<<"\nEnter any 3 digit number:";
    cin>>num;
    while(num!=0)
    {
        mod=num%10;
        reminder=reminder*10+mod;
        num=num/10;
    }
    cout<<"\nReverse number:"<<reminder;
    getch();
}


Output:




You may also like:
Dynamic Memory Management in cpp.
Manipulators in cpp. 
This is the program constructor overloding in cpp 

Sunday, December 8, 2019

Dynamic Memory Management in CPP

Dynamic Memory Management in cpp.

In this tutorial we learn Dynamic Memory Management operator in cpp.This operator use to real time memory of the variable.Memory not allocated statically.This operator use only for Memory allocation and deallocation.

1)New :
      This operator is use to newly allocate memory of any data type.

syntax:
    pointer_variable=new data-type;

Here we use pointer-variable to allocate memory.new is the keyword and followed that data-type of data to store.The data type could be use any built-in data type.It also int user defined data type like structure and class.

Example:
        p=new student;

2)delete
        This operator use to deallocate the memory.It free up the memory that as already allocate.

syntax:
       delete variable-name;

This syntax use to delete the allocate memory.

example:
     delete p;

here p is the object of class when this statement is executed then the memory for this object free up.

code: 
    #include<iostream.h>
    #include<conio.h>
    class student
    {
         char name[20];
         int rno;
         float avg;
         public:
               void getdata();
               void putdata();
    };
    void student::getdata()
    {
         cout<<"\nEnter the name of student:";
         cin>>name;
         cout<<"\nEnter the roll number of student:";
         cin>>rno;
         cout<<"\nEnter average of student:";
         cin>>avg;
    }
    void student::putdata()
    {
        cout<<"\n"<<name<<"\t"<<rno<<"\t"<<avg;
    }
    int main()
    {
        int n,i;
        student *p,*q;
        clrscr();
        cout<<"\nEnter number of student:";
        cin>>n;
        p=new student[n];
        q=p;
        for(i=1;i<=n;i++)
        {
            cout<<"\nEnter "<<i<<" Student information";
            p->getdata();
            p++;
        }
        p=q;
        cout<<"\nStudent information:";
        cout<<"\nName\tRoll no\tAverage";
        for(i=1;i<=n;i++)
        {
            p->putdata();
            p++;
        }
        delete p;
        getch();
        return(0);
  } 


OUTPUT:




 


 

 
 

Manipulators in cpp

Manipulators in cpp.

In this tutorial we learn Manipulators in cpp.These are the function that helps to modify the input/output stream.It does not change any value or data of the variable but it only modify the data as programmer wants.

Types of Manipulators:

1)Manipulator without argument:
  • endl:This manipulator defined in ostream.It used to instead of using \n.It is use to enter the new line.
  • ws:This manipulator use to ignore the all whitespaces in the line.It is defined in istream.
  • ends:It includes the null character into the ostream.It is belonging in ostream.
  • flush:It is also in ostream.It flushes output stream in the line.
 2)Manipulators with Arguments: 

  • setw (val): It is used to sets the field width in output stream.
  • setfill (x): It is used to fill the character ‘x’ on output stream.
  • setprecision (val): It sets val as the new value for the precision of floating-point values. 
code:

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
    int m1,m2;
    double f=3.14159;
    clrscr();
    cout<<"\nEnter two number:";
    cin>>m1>>m2;
    cout<<"\nDisplay the number:";
    cout<<"\nN1:="<<m1;
    cout<<"\nN2:="<<m2;
    cout<<"\nN1="<<setw(8)<<m1<<endl;
    cout<<"\nN2="<<setw(5)<<m2<<endl;
    cout<<setfill('x')<<setw(10);
    cout<<77<<endl;
    cout<<setprecision(4)<<f;
    getch();
    return(0);


OUTPUT:

 

Friday, December 6, 2019

Binary search using cpp.

Binary Search using cpp.

In this tutorial we learn how to search element from given array.This technique is best technique for searching element in large amount of data is given.This techniques is based on the Divide and Conquer.But main condition for this technique is that the data given input is sorted.This approach divide the array in two part and then search the element where the small by middle element or greater.


Code:


#include<iostream.h>
#include<conio.h>
void main()
{
    int n,i,arr[50],search,first,last,middle;
    clrscr();
    cout<<"\nEnter total number of element:";
    cin>>n;
    cout<<"\nEnter the element:";
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"\nEnter searchable element:";
    cin>>search;
    first=0;
    last=n-1;
    middle=(first+last)/2;
    while(first<=last)
    {
        if(arr[middle]<search)
        {
            first=middle+1;
        }
        else if(arr[middle]==search)
        {
            cout<<search<<" found at position:"<<middle+1<<"\n";
            break;
        }
        else
        {
            last=middle-1;
        }
        middle=(first+last)/2;
    }
    if(first>last)
    {
        cout<<"\nElement not found:";
    }
    getch();
}





OUTPUT:



for video demonstration follow:

https://youtu.be/qFpjuqX1gAQ

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++. On the basis of Inheritance concept the another type of inheritance is the Multiple I...