Solution to Exercise on Cpp Inheritance | C++ Tutorials for Beginners #47

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
Рекомендации по теме
Комментарии
Автор

Could you please make a project in C++ which include many of the topics covered by you in c++ course ?

arunangshubiswas
Автор

You are a great teacher.I have searched many course and I haved done those. But every one's teaching method is not so learning friendly like you. That's why everyone watchs your cousrse

manishchymanishchy
Автор

Challenge Accepted
#include <iostream>
#include <math.h>
using namespace std;

class SimpleCalculator
{
int a;
int b;

public:
void input1(void)
{
cout << "You are using Simple Calculator:" << endl;
cout << "Enter the value of a :";
cin >> a;
cout << "Enter the value of b :";
cin >> b;
}
void displayresult1()
{
cout << "a+b = " << a + b << endl;
cout << "a-b = " << a - b << endl;
cout << "a*b = " << a * b << endl;
cout << "a/b = " << a / b << endl;
}
};

class ScientificCalculator
{
int a;
int b;

public:
void input2(void)
{
cout << "You are using Scientific Calculator:" << endl;
cout << "Enter the value of a :";
cin >> a;
cout << "Enter the value of b :";
cin >> b;
}
void displayresult2()
{
cout << "sqrt(a*b) = " << float(sqrt(a * b)) << endl;
cout << "sin(a+b) = " << float(sin(a + b)) << endl;
cout << "a raise to power b = " << pow(a, b) << endl;
cout << "sin^-1(a+b) = " << float(asin(a + b)) << endl;
}
};

class HybridCalculator : public SimpleCalculator, public ScientificCalculator // Multiple Inheritance
{
public:
void usehybrid(void)
{
input1();
displayresult1();
input2();
displayresult2();
}
};

int main()
{
HybridCalculator c;
c.usehybrid();

return 0;
}

RahulYadav-jkum
Автор

a/b ko float me typecast karna chahiye th. It will not return 0 for values less than 1 and also make it precise.

AshutoshKumar-fuqe
Автор

4:27 kiya tha maine solve par jab aap solve kr rhe ho to aur bhi ache se samajh aa rha hai like suddenly solution ko dekhne ka nazariya badal gya jaise hi aapne solve krna shuru kiya

kumkumbhagya
Автор

Pin This Harry Bhai :
if you verify these sin cos and tan values you'll find they are wrong but truly they aren't wrong...because sin(parameter) take values in radians not in degrees so to get correct value for degree use sin(parameter*PI/180) same for cos and tan...
Note: here parameter is the input value in degrees
and you need to declare PI value to 3.14159265
Code:
#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159265;

class SimpleCalculator{

int a, b;

public:
void getdataSimp(){
cout<<"Enter the value of a"<<endl;
cin>>a;
cout<<"Enter the value of b"<<endl;
cin>>b;
}
void performoperationSimp(){
cout<<"the value of a + b is : "<<a+b<<endl;
cout<<"the value of a - b is : "<<a-b<<endl;
cout<<"the value of a * b is : "<<a*b<<endl;
cout<<"the value of a / b is : "<<a/b<<endl;

}

};

class ScientificCalculator{

int a, b;

public:
void getdataSci(){
cout<<"Enter the value of a"<<endl;
cin>>a;
cout<<"Enter the value of b"<<endl;
cin>>b;
}
void performoperationSci(){
cout<<"the value of sin(a) + sin (b) is : //we need to use degree*PI/180 because sin takes angle values in radians to convert angle to radian use (degree*PI/180)
cout<<"the value of cos(a) + cos(b) is : functions take parameter values in radians so to get correct value convert degree to radian
cout<<"the value of tan(a) + tan(b) is :
cout<<"the value of log(a) + log(b) is : "<<log10(a)+log10(b)<<endl;

}

};

class HybridCalculator:public SimpleCalculator, public ScientificCalculator{


};

int main(){

HybridCalculator h;
h.getdataSimp();
h.performoperationSimp();

h.getdataSci();
h.performoperationSci();

return 0;
}

ankurdahiya
Автор

/*
create 2 classes:
1.simpleCalculator-> takes input of two numbers using utility function and perform +, -, *, / and displays the result using another function.

2.ScientificCalculator->takes input of two numbers using utility function and perform any four scientific operations of your choice and display the result using another function.

Now create another Hybridcalculator and inherit the above two claases in it.

*/

