Exercise on C++ Inheritance | C++ Tutorials for Beginners #42

preview_player
Показать описание

Best Hindi Videos For Learning Programming:

►C Language Complete Course In Hindi -

►JavaScript Complete Course In Hindi -

►Django Complete Course In Hindi -

Follow Me On Social Media
Рекомендации по теме
Комментарии
Автор

1. Multiple inheritance
2. Public visibility mode
3. Reusability is implemented as we did not write addition, subtraction, trigonometric calculation in hybrid inheritance but inherited from two different classes.
4. Code:

#include <iostream>
using namespace std;
#include <cmath>

class simplecalc{
protected:
float a, b;
public:
simplecalc(float x, float y){
a=x;
b=y;

}


void show(){
cout<<"The sum of "<<a<<" and "<<b<<" is "<<a+b<<endl;
cout<<"The difference of "<<a<<" and "<<b<<" is "<<a-b<<endl;
cout<<"The product of "<<a<<" and "<<b<<" is "<<a*b<<endl;
cout<<"The div of "<<a<<" and "<<b<<" is "<<a/b<<endl;
}

};

class scientificcalc{
protected:
float a, b;
public:
scientificcalc(float x, float y){
a=x;
b=y;
}

void display(){
cout<<"The sine of "<<a<<" + "<<b<<" is "<<sin(a+b)<<endl;
cout<<"The cosine of "<<a<<" + "<<b<<" is "<<cos(a+b)<<endl;
cout<<"The tangent of "<<a<<" + "<<b<<" is "<<tan(a+b)<<endl;
cout<<a<<" raised to "<<b<<" is "<<pow(a, b)<<endl;
}

};

class hybridcalc: public simplecalc, public scientificcalc{
protected:
float a, b;
public:
hybridcalc(float x, float y):simplecalc(x, y), scientificcalc(x, y){
a=x;
b=y;
}

void calcfunc(){
show();
display();
}
};

int main(){
float x, y;
cout<<"Enter 1st number: ";
cin>>x;
cout<<"Enter 2nd number: ";
cin>>y;
hybridcalc aditya(x, y);
aditya.calcfunc();
return 0;
}

adityagoyal
Автор

1. Multiple inheritance.
2. Public display mode.
3. Reusability is implemented because we reuse the typed code in derived class.
Code:
#include <iostream>
#include<cmath>
using namespace std;
class simple
{
protected:
int a, b;

public:
void set1Data(int num, int num1)
{
a = num;
b = num1;
}
void display(void)
{
cout << "Addition is: " << a + b << endl;
cout << "Substraction is: " << a - b << endl;
cout << "Multiplication is: " << a * b << endl;
cout << "Division is: " << a / b << endl;
}
};
class scientific
{
protected:
int x, y;

public:
void set2Data(int num, int num1)
{
x = num;
y = num1;
}
void process(void)
{
cout << "The square root of x is " << sqrt(x) << endl;
cout << "The square root of y is " << sqrt(y) << endl;
cout << "The cube root of x is " << cbrt(x) << endl;
cout << "The cube root of y is " << cbrt(y) << endl;
}
};

class derived:public simple, public scientific{
public:
void set(int a, int b){
simple::set1Data(a, b);
simple::display();
scientific::set2Data(a, b);
scientific::process();

}
};

int main()
{
int p, q;
cout<<"Enter two number: "<<endl;
cin>>p>>q;

derived der;
der.set(p, q);
return 0;
}

priyanshimulgaonkar
Автор

This channel gonna be one of the best educational channel in india one day.awesome harry bhai ❤

prashant
Автор

/*
Exercise Question
Create 2 classes:
1. SimpleCalculator - Takes input of 2 numbers using a utility function and performs +, -, *, / and displays the results using another function.
2. ScientificCalculator - Takes input of 2 numbers using a utility function and performs any four scientific operations of your choice and displays the results using another function.

Create another class HybridCalculator and inherit it using these 2 classes:
Q1. What type of Inheritance are you using?
Q2. Which mode of Inheritance are you using?
Q3. Create an object of HybridCalculator and display results of the simple and scientific calculator.
Q4. How is code reusability implemented?
*/


#include<iostream>
#include<cmath>
using namespace std;

