on Leave a Comment

C++ Program to Find Largest Element in Array

This article contains C++ Program to Find Largest Element in Array.

To understand this program, you need to know


C++ Program to Find Largest Element in Array

#include<iostream>
using namespace std;
int main()
{
        int l,arr[50],n,i;
        cout<<"Enter the size of array (under 50) : ";
        cin>>n;
        for(i=0; i<n; i++)
        {
            cout<<"\nEnter array element "<<i+1<<" : ";
                cin>>arr[i];
        }
        l=arr[0];
        for(i=0; i<n; i++)
        {
                if(l<arr[i])
                {
                        l=arr[i];
                }
        }
        cout<<"\nLargest Number : "<<l;
        return 0;
}

OUTPUT:

Enter the size of array (under 50) : 5

Enter array element 1 : 9

Enter array element 2 : 4

Enter array element 3 : 3

Enter array element 4 : 5

Enter array element 5 : 8

Largest Number : 9


C++ Program to Find Largest Element in Array Using Functions

#include<iostream>
using namespace std;
void largest(int [], int);
int main()
{
        int arr[50],n,i;
        cout<<"Enter the size of array (under 50) : ";
        cin>>n;
        for(i=0; i<n; i++)
        {
            cout<<"\nEnter array element "<<i+1<<" : ";
                cin>>arr[i];
        }
        largest(arr, n);
        return 0;
}
void largest(int a[], int n)
{
    int l=a[0],i;
        for(i=0; i<n; i++)
        {
                if(l<a[i])
                {
                        l=a[i];
                }
        }
        cout<<"\nLargest Number : "<<l;
}

OUTPUT:

Enter the size of array (under 50) : 5

Enter array element 1 : 58

Enter array element 2 : 49

Enter array element 3 : 65

Enter array element 4 : 29

Enter array element 5 : 38

Largest Number : 65


on Leave a Comment

Types of Inheritance in C++

This article contains Types of Inheritance in C++.

To understand types of inheritance in c++, you must know about Inheritance in C++.

Types of Inheritance in C++

(1) Single Inheritance
(2) Multiple Inheritance
(3) Multilevel Inheritance 
(4) Hierarchical Inheritance
(5) Hybrid Inheritance

Single Inheritance

In single inheritance, a derived class inherits properties of only one base class or super class. This is most simple type of inheritance.
Single inheritance in c++
Example:

class A
{
        public:
            int a;
};
class B: public A
{
        public:
            int b;
};

Multiple Inheritance

In multiple inheritance, a derived class inherits from two or more than two base classes.
multiple inheritance in c++
Example:

class A
{
       public:
           int a;
};
class B
{
       public:
           int b;
};
class C: public A, public B
{
       public:
           int c;
};

Multilevel Inheritance 

In multilevel inheritance, a derived class inherits from one base class and some other derived inherits from that derived class and so on.
multilevel inheritance in c++
Example:

class A
{
       public:
           int a;
};
class B: public A
{
       public:
           int b;
};
class C: public B
{
       public:
           int c;
};

Hierarchical Inheritance

In hierarchical inheritance, multiple derived class inherits from single base class.
Hierarchical Inheritance in C++
Example:

class A
{
       public:
           int a;
};
class B: public A
{
       public:
           int b;
};
class C: public A
       public:
           int c;
};

Hybrid Inheritance 

Hybrid inheritance is a combination of two or more inheritance.
Hybrid Inheritance in C++
Example:

class A
{
       public:
           int a;
};
class B: public A
{
       public:
           int b;
};
class C: public A
       public:
           int c;
};
class D: public B, public C
{
      public:
          int d;
};

on Leave a Comment

Inheritance in C++

In this article, we will read about Inheritance in C++.

Inheritance is essential feature of Object Oriented Programming. Important aspect of Inheritance is code reusability. It is sometime necessary to reuse a same code rather than writing same code over and over. It also save time, money and increase reliability. In inheritance, a new class inherits some properties and characteristics of already existing class. 

The new class which is inheriting properties of existing class is called as derived class or sub class or child class.

The class whose properties are inherited by derived class is called base class or super class or parent class.

The derived class can inherits some or all the properties of base class based on the visibility mode.

Advantages of Inheritance in C++

(1)  Code reusability
(2)  Save time for writing same code over and over.
(3)  Method overriding
(4)  Usage of virtual function
(5)  Extensibility 

Disadvantages of Inheritance in C++

(1) Derived class depends on base class.
(2) Inherited functions work slower than normal functions.

Syntax of Inheritance in C++

class derived_class_name : visibility_mode base_class_name

Example:

class A
{
        public:
            int a;
};
class B: public A
{
        public:
            int b;
};

Inheritance Visibility Modes in C++

Visibility modes defines which properties of base class is inherited by derived class.

Note: All members of base class can be inherited except private members.

Public Mode

In public mode, public members of base class becomes public members of derived class and protected members of base class becomes protected members of derived class.

Syntax:

class derived_class_name : public base_class_name

Example:

class A
{
        public:
            int a;
};
class B: public A
{
        public:
            int b;
};

Private Mode

In private mode, protected and public members of base class becomes private members of derived class. So these members cannot be accessed outside the derived class.

Syntax:

class derived_class_name : private base_class_name

Example:

class A
{
        public:
            int a;
};
class B: private A
{
        public:
            int b;
};

Protected Mode

In protected mode, public and protected members of base class becomes protected members of derived class.

Syntax:

class derived_class_name : protected base_class_name

Example:

class A
{
        public:
            int a;
};
class B: protected A
{
        public:
            int b;
};

on Leave a Comment

C++ Program to Find ASCII Value of a Character

This article contains C++ Program to Find ASCII Value of a Character.

ASCII stands for American Standard Code for Information Interchange. ASCII code is 7 bit code. ASCII code is used to represent text in computers, and other devices.

C++ Program to Find ASCII Value of a Character

#include <iostream>
using namespace std;
int main()
{
    char ch;
    for(int i=1;i<255;i++)
    {
        ch=i;
        cout<<i<<" -> "<<ch<<endl;
    }
    return 0;
}

OUTPUT:


C++ Program to Find ASCII Value of a Character



C++ Program to Find ASCII Value of Particular Character

#include <iostream>
using namespace std;
int main()
{
       char c;
       cout << "Enter a character : ";
       cin >> c;
       int x=int(c);
       cout << "ASCII Value of " << c << " is " <<x;
       return 0;
}

OUTPUT:

Enter a character : b
ASCII Value of b is 98


on Leave a Comment

C++ Program for Fibonacci Series using Recursion

This article contains C++ Program for Fibonacci Series using Recursion.

Recursion: When a function calls itself, it is called recursion.

Fibonacci series example, 0       1       1       2       3       5

C++ Program for Fibonacci Series using Recursion

#include <iostream>
using namespace std;
int fibonacci(int n);
int main()
{
    int n,i=0;
    cout<<"Enter The Terms For Fibonacci Series : ";
    cin>>n;
    cout<<endl<<"Fibonacci Series : "<<endl<<endl;
    while(i<n)
    {
        cout<<fibonacci(i)<<"\t";
        i++;
    }
    return 0;
}
int fibonacci(int n)
{
    if((n==0) || (n==1))
    {
        return n;
    }
    else
    {
        return(fibonacci(n-1)+fibonacci(n-2));
    }
}

OUTPUT:

Enter The Terms For Fibonacci Series : 10

Fibonacci Series :

0       1       1       2       3       5       8       13      21      34