#include<iostream>
#include<math.h>
using namespace std;
class simple_calculator{
protected:
float x, y;
public:
char opr_S;
void set_numbers(int a, int b){
x=a;
y=b;
}
void Operands(){
cout<<"Enter your operand!!"<<endl;
cin>>opr_S;
}
void Simp_operations(){

switch (opr_S)
{
case '+':
cout<<"sum="<<x+y<<endl;
break;
case '-':

break;
case '*':
cout<<"product="<<x*y<<endl;
break;
case '/':

break;

}
}
};

class scientific_calculator:public simple_calculator{
public:
//char opr_Sc;
// void scientific_Operands(){
// cout<<"Enter your scientific operand!!"<<endl;
// cin>>opr_Sc;
// }
void scientific_cal(){

switch (opr_S)
{
case '^':
cout<<"X^Y="<<pow(x, y);
break;
case 'S':
cout<<"sin(x)="<<sin(x);
break;
case 'R':
cout<<"square underroot="<<sqrt(x);
break;
case '!':
int fact=1;
for (int i = 1; i <=x; i++)
{
fact=i*fact;
}
cout<<"X!="<<fact;
break;
}
}
};

class hybrid_calculator:public scientific_calculator{
public:
void H_oprn(){
if (opr_S == '+'|| opr_S== '-' || opr_S == '*' || opr_S == '/')
{
Simp_operations();
}
else{
scientific_cal();
}
}
};

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

hybrid_calculator H;
H.set_numbers(p, q);
H.Operands();
H.H_oprn();

return 0;
}

thethinker
Автор

I have done it correctly broo..

I'm very happy that im going in a correct way of learning with your vedios .. thanks a lot ..

yash._
Автор

We can also make calculator using switch case (that make real one and has more sense).

eeshmidha
Автор

//No special calling required. Simply create an object of HybridCalculator class and everything will happen automatically (:
#include<iostream>
#include<cmath>
using namespace std;

class SimpleCalculator{
protected:
int add(int a, int b);
int substraction(int a, int b);
int product(int a, int b);
double divide(int a, int b);
public:
void useSimpleCalc(int num1, int num2){
cout<<"Numbers Entered: "<<num1<<" and "<<num2<<endl;
int choice;
cout<<"Enter your choice from below:"<<endl
<<"1. Addition\n"<<"2. Substraction\n"
<<"3. Product\n"<<"4. Division"<<endl;
cout<<"Choice: ";
cin>>choice;
if(choice==1){
int res = add(num1, num2);
cout<<"Result: "<<res<<endl;
}else if(choice==2){
int res = substraction(num1, num2);
cout<<"Result: "<<res<<endl;
}else if(choice==3){
int res = product(num1, num2);
cout<<"Result: "<<res<<endl;
}else if(choice==4){
double res = divide(num1, num2);
cout<<"Result: "<<res<<endl;
}else{
cout<<"The choice entered is invalid"<<endl;
exit(0);
}
}
};
int SimpleCalculator :: add(int a, int b){
return a+b;
}
int SimpleCalculator :: substraction(int a, int b){
return a-b;
}
int SimpleCalculator :: product(int a, int b){
return a*b;
}
double SimpleCalculator :: divide(int a, int b){
return (double(a))/(double(b));
}

class ScientificCalculator{
protected:
float logBase10(float num);
float squareRoot(float num);
float exponential(float num);
float sin(float num);
public:
void useScientificCalc(float num){
cout<<"Number Entered: "<<num<<endl;
int choice;
cout<<"Enter your choice from below:"<<endl
<<"1. Log Base 10\n"<<"2. Square Root\n"
<<"3. Exponential\n"<<"4. Sin(-angle-)"<<endl;
cout<<"Choice: ";
cin>>choice;
if(choice==1){
float res = logBase10(num);
cout<<"Result: "<<res<<endl;
}else if(choice==2){
float res = squareRoot(num);
cout<<"Result: "<<res<<endl;
}else if(choice==3){
float res = exponential(num);
cout<<"Result: "<<res<<endl;
}else if(choice==4){
float res = sin(num);
cout<<"Result: "<<res<<endl;
}else{
cout<<"The choice entered is invalid"<<endl;
exit(0);
}
}
};
float ScientificCalculator :: logBase10(float num){
return log10f(num);
}
float ScientificCalculator :: squareRoot(float num){
return sqrtf(num);
}
float ScientificCalculator :: exponential(float num){
return expf(num);
}
float ScientificCalculator :: sin(float num){
return sinf(num);
}