class Simple{
protected:
int num1, num2;
int sum, diff, prod;
float div;
public:
void setInfo_Sim(int a, int b){
num1 = a;
num2 = b;
}
void process_Sim(void){
sum = num1 + num2;
diff = num1 - num2;
prod = num1 * num2;
div = (float)num1 / num2;
}
void display_Sim(void){
cout << num1 << " + " << num2 << " = " << sum << endl;
cout << num1 << " - " << num2 << " = " << diff << endl;
cout << num1 << " * " << num2 << " = " << prod << endl;
cout << num1 << " / " << num2 << " = " << div << endl;
}
};

class Scientific{
protected:
int num3, num4;
float sqr3, sqr4;
float cub3, cub4;
float sqrt3, sqrt4;
float cbrt3, cbrt4;
public:
void setInfo_Sci(int a, int b){
num3 = a;
num4 = b;
}
void process_Sci(void){
sqr3 = num3 * num3;
sqr4 = num4 * num4;
cub3 = num3 * num3 * num3;
cub4 = num4 * num4 * num4;
sqrt3 = sqrt(num3);
sqrt4 = sqrt(num4);
cbrt3 = cbrt(num3);
cbrt4 = cbrt(num4);
}
void display_Sci(void){
cout << "Square of " << num3 << ": " << sqr3 << endl;
cout << "Square of " << num4 << ": " << sqr4 << endl;
cout << "Cube of " << num3 << ": " << cub3 << endl;
cout << "Cube of " << num4 << ": " << cub4 << endl;
cout << "Square Root of " << num3 << ": " << sqrt3 << endl;
cout << "Square Root of " << num4 << ": " << sqrt4 << endl;
cout << "Cube Root of " << num3 << ": " << cbrt3 << endl;
cout << "Cube Root of " << num4 << ": " << cbrt4 << endl;
}
};

class Hybrid : public Simple, public Scientific{
public:
void set(int a, int b){
setInfo_Sim(a, b);
setInfo_Sci(a, b);
}
void process(void){
process_Sim();
process_Sci();
}
void display(void){
display_Sim();
display_Sci();
}
};

int main(){
int a, b;
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;

Hybrid object;
object.set(a, b);
object.process();
object.display();

return 0;
}

abhikgupta
Автор

/* Create two classes :
1. SimmpleCalculator- Takes input of 2 number using a utility function and perfoms +, -, *, /
and displays the result using another function
2. ScientificCalculator - Takes input of 2 numbers using a utility functionand performs any four scientific operations
of your choice and displays the results using another functions

create a class hybridcalculator and inhherit it usinng these 2 classes:
Q1. What type of inheritance you are using ?--> Multiple
Q2. Which mode of inheritance are you using?--> public
Q3. Create an object of hybridcalculator and displaybresults of simple and scientific calculator.
Q4. Hpw is code reusibility implemented?
*/
#include <iostream>
#include <cmath>
using namespace std;

class simplecalculator
{
public:
int a;
int b;
char c;
void getdata()
{

cout << "Enter the two number "<<endl;
cin >> a >> b;
}
void oprate()
{
cout << "Enter the operator (+, -, *, /) : ";
cin >> c;

switch (c)
{
case '+':
cout <<"The sum of both number = " <<a + b;
break;

case '-':
cout <<"The diff of both number = " <<a - b;
break;

case '*':
cout <<"The multiplication of both number = "<< a * b;
break;

case '/':
if (b>0)
{
cout <<"The division of both number = " <<a / b;

}
else cout<<"it is not divisible ";

break;

default:
cout<< " Invalid operator ";
break;
}
}
};
class scientificcalculator{
public:
int x;
char y;
void getdata(){
cout<<"Enter the number: ";
cin>>x;
}
void oprate(){
cout<<"use this letters for above operators:\n"
<<"(log= l, sqrt= s, exp= e and cos = c ) \n";
cout<<"Enter the operator (log, sqrt, exp, cos): ";

cin>>y;

switch (y)
{
case 'l':
cout<< "The log of the given number is = "<<log (x);

break;
case 's':
cout<<"The square root of the given number is = " <<sqrt (x);
break;
case 'e':
cout<< "The exponent of the given number is = "<<exp(x);
break;
case 'c':
cout<<"The cos of the given number is = "<< cos(x);
break;

default:
cout<<"Invalid Operator ";
break;
}
}
};

