Posts

Showing posts from May, 2021

Strings conversion-basic concepts

 stoi(//argument) function is used to convert string into int to_string(//argument) function is used to convert int into string Check Aman Dhattarwal's channel for strings

Hackerrank- Vector Sort problem(solved)

#include   < cmath > #include   < cstdio > #include   < vector > #include   < iostream > #include   < algorithm > using   namespace  std; int  main() {      /* Enter your code here. Read input from STDIN. Print output to STDOUT */        vector < int > vec;      int  size;     cin>>size;      for ( int  i= 0 ;i<size;i++)     {          int  a;         cin>>a;         vec.push_back(a);     }      sort(vec.begin(),vec.end());       for ( int  i= 0 ;i<size;i++)      cout<<vec[i]<< " " ;      return   0 ; } Hac

STL-Everything about STL

Image
 

Hackerrank C++ class templates problem-(terminated due to timeout)

  #include   < cmath > #include   < cstdio > #include   < vector > #include   < iostream > #include   < algorithm > #include   < cassert > using   namespace  std; /*Write the class AddElements here*/ template < class  T> class  AddElements{      protected :     T obj;      public :     AddElements(T ob)     {     obj=ob;     }      T add(T var)     {     obj=obj+var;      return  obj;     }     T concatenate(T var)     {      obj=obj+var;       return  obj;     } }; int  main () {    int  n,i;   cin >> n;    for (i= 0 ;i<n;i++) {     string type;     cin >> type;      if (type== "float" ) {          double  element1,element2;         cin >> element1 >> element2;         AddElements< double > myfloat (element1);         cout << myfloat.add(element2) << endl;     }      else   if (type ==  "int" ) {          int  element1, element2;         cin >> element1 >> element2;  

Templates in C++

Image
 

Files in C++-Mind Map

Image
 

Hackerrank-Accessing Inherited Functions problem-Inheritance

#include< iostream > using   namespace  std; class  A {      public :         A(){             callA =  0 ;         }      private :          int  callA;          void  inc(){             callA++;         }      protected :          void  func( int  & a)         {             a = a *  2 ;             inc();         }      public :          int  getA(){              return  callA;         } }; class  B {      public :         B(){             callB =  0 ;         }      private :          int  callB;          void  inc(){             callB++;         }      protected :          void  func( int  & a)         {             a = a *  3 ;             inc();         }      public :          int  getB(){              return  callB;         } }; class  C {      public :         C(){             callC =  0 ;         }      private :          int  callC;          void  inc(){             callC++;         }      protected :          void  func( int  & a)         {             a =

Hackerrank-Virtual Functions Question

#include   < cmath > #include   < cstdio > #include   < vector > #include   < iostream > #include   < algorithm > using   namespace  std; class  Person{      protected :      int  age;     string name;      public :      void   virtual  putdata( void )= 0 ;      void   virtual  getdata( void )= 0 ; }; static   int  cur_id_1= 1 ; static   int  cur_id_2= 1 ; class  Professor: public  Person{      private :      int  publications;           public :      void  getdata()     {         cin>>name>>age>>publications;     }      void  putdata()     {         cout<<name<< " " <<age<< " " <<publications<< " " <<cur_id_1<<endl;         cur_id_1++;     } }; class  Student: public  Person{      private :      int  marks[ 6 ],total= 0 ;      public :      void  getdata( void )     {         cin>>name>>age;          for ( int  i= 0 ;i< 6 ;i++)         {         

Inheritance-Virtual Functions Example

 #include <iostream> using namespace std; class CWH{ protected: float rating; public: CWH(float r){ cout<<"Hello"<<endl<<endl; rating=r; } virtual void display(void) {     cout<<"Harry bhaiya beautiful"<<endl; } }; class CWHvideo:public CWH{     protected:     string name;     float length;     int views;     public:     CWHvideo(string n,float l,int v,float r):CWH(r){        name=n;        length=l;        views=v;     }     void display(void)     {         cout<<"Name of our video channel is "<<name<<endl         <<"Length of the video is "<<length<<endl<<         "Total views on the video is "<<views<<endl<<         "Total rating of this video channel is "<<rating<<endl<<endl;     } }; class CWHtext:public CWH{     protected:     string name;     int words;     int views;     public:     CWHtext(string n,int w,i

Inheritance-Array of Objects Using Pointers in C++ -Example

 #include <iostream> using namespace std; class Dukan {     protected:     int item_number;     string item_name;     public:     void set_data(int num,string name)     {         item_name=name;         item_number=num;     }     void get_data()     {         cout<<"Item number : "<<item_number<<endl<<"Item name is : "<<item_name<<endl<<endl;     } }; int main() {     int size;     cin>>size; Dukan *d=new Dukan[size]; for(int i=0;i<size;i++) {     int num=0;     string name;     cin>>num>>name;     d->set_data(num,name);     d->get_data();     d++; } return 0; }

Inheritance-Use of constructors in classes-Example done by me

 #include <iostream> using namespace std; class MI {        protected:     int trophies_mi;     public:          MI(int t)     {            trophies_mi=t;         cout<<"Mi won cup for "<<trophies_mi<<endl;              }     void slogan_mi(void)     {         cout<<"Duniya hila denge hum"<<endl;     } }; class CSK {      protected:     int trophies_csk;     public:          CSK(int t)     {            trophies_csk=t;         cout<<"CSK won cup for "<<trophies_csk<<endl;     }     void slogan_csk(void)     {         cout<<"Whistle podu"<<endl;     } }; class IPL:public CSK,public MI{      protected:     int years=0;     public:     IPL(int a,int b,int c):CSK(b),MI(a) {     years=c; }    void display()    {        cout<<"Thank you IPL for entertaining us for last "<<years<<"years"<<endl;    } }; int main() {        IPL i(5,3,15);     i.slo

Inheritance-Virtual Base Class( Example-Done by me)

 #include <iostream> using namespace std; class student{   protected:   int roll_number;   string name;   public:   void get_student_data(int r,string n)   {       roll_number=r;       name=n;   }   void display_student_data()   {       cout<<"The roll number is "<<roll_number<<endl;       cout<<"The name of student is "<<name<<endl;   }    }; class academics:virtual public student{     protected:     int score=0;     public:     void set_test_score(int m,int c,int p)     {         score=m+p+c;     }     void display_test_score()     {         cout<<"The total score is "<<score<<endl;         float average=float(score)/3;         cout<<"The average is "<<average<<endl;     } }; class sports: virtual public student{     protected:     char point;     public:     void set_sports_score(char sc)     {         point=sc;     }     void display_sports_point()     {         cou

Inheritance- Use of constructors in derived and base class

 #include<iostream> using namespace std; /* Case1: class B: public A{    // Order of execution of constructor -> first A() then B() }; Case2: class A: public B, public C{     // Order of execution of constructor -> B() then C() and A() }; Case3: class A: public B, virtual public C{     // Order of execution of constructor -> C() then B() and A() }; */ class Base1{     int data1;     public:         Base1(int i){             data1 = i;             cout<<"Base1 class constructor called"<<endl;         }         void printDataBase1(void){             cout<<"The value of data1 is "<<data1<<endl;         } }; class Base2{     int data2;     public:         Base2(int i){             data2 = i;             cout << "Base2 class constructor called" << endl;         }         void printDataBase2(void){             cout << "The value of data2 is " << data2 << endl;         } }; class

Inheritance-Virtual Base Class

Image
 #include<iostream> using namespace std; /* Inheritance: student -->test [Done] student-->sports [Done] test --> result [Done] sports --> result [Done] */ class Student{     protected:         int roll_no;     public:         void set_number(int a){             roll_no = a;         }         void print_number(void){             cout<<"Your roll no is "<< roll_no<<endl;         } }; class Test : public Student{     protected:         float maths, physics;         public:             void set_marks(float m1, float m2){                 maths = m1;                 physics = m2;             }             void print_marks(void){                 cout << "You result is here: "<<endl                      << "Maths: "<< maths<<endl                      << "Physics: "<< physics<<endl;             } }; class Sports: public Student{     protected:         float score;     publ