C++

[C++] Operator overloading Part2

#include<iostream>
#include<cstring>
#define MAX_STRING 100
using namespace std;
class myString
{
    friend istream& operator >> (istream&, myString&);
    friend ostream& operator << (ostream&, myString&);
    private:
        char content[MAX_STRING];
    public:
        char operator [] (int);
        myString operator += (myString);
};
istream &operator >> (istream &in, myString &temps)
{
    in >> temps.content;
    return in;
}
ostream &operator << (ostream &out, myString &temps)
{
    out << temps.content;
    return out;
}
char myString::operator [] (int n)
{
    return content[n];
}
myString myString::operator += (myString s)
{
    strcat_s(content, s.content);
    return *this;
}
int main()
{
    myString s1,s2;
    cin >> s1 >> s2;
    cout << s1[0]<<endl;
    cout << (s1 += s2) << endl;
    system("pause"); 
    return 0;
}

[C++] Operator Overloading

#include<iostream>
#include<cstdlib>

using namespace std;
class rec
{
    private:
    int width,length,area;
    public:
    rec(int w=1,int l=1)
    {
        width=w;
        length=l;
        area=w*l;
    }
    bool operator > (rec temp_rec)
    {
        if(area>temp_rec.area)
        {
            return true;
        }
        return false;
    }
    bool operator == (rec temp_rec)
    {
        if(area==temp_rec.area)return true;
        return false;
    }
};
int main()
{
    rec a(5,10),b(10,6);
    if(a>b)
    {
        cout<<"a>b\n";
    }
    else if(a==b)
    {
        cout<<"a=b\n";
    }
    else
    {
        cout<<"a<b\n";
    }
    system("pause");
    return 0;
}

[C++] Permutation

  • Rotate example: [ 1 2 3 4]-> 4 1 2 3 (right shift)
  • Permutation of 1 2 3 4: [] = rotate, () = recursion
    • [1] => 1 (2 3 4)
    • [1 2] => 2 (1 3 4)
    • [1 2 3] => 3 (1 2 4)
    • [1 2 3 4] => 4 (1 2 3)

  • Code
#include<iostream>
#include<vector>
using namespace std;

int count = 0;

void rotate(vector<int> &list, int x, int y) {
    int tmp = list[y];
    for (int i = y; i > x; i--) {
        list[i] = list[i-1];
    }
    list[x] = tmp;
}

void perm(int a, int n, vector<int> list){
    if (a < n){
        for (int i = a; i < n; i++) {
            vector<int> tmplist(list);
            rotate(tmplist, a, i);
            perm(a+1, n, tmplist);
        }
    }
    else{
        for (int i = 0; i < n; i++) {
            cout<<list[i]<<" ";
        }
        cout<<endl;
        count++;
    }
}

int main()
{
    int n;
    vector<int> list;
    cin>>n;
    list.resize(n);
    for (int i = 0;i < n; i++) {
        cin>>list[i];
    }
    perm(0, n, list);
    cout<<"count="<<count<<endl;
    return 0;
}

[C++] Polymorphism

/*(base)A->B->C->D(derived)
 B* b=new C;
 A* a=b; //OK

 A* a=new D;
 A* a=new B; //next line will return NULL
 C* c=dynamic_cast<C*>(a); //OK
*/
#include<iostream>

using namespace std;
class instrument
{
    public:
    virtual void play()=0;
};
class guitar:public instrument
{
    public:
    void play()
    {
        cout<<"Guitar played.\n";
    }
};
class piano:public instrument
{
    public:
    void play()
    {
     cout<<"Piano played.\n";
    }
};
int main()
{
    instrument *s=new guitar;
    //Guitar played
    s->play();
    delete s;
    //Piano played
    s=new piano;
    s->play();
    delete s;
    return 0;
}

[C++] getline(istream,string)

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
using namespace std;

int main()
{
 string s;
 char a[100];
 
 //std::istream::getline(char*,int) for char*
 cin.getline(a,100);
 cout<<a<<endl;
 
 //std::getline(istream&,string) for string
 getline(cin,s);
 cout<<s;
 return 0;
}

[C++] Binary File Input/Output

person.h

#include<cstring>
class person
{
    private:
    char name[8];
    int phone_num;
    public:
        person(char s[]="abc",int n=0){
            strcpy(name,s);
            phone_num=n;
        }
        char *getName(){
            return name;
        }
        int getPhone()
        {
            return phone_num;
        }
};

BinaryFileOutput.cpp

#include<iostream>
#include<fstream>
#include"person.h"
using namespace std;

int main()
{
    ofstream output("test.txt",ios::binary);
    person group[3]={person("Marry",100),person("Tom",101),person("Jack",102)};
    output.write((char*)group,sizeof(group));
    return 0;
}

BinaryFileInput.cpp

#include<iostream>
#include<fstream>
#include"person.h"
using namespace std;

int main()
{
    ifstream input("test.txt",ios::binary);
    person *temp_person=new person[3];
    input.read((char*)temp_person,sizeof(person)*3);
    for(int i=0;i<3;i++)
    {
        cout<<temp_person[i].getName()<<" "<<temp_person[i].getPhone()<<endl;
    } 
    return 0;
}

[C++] Class Template Inheritance

#include<iostream>
#include<cstdlib>
using namespace std;
template<class T>
class foo
{
    public:
    T a,b;
};
template<class T>
class bar:public foo<T>
{
    public:
    void show()
    {
        cout<<"Son";
    }
};
int main()
{
    bar<int> k;
    k.show();
    return 0;
}

[C++] stolower()

#include <algorithm>
#include <string>
using namespace std; string data = "Abc"; 
transform(data.begin(), data.end(), data.begin(), ::tolower);

OR

#include
#include

using namespace std;
string &stolower(string &s)
{
    for(int i=0;i<s.length();i++)
    {
        s[i]=tolower(s[i]);
    }
    return s;
}
int main()
{
    string s="ABC";
    cout<<stolower(s);
    return 0;
}

[C++] Process Input as char

#include<iostream>
#include<string>

using namespace std;

int main()
{
    char a;
    while(cin.get(a)&&a!='\n')
    { 
        cout<<a;
    } 
    return 0;
}