class hybridcalculator : public simplecalculator, public scientificcalculator{
public:
char d;
void choose(){
cout<<"For Simple calculator (press s) & For Scientific calculator (press c): ";
cin>>d;
switch (d)
{
case 's':
simplecalculator::getdata();
simplecalculator::oprate();
break;

case 'c':


break;
default:
cout<<" Invalid character";
break;
}
}
};
int main()
{
hybridcalculator calc;
calc.choose();
return 0;
}

abhaygupta
Автор

4:13
1. Multiple Inheritance.
2. Public mode

sandeep_cse_
Автор

Sir My name is Adarsh and and thanking u for give this exercise for practice.

*Answers -:*
1. Multiple Inheritance.
2. Public Mode of Inheritance.
4. We use reusability in deriving Simple and scientific calculator class in Hybrid calculator class.

*Code*
#include<iostream>
#include<math.h>
using namespace std;

class SimpleCalculator{
protected:
int a, b;
public:
void setnumber1(int x, int y ){
a = x;
b = y;
}
void getnumber1(){
cout<<"The addition of a and b is "<<(a+b)<<endl;
cout<<"The subtraction of a and b is "<<(a-b)<<endl;
cout<<"The multiplication of a and b is "<<(a*b)<<endl;
cout<<"The division of a and b is "<<(a/b)<<endl;
cout<<endl;
}
};

class ScientificCalculator{
protected:
int j, k, l, m;
public:
void setnumber2(int p, int q, int r, int s){
k = q;
l = r;
j = p;
m = s;
}
void getnumber2(){
cout<<"The square of numbers are -:"<<endl;
cout<<"The square of "<<j<<" is "<<(j*j)<<endl;
cout<<"The square of "<<k<<" is "<<(k*k)<<endl;
cout<<"The square of "<<l<<" is "<<(l*l)<<endl;
cout<<"The square of "<<m<<" is "<<(m*m)<<endl;
cout<<endl;
cout<<"The cube of numbers are -:"<<endl;
cout<<"The cube of "<<j<<" is "<<(j*j*j)<<endl;
cout<<"The cube of "<<k<<" is "<<(k*k*k)<<endl;
cout<<"The cube of "<<l<<" is "<<(l*l*l)<<endl;
cout<<"The cube of "<<m<<" im "<<(m*m*m)<<endl;
cout<<endl;

// you have to write log base 10 as log10() the the compiler give write value.
cout<<"The log of numbers are -:"<<endl;
cout<<"The log of "<<"log("<<j<<")"<<" is "<<log10(j)<<endl;
cout<<"The log of "<<"log("<<k<<")"<<" is "<<log10(k)<<endl;
cout<<"The log of "<<"log("<<l<<")"<<" is "<<log10(l)<<endl;
cout<<"The log of "<<"log("<<m<<")"<<" is "<<log10(m)<<endl;
cout<<endl;
cout<<"The Sine of numbers are -:"<<endl;
cout<<"The Sin of "<<"Sin("<<j<<")"<<" is "<<sin(j)<<endl;
cout<<"The Sin of "<<"Sin("<<k<<")"<<" is "<<sin(k)<<endl;
cout<<"The Sin of "<<"Sin("<<l<<")"<<" is "<<sin(l)<<endl;
cout<<"The Sin of "<<"Sin("<<m<<")"<<" is "<<sin(m)<<endl;
}
};

class HybridCalculator : public SimpleCalculator, public ScientificCalculator{
public:
void display(){
getnumber1();
getnumber2();
}
};

int main(){
HybridCalculator AAA;
AAA.setnumber1(4, 5);
AAA.setnumber2(7, 6, 4, 2);
AAA.display();
return 0;
}

rekhasrivastava
Автор

// Exercise1
// Multilevel inheritance
#include <iostream>
#include <cmath>
using namespace std;

