sábado, 12 de março de 2016

Template Specialization

// template specialization
#include <iostream>
using namespace std;

// class template:
template <class T>
class mycontainer {
    T element;
  public:
    mycontainer (T arg) {element=arg;}
    T increase () {return ++element;}
};

// class template specialization:
template <>
class mycontainer <char> {
    char element;
  public:
    mycontainer (char arg) {element=arg;}
    char uppercase ()
    {
      if ((element>='a')&&(element<='z'))
      element+='A'-'a';
      return element;
    }
};

int main () {
  mycontainer<int> myint (7);
  mycontainer<char> mychar ('j');
  cout << myint.increase() << endl;
  cout << mychar.uppercase() << endl;
  return 0;
}

class templates

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}

sexta-feira, 11 de março de 2016

Const member functions

// Example 1 - constructor on const object

#include <iostream>

class Object{
    int num;
public:
    Object():num(0){};
    void moo() { std::cout << num++ <<std::endl;}
    void say() const {std::cout << "Hello"<<std::endl;}         
};

int main()
{
    const Object a;
    //a.moo();                                              //can not be modified.
    //a.moo();                                              //can not be modified.
    a.say();
    return 0;
}


// Example 2 - constructor on const object
#include <iostream>
using namespace std;

class MyClass {
  public:
    int x;
    MyClass(int val) : x(val) {}
    int get() {return x;}
};

int main() {
  const MyClass foo(10);
// foo.x = 20;            // not valid: x cannot be modified
  cout << foo.x << '\n';  // ok: data member x can be read
  return 0;
}

Overloading operators

// overloading operators example
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int a,int b) : x(a), y(b) {}
    CVector operator + (const CVector&);
    CVector operator - (const CVector&);
}; CVector CVector::operator+ (const CVector& param) { //operator+ CVector temp; temp.x = x + param.x; temp.y = y + param.y; return temp; }
CVector CVector::operator- (const CVector& param) {           //operator-
  CVector temp;
  temp.x = x - param.x;
  temp.y = y - param.y;
  return temp;
}
int main () { CVector foo (3,1); CVector bar (1,2); CVector result; result = foo + bar; cout << result.x << ',' << result.y << '\n'; return 0; }

Overloading constructors

// overloading class constructors
#include <iostream>
using namespace std;

class Rectangle {
    long double width, height;
  public:
    Rectangle ();
    Rectangle (int,int);

    int area (void) {return (width*height);}
};

Rectangle::Rectangle () {
  width = 5;
  height = 5;
}

Rectangle::Rectangle (int a, int b) {
  width = a;
  height = b;
}


int main () {
  Rectangle rect (3,4);
  Rectangle rectb;
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}

quinta-feira, 10 de março de 2016

This Pointer

// example 1 on this
#include <iostream>
using std::cout;
using std::endl;

class Carro{
public:
    int MaxHeart;                                           // this->MaxHeart
    Carro(int MaxHeart)
    {
        this->MaxHeart = MaxHeart;              //Object of Carro MaxHeart = argument of construtor
    }
    void Imprimir();
};
void Carro::Imprimir()
{
    cout << MaxHeart <<endl;
}

int main () {
    Carro ca(5);
    ca.Imprimir();                                             // Will Print 5
  return 0;
}


#include <iostream>
using std::cout;
using std::endl;

class Carro{
public:
    void foo() const {
        cout << this << endl;
    }
};

int main () {
    Carro ca;
    cout << &ca<<endl;          //same address of object ca
    ca.foo();                   //same address of object ca

    Carro* rat = new Carro();
    cout << rat <<endl;         //same address of object rat
    rat->foo();                 //same address of object rat
  return 0;
  delete rat;
}







Static members and Normal menbers

#include <iostream>
using namespace std;

class Soma{
public:
    Soma(){n=0;}

    static int Num_Static(){           //Static Member
        num++;
        return num;
    }
    int Num_Normal(){                //Normal Member
        n++;
        return n;
    }
private:
    static int num;
    int n;
};
int Soma::num = 0;
int main(){
    Soma a;
    cout << "Static "<<  a.Num_Static() << " and not static "<< a.Num_Normal()<< endl;
    Soma* b = new Soma();
    cout << "Static "<<  b->Num_Static()<<  " and not static "<<  b->Num_Normal()<< endl;
    Soma* c = new Soma();
    cout << "Static "<< c->Num_Static()<< " and not static "<< c->Num_Normal()<< endl;
}