class HybridCalculator : public SimpleCalculator, public ScientificCalculator{
public:
HybridCalculator(){
int choice;
cout<<"Which Calculator you want to use from below: "<<endl
<<"1. Simple Calculator\n"<<"2. Scientific Calculator"<<endl;
cout<<"Choice: ";
cin>>choice;
if(choice==1){
int num1, num2;
cout<<"Enter two numbers:"<<endl;
cin>>num1>>num2;
useSimpleCalc(num1, num2);
}else if(choice==2){
int num;
cout<<"Enter a number or an angle:"<<endl;
cin>>num;
useScientificCalc(num);
}else{
cout<<"The choice entered is Invalid"<<endl;
exit(0);
}
}
~HybridCalculator(){
cout<<"This object of Hybrid Calculator is Destructed Now!"<<endl;
}
};

int main(){
HybridCalculator hbc;
return 0;
}

bruiciedoocie
Автор

Commenting for better reach because this guy is awesome

altamashsabri
Автор

#include <iostream>
#include <cmath> // OR #include<math.h>
using namespace std;

class SimpleCalculator
{
protected:
int num1, num2;

public:
void set_numbersSimple()
{
cout << "Enter the value for first number: " << endl;
cin >> num1;

cout << "Enter the value for second number: " << endl;
cin >> num2;
}

int sum()
{
return num1 + num2;
}

int subtract()
{
return num1 - num2;
}

int difference()
{
return abs(num1 - num2);
}

int product()
{
return num1 * num2;
}

float division()
{
return (float)num1 / num2;
}

void display_01()
{
cout << "The sum is: " << sum() << endl;
cout << "The subtraction is: " << subtract() << endl;
cout << "The difference is: " << difference() << endl;
cout << "The product is: " << product() << endl;
cout << "The division is: " << division() << endl;
}
};

class ScientificCalculator
{
protected:
int num1, num2;

public:
void set_numbersScientific()
{
cout << "Enter the value for first number: " << endl;
cin >> num1;

cout << "Enter the value for second number: " << endl;
cin >> num2;
}

float square_root()
{
return sqrt(num1);
}

float sine()
{
return sin(num1);
}

float cosine()
{
return cos(num1);
}

float tangent()
{
return tan(num1);
}

int power()
{
return pow(num1, num2);
}

void display_02()
{
cout << "The square root of first number is: " << square_root() << endl;
cout << "The value of sin(first number) is: " << sine() << endl;
cout << "The value of cos(first number) is: " << cosine() << endl;
cout << "The value of tan(first number) is: " << tangent() << endl;
cout << "The power of first number raised to second is: " << power() << endl;
}
};

class HybridCalculator : public SimpleCalculator, public ScientificCalculator
{
};

int main()
{
HybridCalculator h;

h.set_numbersSimple();
h.display_01();

h.set_numbersScientific();
h.display_02();

return 0;
}

imPriyansh
Автор

/*
1. I am using multi-level inheritance
2. public mode
3. code reusability is implemented in this programm by using multi-level inheritance
*/
#include<iostream>
using namespace std;
class simpcal
{
protected:
int a, b;
public:
int x, y;
void setvalues()
{
a=x;
b=y;
}
void displaysimp()
{
cout << "\n The value of "<<a<<"+"<<b<<" is " << a+b
<< "\n The value of "<<a<<"-"<<b<<" is " << a-b
<< "\n The value of "<<a<<"*"<<b<<" is " << a*b
<< "\n The value of "<<a<<"/"<<b<<" is " << a/b;

}
};
class scical: public simpcal
{
public:
void displaysci()
{
cout << "\n The value of sin("<<a<<"+"<<b<<") is " << "sin("<<a+b<<")"
<< "\n The value of sin("<<a<<"-"<<b<<") is " << "sin("<<a-b<<")"
<< "\n The value of sin("<<a<<"*"<<b<<") is " << "sin("<<a*b<<")"
<< "\n The value of sin("<<a<<"/"<<b<<") is " << "sin("<<a/b<<")";
}
} ;
class hycal : public scical
{
public:
hycal()
{
cout<<"\n Enter the value of a \n";
cin>>x;
cout<<"\n Enter the value of b \n";
cin>>y;
setvalues();
displaysimp();
displaysci();
}
};
int main(){
hycal a;
return 0;
}