class SimpleCalculator
{
protected:
int num1, num2;
void get()
{
cout << "Enter value of first number: ";
cin >> num1;
cout << "Enter value of second number: ";
cin >> num2;
}
void display()
{
cout << "The first numebr is: " << num1 << endl;
cout << "The second numebr is: " << num2 << endl;
}
void calc_simple()
{
int a;
cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication, 4 for division: ";
cin >> a;
switch (a)
{
case 1:
cout << "The answer is: " << num1 + num2 << endl;
break;
case 2:
cout << "The answer is: " << num1 - num2 << endl;
break;
case 3:
cout << "The answer is: " << num1 * num2 << endl;
break;
case 4:
cout << "The answer is: " << num1 / num2 << endl;
break;
default:
cout << "Invalid input!" << endl;
break;
}
}
};

class ScientificCalculator : public SimpleCalculator
{
protected:
int num;
void set()
{
cout << "Enter number: ";
cin >> num;
}
void calc_scientific()
{
int a;
cout << "Enter 1 for natural log, 2 for common log, 3 for sqrt, 4 for absulute value: ";
cin >> a;
switch (a)
{
case 1:
cout << "The answer is: " << log(num) << endl;
break;
case 2:
cout << "The answer is: " << log10(num) << endl;
break;
case 3:
cout << "The answer is: " << sqrt(num) << endl;
break;
case 4:
cout << "The answer is: " << abs(num) << endl;
break;
default:
cout << "Invalid input!" << endl;
break;
}
}
};

class HybridCalculator : public ScientificCalculator
{
public:
void calc()
{
cout << "\nFunctions of simple calculator" << endl;
get();
calc_simple();
cout << "\nFunctions of scientific calculator" << endl;
set();
calc_scientific();
}
};

int main()
{
HybridCalculator h1;
h1.calc();
return 0;
}

rohansaini
Автор

1. I'm using multiple inheritence.
2. I'm using Public dispaly mode.
3. Code is written below the answers.
4. Here resuability is implemented because i reused the code in scientific and derived class.

Code -->

#include <iostream>
#include <cmath>
using namespace std;

class simple
{
protected:
int a, b;

public:
void this1data(int value1, int value2)
{
a = value1;
b = value2;
}

void print(void)
{
cout << "Sum of these numbers is " << (a + b) << endl;
cout << "Substraction of these numbers is " << (a - b) << endl;
cout << "Multiplication of these numbers is " << (a * b) << endl;
cout << "Division of these numbers is " << (a / b) << endl;
}
};

class scientific
{
protected:
int q, p;

public:
void this2data(int num1, int num2)
{
p = num1;
q = num2;
}

void show(void)
{
cout << "Square root of number 1 is " << sqrt(p) << endl;
cout << "Square root of number 2 is " << sqrt(q) << endl;
cout << "Cube root of number 1 is " << cbrt(p) << endl;
cout << "Cube root of number 2 is " << cbrt(q) << endl;
}
};

class derived : public simple, public scientific
{
public:
void set(int a, int b)
{
cout << "Simple calculator -->" << endl;
cout << endl;
simple ::this1data(a, b);
simple ::print();
cout << endl;
}
void get(int p, int q)
{
cout << "Scientific calculator -->" << endl;
cout << endl;
scientific ::this2data(p, q);
scientific ::show();
}
};

int main()
{
int x;
cout << "Enter the value of number 1 : " << endl;
cin >> x;
cout << endl;

int y;
cout << "Enter the value of number 2 :" << endl;
cin >> y;
cout << endl;

derived der;
der.set(x, y);

derived ders;
ders.get(x, y);
return 0;
}

VIOLENT_WHISPERER
Автор

A1. We are using multiple inheritance.
A2. We are using public display mode.
A4. Code reusability is implemented as we reuse the typed code in HybridCalculator

CETInsights
Автор

1) Used Multi-level Inheritance. By the way I can use Multiple too.
2) Used Public visibility mode.
3) Add, Sub, Mul, Div functions are reused and not repeated them for Scientific Calculator.
4) Derived class used only to call the functions made in Scientific and Simple Calculator.

Uzairkhan-dvhz
Автор

Q1.- Multiple Inheritance

Q2.- Public Mode

Q3.-
HybridCalculator h;
int num1, num2;
cout<<"Enter two numbers "<<endl;
cin>>num1>>num2;
h.set1(num1, num2);
h.set2(num1, num2);
h.show();

