Posts

Showing posts from April, 2021

Inheritance- Code with Harry-Lecture 41 solution (Multiple inheritance)

Create 2 classes: 1. SimpleCalculator - Takes input of 2 numbers using a utility function and perfoms +, -, *, / and displays the results using another function. 2. ScientificCalculator - Takes input of 2 numbers using a utility function and perfoms any four scientific operation of your chioice 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 simple and scientific calculator. Q4. How is code reusability implemented?    Ans1. Multiple level inheritance Ans2. Public mode Ans3. #include <iostream> #include <cmath> using namespace std; class SimpleCalculator { protected: float result; int number_1,number_2; char operand; public: void get_number(int n1,int n2) { number_1=

Inheritance-Multiple Inheritance example

 #include <iostream> using namespace std; class Student_name{     protected:     string name;     public:     void set_name(string n)     {         name=n;     } }; class Student_roll_number{     protected:     int roll_number;     public:     void set_roll_number(int r)     {         roll_number=r;     } }; class Student_average:public Student_name,public Student_roll_number{     protected:     float mean;     public:     void average(int,int,int);     void print(void);      }; void Student_average::average(int math,int phy,int chem) {     float a=float(math+phy+chem)/3;     mean=a; } void Student_average:: print() {     cout<<"The name is "<<name<<endl;     cout<<"The roll number is "<<roll_number<<endl;     cout<<"The average is "<<mean<<endl;     cout<<"The percentage is "<<mean<<"%"<<endl; } int main() { Student_average a1; a1.set_name("Pee

Inheritance- protected access modifier

Image
 

Inheritance- concept theory

Image
 

Constructors example

 #include <iostream> using namespace std; class exam {     int total;     char grade;     void intro(void);     public:     exam(){         intro();     }     exam(int t,char g)     {            intro();         total=t;         grade=g;         cout<<"Your total marks are"<<total<<" and grade is "<<grade<<endl;              }     }; void exam::intro()     {         cout<<"Hello,Namaste,Sat sriyakal"<<endl;     }      int main() {     exam e1(95,'A');     exam e2; return 0; }

Hackerrank-C++-Class solution

  #include   < iostream > #include   < sstream > #include   < string.h > using   namespace  std; class  Student{      public :      int  age;      int  standard;     string first_name;     string last_name;           void  set_age( int  a)     {         age=a;     }      void  set_standard( int  s)     {         standard=s;     }      void  set_first_name(string s1)     {         first_name=s1;     }      void  set_last_name(string s2)     {         last_name=s2;     }      int  get_age()     {          return  age;     }     string get_last_name()     {          return  last_name;     }     string get_first_name()     {          return  first_name;     }      int  get_standard()     {          return  standard;     }     string to_string()     {         string data_1,data_2,data_3,data_4;              stringstream ss1;         ss1<<age;         ss1>>data_1;                  data_2=first_name;         data_3=last_name;                  stringstream ss2;   

OOPS-Passing objects by functions

 #include <iostream> using namespace std; class Complex {     private:     int a,b;     public:     void add(int a1, int b1)     {         a=a1;         b=b1;     }          void complexadd(Complex o1, Complex o2)     {         a=o1.a+o2.a;         b=o1.b+o2.b;     }          void display()     {         cout<<"The number is"<<a<<"+i"<<b<<endl;     }           }; int main() { Complex o1; o1.add(3,4); o1.display(); Complex o2; o2.add(5,6); o2.display(); Complex o3; o3.complexadd(o1,o2); o3.display(); return 0; }

OOPS_basic_2

 #include <iostream> #include <string> using namespace std; class maths{     private:     string s;          public:     void input(void);     void checkbinary(void);     void onecompliment();     void display(); }; void maths:: input() {   cin>>s;      } void maths:: checkbinary() {  for(int i=0;i<s.length();i++)  {      if(!(s.at(i)=='0'||s.at(i)=='1'))      {     cout<<"Not a binary number"<<endl;      exit(0);      }  } } void maths::onecompliment() {   for(int i=0;i<s.length();i++)   {       if(s.at(i)=='1')       s.at(i)='0';       else       s.at(i)='1';   } } void maths::display() {     cout<<s<<endl; } int main() {     maths first;     first.input();     first.display();     first.checkbinary();     first.onecompliment();     first.display(); return 0; }

OOPS in C++

 #include <iostream> using namespace std; class student {     private:     int a,b,c;          public:     int d,e;          void disdata()     {         cout<<"first num is "<<a<<endl;         cout<<"second num is "<<b<<endl;         cout<<"third num is "<<c<<endl;         cout<<"fourth num is "<<d<<endl;         cout<<"fifth num is "<<e<<endl;     }     void setdata(int a,int b,int c); }; void student:: setdata(int a1, int b1, int c1) {     a=a1;     b=b1;     c=c1; } int main() { student peeyush; peeyush.setdata(5,6,7); peeyush.d=8; peeyush.e=9; peeyush.disdata(); return 0; }