karandeepkhosa
Автор

Thanks a lot for providing solutions ❤️❤️

lovetocode
Автор

We can also use #include<math.h> ?

amangupta
Автор

/*
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? //multiple inheritance
Q2. Which mode of Inheritance are you using? //Public inheritance
Q3. Create an object of HybridCalculator and display results of the simple and scientific calculator.
Q4. How is code reusability implemented?
*/
#include<iostream>
#include<math.h>
using namespace std;
class SimpleCalculator{
protected:
int n1, n2;
public:
void takeval(){
cout<<"Enter the first value: "<<endl;
cin>>n1;
cout<<"Enter the second value: "<<endl;
cin>>n2;
}
int sum(){
return n1+n2;
}
int diff(){
return n1-n2;
}
int mul(){
return n1*n2;
}
int div(){
return n1/n2;
}
void show1(){
cout<<"sum of "<<n1<<"and "<<n2<<"is = "<<sum()<<endl;
cout<<"difference of "<<n1<<"and "<<n2<<"is = "<<diff()<<endl;
cout<<"product of "<<n1<<"and "<<n2<<"is = "<<mul()<<endl;
cout<<"quotient of "<<n1<<"and "<<n2<<"is = "<<div()<<endl;

}
SimpleCalculator(int l, int m):n1(l), n2(m){};
};
class ScientificCalculator{
protected:
int n1, n2;
public:
int power(){
return pow(n1, n2);
}
int fac(int n){
if(n==0||n==1){
return 1;
}
return n*fac(n-1);
}
float sinOp(int n){
return sin(n);
}
float cosOp(int n){
return cos(n);
}
void show2(){
cout<<"value of "<<n1<<"raise to power "<<n2<<"is: "<<power()<<endl;
cout<<"Factorial value of "<<n1<<"is : "<<fac(n1)<<endl;
cout<<"Factorial value of "<<n2<<"is : "<<fac(n2)<<endl;
cout<<"sine value of "<<n1<<"is : "<<sinOp(n1)<<endl;
cout<<"sine value of "<<n2<<"is : "<<sinOp(n2)<<endl;
cout<<"cosine value of "<<n1<<"is : "<<cosOp(n1)<<endl;
cout<<"cosine value of "<<n2<<"is : "<<cosOp(n2)<<endl;
}
ScientificCalculator(int l, int m):n1(l), n2(m){};

};
class HybridCalculator: virtual public SimpleCalculator, virtual public ScientificCalculator{
public:
HybridCalculator(int a, int b):SimpleCalculator(a, b), ScientificCalculator(a, b){};


};
int main(){
HybridCalculator h(30, 45);
h.show1();
h.show2();

return 0;
}

Abc-rzps
Автор

Love you bhai.Thanks alot You are Helping me so much.🙏🙏🙏

pushpender_rajput
Автор

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

public:
void set_numbers(float l, float k){

a=l;
b=k;

}
void calculation1(){

cout << "sum is " <<a+b<< endl;
cout << "minus is"<<a-b<< endl;
cout << "into is "<<a*b<< endl;
cout << "divide is "<<a/b << endl;



}
};
class scientificcalculator{

protected:
float p, s;

public:
void set_numbers2(float m, float q){

p=m;
s=q;
}

void calculation2(){
cout << "square " <<p*p<< endl;
cout << "square " <<s*s<< endl;
cout << "cube "<<p*p*p << endl;
cout << "cube "<<s*s*s << endl;
cout << "square root "<<sqrt(p) << endl;
cout << "square root "<<sqrt(s) << endl;
cout << "cube root "<<cbrt(p) << endl;
cout << "cube root "<<cbrt(s) << endl;
}
};
class hybrid : public simplecalculator, public scientificcalculator{



};






int main(){

/*simplecalculator ra;
scientificcalculator ra1;
ra.set_numbers(3.0, 4.0);
ra1.set_numbers2(5.0, 6.0);
ra.calculation1();
ra1.calculation2();*/


hybrid rrr;
rrr.set_numbers(2.0, 3.0);
rrr.calculation1();
rrr.set_numbers2(4.0, 5.0);
rrr.calculation2();
return 0;





}

RahulSingh-ttfk
Автор

Ab bhaiya ek project banao class ki help se taki aur mazza aye😁

ashpreetsinghanand
Автор

Please make a tutorial on Deep learning with tensorflow ♥️

giyu
welcome to shbcf.ru