Q4.- Functions of both the base class as well as the data members of the classes are inherited

/*Below is the whole program*/

#include<iostream>
#include<cmath>
using namespace std;
class SimpleCalculator{
protected:
int a, b;
public:
void set1(int n1, int n2){
a=n1;
b=n2;
}
void display1(){
cout<<"The sum of two numbers is "<<a+b<<endl;
cout<<"The difference of two numbers is "<<a-b<<endl;
cout<<"The multiplication of two numbers is "<<a*b<<endl;
cout<<"The division of two numbers is "<<int(a/b)<<endl;
}
};
class ScientificCalculator{
protected:
int a, b;
public:
void set2(int no1, int no2){
a=no1;
b=no2;
}
void display2(){
cout<<"The sine of two numbers are "<<sin(a)<<" "<<sin(b)<<endl;
cout<<"The cosine of two numbers are "<<cos(a)<<" "<<cos(b)<<endl;
cout<<"The tan of two numbers are "<<tan(a)<<" "<<tan(b)<<endl;
cout<<"The logarithms of two numbers are "<<log(a)<<" "<<log(b)<<endl;
}
};

class HybridCalculator:public SimpleCalculator, public ScientificCalculator{
public:
void show(){
display1();
display2();
cout<<"The calculations are done "<<endl;
}

};
int main(){
HybridCalculator h;
int num1, num2;
cout<<"Enter two numbers "<<endl;
cin>>num1>>num2;
h.set1(num1, num2);
h.set2(num1, num2);
h.show();
}

arpitnayak
Автор

A1 . we are using multiple inheritance.
A2 . we use public visibility mode.
A4 . we just simply create data type of hybrid in main() and in hybrid class we call the functions of two classes with help of multiple inheritance

create program:
#include<iostream>
#include<cmath>
using namespace std;

class simple_calc
{
protected:
int a, b;
public :
void set_data1(int a1, int b1)
{
a = a1;
b = b1;
}
void display1()
{
cout<<"The addition of these two numbers is "<<a+b<<endl;
cout<<"The substraction of these two numbers is "<<a-b<<endl;
cout<<"The muliplication of these two numbers is "<<a*b<<endl;
cout<<"The division of these two numbers is "<<a/b<<endl;
cout<<"The modulus of these two numbers is "<<a%b<<endl;
}

};

class scientific_calc
{
protected:
int x, y;
public:
void set_data2(int n1, int n2)
{
x = n1;
y = n2;
}
void display2()
{
cout<<"The sine of x is "<<sin(x)<<endl;
cout<<"The sine of y is "<<sin(y)<<endl;
cout<<"The cose of x is "<<cos(x)<<endl;
cout<<"The cose of y is "<<cos(y)<<endl;
cout<<"The square root of x is "<<sqrt(x)<<endl;
cout<<"The square root of x is "<<sqrt(y)<<endl;
cout<<"The Log of x is "<<log(x)<<endl;
cout<<"The Log of y is "<<log(y)<<endl;
}
};

class derived : public simple_calc, public scientific_calc
{
public:
void set(int y1, int y2)
{
simple_calc :: set_data1(y1, y2);
simple_calc :: display1();
scientific_calc :: set_data2(y1, y2);
scientific_calc :: display2();
}
};

int main()
{
int p, q;
cout<<"Enter the two numbers "<<endl;
cin>>p>>q;

derived der;
der.set(p, q);
}

shahmirkhan
Автор

class simple
{
protected:
int x1, y1;
public:
void process()
{
cout<<"The addition is "<<x1+y1<<endl;
cout<<"The subtraction is "<<x1-y1<<endl;
cout<<"The multiplication is "<<x1*y1<<endl;
cout<<"The division is "<<x1/y1<<endl;
}
void set(int a, int b)
{
x1=a;
y1=b;
}
};

class scientific
{
protected:
int x2, y2;
public:
void process()
{
cout<<"The square root of x is "<<sqrt(x2)<<endl;
cout<<"The square root of y is "<<sqrt(y2)<<endl;
cout<<"The cube root of x is "<<cbrt(x2)<<endl;
cout<<"The cube root of y is "<<cbrt(y2)<<endl;
}
void set(int a, int b)
{
x2=a;
y2=b;
}
};

