C++ program to find greatest between three numbers using single inheritance

Greatest between three numbers using single inheritance.

C++ program to find greatest between three numbers using single inheritance.Here class scan is base class and class great is a derived class from class scan.

#include<iostream.h>
#include<conio.h>
class scan
{
 protected:
 int a,b,c;
 public:
 void get()
 {
  cout<<"Enter value of a:- ";
  cin>>a;
  cout<<"Enter value of b:- ";
  cin>>b;
  cout<<"Enter value of c:- ";
  cin>>c;
 }
};
class great:public scan
{
 public:
 void great_val()
 {
 if(a>b)
 {
  if(a>c)
   {
   cout<<"a is Greater";
   }
   else
   {
   cout<<"c is Greater";
   }
 }
 else
 {
 if(b>c)
   {
    cout<<"b is greater";
   }
   else
   {
    cout<<"c is Greater";
   }
 }
 }
};
void main()
{
 clrscr();
 great g1;
 g1.get();
 g1.great_val();
 getch();
}

OUTPUT

Greatest between three numbers using single inheritance


The above program can also be done by passing value using object.

#include<iostream.h>
#include<conio.h>
class scan
{
 protected:
 int a,b,c;
 public:
 void get(int x,int y,int z)
 {
    a=x;
    b=y;
    c=z;
 }
};
class great:public scan
{
 public:
 void great_val()
 {
 if(a>b)
 {
  if(a>c)
   {
   cout<<"a is Greater";
   }
   else
   {
   cout<<"c is Greater";
   }
 }
 else
 {
 if(b>c)
   {
    cout<<"b is greater";
   }
   else
   {
    cout<<"c is Greater";
   }
 }
 }
};
void main()
{
 clrscr();
 int a,b,c;
 great g1;
 cout<<"Enter value of a:- ";
 cin>>a;
 cout<<"Enter value of b:- ";
 cin>>b;
 cout<<"Enter value of c:- ";
 cin>>c;
 g1.get(a,b,c);
 g1.great_val();
 getch();
}

OUTPUT

Greatest between three numbers using single inheritance


Comment below if you are facing any problem.


Also read

C++ program swap two numbers using single inheritance


Summary
C++ program to find greatest between three numbers using single inheritance
Article Name
C++ program to find greatest between three numbers using single inheritance
Description
Program to find greatest between three numbers using single inheritance.A simple if and else method is used to find greatest between three number.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *