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++)     ...

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...

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();      ...

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;     }    ...

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 chann...

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; ...

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 a...

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){     ...

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;           ...