class hybrid: public simple, public scientific
{
public:
void masterset(int a, int b)
{
simple::set(a, b);
scientific::set(a, b);
simple::process();
scientific::process();
}
};

int main()
{
int p, q;
hybrid h;
cout<<" Enter two values "<<endl;
cin>>p>>q;

h.masterset(p, q);
return 0;
}

harshitkhanna
Автор

1) mulitple inheritance.
2) public mode.
3) We used the function direct in HybridCalculator or we use the function of both class simplecalculator and scientific calculator whitout repeating the code again in hybridcalculator class.


#include<math.h>
#include <iostream>
using namespace std;
class simplecalculator{
int a, b, plus, minus, multi, divide;
public:
void Operator(int, int);
void display();
};
void x, int y){
a=x;
b=y;
plus=(a+b);
minus=(a-b);
multi=a*b;
divide=a/b;
}
void simplecalculator::display(){
cout<<a<<" + "<<b<<" = "<<plus<<endl;
cout<<a<<" - "<<b<<" = "<<minus<<endl;
cout<<a<<" * "<<b<<" = "<<multi<<endl;
cout<<a<<" / "<<b<<" = "<<divide<<endl;
}
class scientificCalculator{
int a, b, exponential;
public:
void Exponential(int x, int y){
a=x;
b=y;
exponential=pow(a, b);
}
void display2(){
cout<<a<<" ^ "<<b<<" = "<<exponential<<endl;
}

};
class HybridCalculator:public simplecalculator, public scientificCalculator{
int a, b;
public:
void class3(int k, int l){
a=k;
b=l;
Operator(a, b);
display();
Exponential(a, b);
display2();

}

};
int main(){
HybridCalculator h;
h.class3(2, 3);
return 0;
}

preetiprajapat
Автор

Ans 1. I am using multilevel inheritance
Ans 2. I am using public mode of inheritance
code:
#include<iostream>
#include<cmath>
using namespace std;


class simplecalculator{
protected:
int a, b;

public:

void input(){
cout<<"Enter your first number "<<endl;
cin>>a;
cout<<"Enter your second number "<<endl;
cin>>b;

}


void output(){
cout<<"Multipication is "<<a*b<<endl;
cout<<"Division is "<<a/b<<endl;
cout<<"Subtraction is "<<a-b<<endl;
cout<<"Addition is "<<a+b<<endl;
}
};

class scientificcalculator:public simplecalculator{
public:
void soperations()
{
input();
output();
cout<<"Square root of a is "<<sqrt(a)<<endl;
cout<<"Cuberoot of a is "<<cbrt(a)<<endl;
cout<<"Square root of b is "<<sqrt(b)<<endl;
cout<<"Cuberoot of b is "<<cbrt(b)<<endl;
cout<<"a to the power b is "<<pow(a, b)<<endl;

}
};

class Hybridcalculator:public scientificcalculator{
public:
void display(){
cout<<"The result of both simple and scientific calculator operations are "<<endl;

soperations();
}
};

int main(){
Hybridcalculator h;
h.display();
return 0;
}

devanshdrp
Автор

#include <iostream>
#include <math.h>
using namespace std;
/*Create 2 classes:
1.SimpleCalculator - takes input of 2 numbers using a utility function and performs +, -, *, /
2.ScientificCalculator - Takes input of 2 numbers using a utility function performs any four scientific
operations of your choice.

Create another class name as hybridcalculator and inherit it using these 2 classes:
Q1] What type of inheritance are u using ?
Q2] Which mode of inheritance are u using?

*/
int a, b;
class SimpleCalculator
{

public:
void get_data()
{
cout << "Enter the value of a " << endl;
cin >> a;
cout << "Enter the value of b " << endl;
cin >> b;
}

void operation()
{
cout << "The addition of a and b is " << a + b << endl;
cout << "The substraction of a and b is " << a - b << endl;
cout << "The multiplication of a and b is " << a * b << endl;
cout << "The division of a and b is " << a / b << endl;
}
};

class ScientificCalculator
{
protected:
int x, y;

public:
void operation()
{

cout << "The square root of x is " << sqrt(a) << endl;
cout << "The square root of y is " << sqrt(b) << endl;
cout << "The cube root of x is " << cbrt(a) << endl;
cout << "The cube root of y is " << cbrt(b) << endl;
}
};

class Hybrid : public SimpleCalculator, public ScientificCalculator
{
public:
void total_operations()
{



}
};
int main()
{
Hybrid H1;
H1.get_data();

H1.total_operations();

return 0;
}

Автор

One day, you will have a million views in every video. ❤️❤️

arnabmaiti
Автор

It was pretty easy to be honest, there are still a couple of improvements needed in my program.

#include <iostream>
#include <cmath>
using namespace std;
class SimpleCalculator
{
protected:
float a, b, c, i;

public:
void enterData()
{
cout << "Enter two numbers a and b " << endl;
cin >> a >> b;

cout << "Choose the operations two be performed \n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division " << endl;
cin >> i;
}

void simpleCalculation()
{
if (i == 1)
{
c = a + b;
}
else if (i == 2)
{
c = a - b;
}
else if (i == 3)
{
c = a * b;
}
else
{
c = a / b;
}
}

void showResult()
{
cout << "Your result is " << c << endl;
}
};
class scientificCalculator
{
protected:
int a, i, c;

public:
void enterNumber()
{
cout << "Enter the value of number a " << endl;
cin >> a;

cout << "Choose the operation to be performed \n 1. Square \n 2. Cube \n 3. SquareRoot \n 4. CubeRoot " << endl;
cin >> i;
}

void Calculation()
{
if (i == 1)
{
c = a * a;
}

else if (i == 2)
{
c = a * a * a;
}

else if (i == 3)
{
c = sqrt(a);
}

else
{
c = cbrt(a);
}
}

void showCalculation()
{
cout << "The result is of the choosen operation is " << c << endl;
}
};

class HybridCalculator : public SimpleCalculator, public scientificCalculator
{
};

int main()
{
SimpleCalculator simple;
simple.enterData();
simple.simpleCalculation();
simple.showResult();

scientificCalculator scientific;
scientific.enterNumber();
scientific.Calculation();


HybridCalculator inherSimple, inherScienti;
// Simple Calculator
inherSimple.enterData();

inherSimple.showResult();

// Scientific Calculator
inherScienti.enterNumber();
inherScienti.Calculation();


return 0;
}

deepnaik
Автор

1)Multiple Inheritance.
2) visibility mode for inheritance is public.
3)
#include <iostream>
#include <math.h>

using namespace std;

class Simplecalculator
{
protected:
float a, b;

public:
void setnumber(float num1, float num2)
{
a = num1;
b = num2;
}
void display1()
{
cout << "The sum of two numbers are: " << a << "+" << b << "=" << (a + b) << endl;
cout << "The difference of two numbers are: " << a << "-" << b << "=" << (a - b) << endl;
cout << "The multiplication of two numbers are: " << a << "X" << b << "=" << (a * b) << endl;
cout << "The divison of two numbers are: " << a << "/" << b << "=" << (a / b) << endl;
}
};

class Scientificcalculator
{
protected:
float x, y;

public:
void setdata(float num1, float num2)
{
x = num1;
y = num2;
}
void display2()
{
cout << "power: " << pow(x, y) << endl;
cout << "Sine: " << sin(x) << " " << sin(y) << endl;
cout << "Squareroot: " << sqrt(x) << " " << sqrt(y) << endl;
cout << "Cosine: " << cos(x) << " " << cos(y) << endl;
}
};
class Hybridcalculator : public Simplecalculator, public Scientificcalculator
{

public:
void display_result()
{

cout << a << "+" << sqrt(x) << "is"<<"="<<(a + sqrt(x)) << endl;
cout << a << "X" << pow(x, y)<<"is"<<"="<< (a * pow(x, y)) << endl;
}
};

int main()
{
Hybridcalculator cal;
cal.setnumber(1, 2);
cal.setdata(1.0, 2.0);
cal.display1();
cal.display2();
cal.display_result();

return 0;
}
4) As we are using multiple inheritance here, inherited class can access all the functionality of base class, and the base class's code can be reused in the derived class.

arpitamishra