Monday, February 24, 2014

some basic cpp syntax

1) simple example:
#include <iostream>
using std::cout;
using std::endl;

class basePoint {
public:
    void setPoint(int, int);
    void display();
private:
    int x;
    int y;
};

void basePoint::setPoint(int x, int y) {
    this->x = x;
    this->y = y;
}

void basePoint::display() {
    std::cout << x << " " << y << std::endl;
}

int main(int argc, char* argv[]) {
    basePoint bp0;
    basePoint *bp1 = new basePoint();

    bp0.setPoint(1, 2);
    bp1->setPoint(3, 4);
    bp0.display();
    bp1->display();
    return 0;
}

2) variable accessibility
#include <iostream>
using std::cout;
using std::endl;

class base {
public:
    base();
    void set_data(int, int, int);
    void clear_data();
    void display();
    int z;

protected:
    int y;

private:
    int x;
};

base::base() {
    x = 0;
    y = 0;
    z = 0;
}

void base::set_data(int x, int y, int z) {
    this->x = x;
    this->y = y;
    this->z = z;
}

void base::clear_data() {
    x = 0;
    y = 0;
    z = 0;
}

void base::display() {
    cout << x << " " << y << " " << z << endl;
}

class public_subclass : public base {
public:
    public_subclass();
    void set_data(int, int, int, int, int, int);
    void clear_data();
    void display();
    int a;

protected:
    int b;

private:
    int c;
};

public_subclass::public_subclass() {
    a = 0;
    b = 0;
    c = 0;
    //x of class base cannot be accessed
    //x = 0;
    y = 0;
    z = 0;
}

void public_subclass::set_data(int a, int b, int c, int x, int y, int z) {
    this->a = a;
    this->b = b;
    this->c = c;
    //x of class base cannot be accessed
    //this->x = x;
    this->y = y;
    this->z = z;
}

void public_subclass::clear_data() {
    a = 0;
    b = 0;
    c = 0;
    //x of class base cannot be accessed
    //x = 0;
    y = 0;
    z = 0;
}

void public_subclass::display() {
    //x of class base cannot be accessed
    //cout << a << " " << b << " " << c << " " << x << " " << y << " " << z << endl;
    cout << a << " " << b << " " << c << " " << y << " " << z << endl;
}

class protected_subclass : protected base {
public:
    protected_subclass();
    void set_data(int, int, int, int, int, int);
    void clear_data();
    void display();
    int a;

protected:
    int b;

private:
    int c;
};

protected_subclass::protected_subclass() {
    a = 0;
    b = 0;
    c = 0;
    //x of class base cannot be accessed
    //x = 0;
    y = 0;
    z = 0;
}

void protected_subclass::set_data(int a, int b, int c, int x, int y, int z) {
    this->a = a;
    this->b = b;
    this->c = c;
    //x of class base cannot be accessed
    //this->x = x;
    this->y = y;
    this->z = z;
}

void protected_subclass::clear_data() {
    a = 0;
    b = 0;
    c = 0;
    //x of class base cannot be accessed
    //x = 0;
    y = 0;
    z = 0;
}

void protected_subclass::display() {
    //x of class base cannot be accessed
    //cout << a << " " << b << " " << c << " " << x << " " << y << " " << z << endl;
    cout << a << " " << b << " " << c << " " << y << " " << z << endl;
}

class private_subclass : private base {
public:
    private_subclass();
    void set_data(int, int, int, int, int, int);
    void clear_data();
    void display();
    int a;

protected:
    int b;

private:
    int c;
};

private_subclass::private_subclass() {
    a = 0;
    b = 0;
    c = 0;
    //x of class base cannot be accessed
    //x = 0;
    y = 0;
    z = 0;
}

void private_subclass::set_data(int a, int b, int c, int x, int y, int z) {
    this->a = a;
    this->b = b;
    this->c = c;
    //x of class base cannot be accessed
    //this->x = x;
    this->y = y;
    this->z = z;
}

void private_subclass::clear_data() {
    a = 0;
    b = 0;
    c = 0;
    //x of class base cannot be accessed
    //x = 0;
    y = 0;
    z = 0;
}

void private_subclass::display() {
    //x of class base cannot be accessed
    //cout << a << " " << b << " " << c << " " << x << " " << y << " " << z << endl;
    cout << a << " " << b << " " << c << " " << y << " " << z << endl;
}

int main(int argc, char* argv[]) {
    base u_base;
    public_subclass u_public_subclass;
    protected_subclass u_protected_subclass;
    private_subclass u_private_subclass;

    u_base.set_data(1, 2, 3);
    u_base.display();

    /////////////////////////////////////////////////
    //x and y of class base cannot be accessed
    /////////////////////////////////////////////////
    //u_base.x = 10;
    //u_base.y = 20;
    u_base.z = 30;
    u_base.display();

    u_base.clear_data();
    u_base.display();

    /////////////////////////////////////////////////
    //public base class
    /////////////////////////////////////////////////
    u_public_subclass.z = 21;
    u_public_subclass.a = 22;
    //b and c cannot be accessed
    //u_public_subclass.b = 23;
    //u_public_subclass.c = 24;
    u_public_subclass.display();

    /////////////////////////////////////////////////
    //protected base class
    /////////////////////////////////////////////////
    //z cannot be accessed
    //u_protected_subclass.z = 31;
    u_protected_subclass.a = 37;
    u_protected_subclass.display();
    u_protected_subclass.set_data(31, 32, 33, 34, 35, 36);
    u_protected_subclass.display();

    /////////////////////////////////////////////////
    //private base class
    /////////////////////////////////////////////////
    //z cannot be accessed
    //u_private_subclass.z = 41;
    u_private_subclass.a = 47;
    u_private_subclass.display();
    u_private_subclass.set_data(41, 42, 43, 44, 45, 46);
    u_private_subclass.display();
}

3) constructor and deconstructor
//deconstructor of base class must be virtual function
#include <iostream>
using std::cout;
using std::endl;

class base {
public:
    base();
    virtual ~base();
};

base::base() {
}

base::~base() {
    cout << "Calling deconstruction of base!" << endl;
}

class subclass : public base {
public:
    subclass();
    ~subclass();
};

subclass::subclass() {
}
subclass::~subclass() {
    cout << "Calling deconstruction of subclass!" << endl;
}

int main(int argc, char* argv[]) {
    base *u_base = new subclass;
    delete u_base;

    return 0;
}

4) dynamically allocate memory
#include <iostream>
//#include <cstddef>
#include <stddef.h>
using std::cout;
using std::endl;
using std::nothrow;
//using std::nullptr;
//using std::nullptr_t;

int main(int argc, char* argv[]) {
    //will throw an error when memory cannot be allocated
    int *var1 = new int[10];
    //will not throw an error when memory cannot be allocated
    int *var2 = new (nothrow) int [10];

    //c++ 11
    //if(var2 == nullptr) {
    if(var2 == 0) {
        cout << "Cannot allocate memory" << endl;
        return 1;
    }

    for(int i=0; i<10; i++) {
        *(var1 + i) = i;
        *(var2 + i) = i + 10;
    }

    for(int i=0; i<10; i++) {
        cout << *(var1 + i) << " " << *(var2 + i) << endl;
    }

    delete [] var1;
    delete [] var2;

    return 0;
}

5) inherent
/////////////////////////////////////////////
//base.h
/////////////////////////////////////////////
#ifndef __BASE_H__
#define __BASE_H__
#include <iostream>
using std::cout;
using std::endl;

class basePoint {
public:
    basePoint();
    basePoint(int, int);
    virtual ~basePoint();
    void setPoint(int, int);
    void display();

private:
    int x;
    int y;
};

#endif

/////////////////////////////////////////////
//base.cpp
/////////////////////////////////////////////
#ifndef __BASE_CPP__
#define __BASE_CPP__
#include "base.h"

basePoint::basePoint() {
    x = 10;
    y = 10;
}

basePoint::basePoint(int x, int y) {
    this->x = x;
    this->y = y;
}

basePoint::~basePoint() {

}

void basePoint::setPoint(int x, int y) {
    this->x = x;
    this->y = y;
}

void basePoint::display() {
    std::cout << x << " " << y << " " << std::endl;
}

#endif

/////////////////////////////////////////////
//polygon.h
/////////////////////////////////////////////
#ifndef __SUBCLASS_H__
#define __SUBCLASS_H__
#include <iostream>
#include "base.h"
using std::cout;
using std::endl;

class polygon {
public:
    polygon();
    polygon(int);
    virtual ~polygon();
    void display();

protected:
    bool genPolygon();

private:
    int pointNo;
    basePoint **bp;
};

#endif

/////////////////////////////////////////////
//polygon.cpp
/////////////////////////////////////////////
#ifndef __SUBCLASS_CPP__
#define __SUBCLASS_CPP__
#include "polygon.h"

polygon::polygon() {
    pointNo = 10;
    genPolygon();
}

polygon::polygon(int number) {
    pointNo = number;
    genPolygon();
}

polygon::~polygon() {
    for(int i=0; i<pointNo; i++) {
        delete bp[i];
    }
    delete [] bp;
}

bool polygon::genPolygon() {
    bp = new basePoint*[pointNo];
    for(int i=0; i<pointNo; i++) {
        bp[i] = new basePoint();
    }

    return true;
}

void polygon::display() {
    for(int i=0; i<pointNo; i++) {
        cout << "Point: " << i << endl;
        bp[i]->display();
    }
}

#endif

/////////////////////////////////////////////
//rectangle.h
/////////////////////////////////////////////
#ifndef __RECTANGLE_H__
#define __RECTANGLE_H__
#include <iostream>
#include "polygon.h"
using std::cout;
using std::endl;

class rectangle : public polygon {
public:
    rectangle();
    virtual ~rectangle();

};

#endif

/////////////////////////////////////////////
//rectangle.cpp
/////////////////////////////////////////////
#ifndef __RECTANGLE_CPP__
#define __RECTANGLE_CPP__
#include "rectangle.h"

rectangle::rectangle() : polygon(4) {
}

rectangle::~rectangle() {
}

#endif

/////////////////////////////////////////////
//main.cpp
/////////////////////////////////////////////
#include "rectangle.h"

int main(int argc, char* argv[]) {
    polygon poly;
    poly.display();

    rectangle *rect = new rectangle();
    rect->display();

    delete rect;
    return 0;
}

#############################################
#Makefile
#############################################

.PHONY: clean all

all: clean a.out

clean:
rm -rf *.o *.out *.exe

a.out: base.o main.o  polygon.o rectangle.o
g++ -o $@ $^

6) polymorphism
/////////////////////////////////////////////
//polygon.h
/////////////////////////////////////////////
#ifndef __POLYGON_H__
#define __POLYGON_H__
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;

class polygon {
public:
    polygon(int, int);
    virtual ~polygon();
    void display();
#ifdef USE_VIRTUAL
    virtual float area();
#else
    float area();
#endif

    int width;
    int height;
};

#endif

/////////////////////////////////////////////
//polygon.cpp
/////////////////////////////////////////////
#ifndef __POLYGON_CPP__
#define __POLYGON_CPP__
#include "polygon.h"

polygon::polygon(int width, int height) {
    this->width = width;
    this->height = height;
}

polygon::~polygon() {
}

float polygon::area() {
    return 0;
}

void polygon::display() {
    cout << "width: " << width << " height: " << height << endl;
}

#endif

/////////////////////////////////////////////
//rectangle.h
/////////////////////////////////////////////
#ifndef __RECTANGLE_H__
#define __RECTANGLE_H__
#include <iostream>
#include "polygon.h"
using std::cout;
using std::endl;

class rectangle : public polygon {
public:
    rectangle(int, int);
    virtual ~rectangle();

#ifdef USE_VIRTUAL
    virtual float area();
#else
    float area();
#endif

};

#endif

/////////////////////////////////////////////
//rectangle.cpp
/////////////////////////////////////////////
#ifndef __RECTANGLE_CPP__
#define __RECTANGLE_CPP__
#include "rectangle.h"

rectangle::rectangle(int width, int height) : polygon(width, height) {
}

rectangle::~rectangle() {
}

float rectangle::area() {
    return width * height;
}

#endif

/////////////////////////////////////////////
//triangle.h
/////////////////////////////////////////////
#ifndef __TRIANGLE_H__
#define __TRIANGLE_H__
#include <iostream>
#include "polygon.h"
using std::cout;
using std::endl;

class triangle : public polygon {
public:
    triangle(int, int);
    virtual ~triangle();

#ifdef USE_VIRTUAL
    virtual float area();
#else
    float area();
#endif

};

#endif

/////////////////////////////////////////////
//triangle.cpp
/////////////////////////////////////////////
#ifndef __TRIANGLE_CPP__
#define __TRIANGLE_CPP__
#include "triangle.h"

triangle::triangle(int width, int height) : polygon(width, height) {
}

triangle::~triangle() {
}

float triangle::area() {
    return float(width) * float(height) / 2;
}

#endif

/////////////////////////////////////////////
//main.cpp
/////////////////////////////////////////////
#include "polygon.h"
#include "rectangle.h"
#include "triangle.h"

int main(int argc, char* argv[]) {
    rectangle *rect = new rectangle(2, 3);
    polygon *poly1 = new rectangle(2, 3);
    cout << "Area of rectangle by calling the function area of rectangle: " << rect->area() << endl;
    //if area is not virtual function, the return data will be 0
    cout << "Area of rectangle by calling the function area of polygon: " << poly1->area() << endl;

    triangle *tri = new triangle(2, 3);
    polygon *poly2 = new triangle(2, 3);
    cout << "Area of triangle by calling the function area of triangle: " << tri->area() << endl;
    //if area is not virtual function, the return data will be 0
    cout << "Area of triangle by calling the function area of polygon: " << poly2->area() << endl;

    delete rect;
    delete tri;
    return 0;
}

#############################################
#Makefile
#############################################

.PHONY: clean all

all: clean a.out

ifeq ($(USE_VIRTUAL),1)
CPPFLAGS = -DUSE_VIRTUAL
endif

clean:
rm -rf *.o *.out *.exe

a.out: main.o  polygon.o rectangle.o triangle.o
g++ -o $@ $^

7) override
/////////////////////////////////////////////
//polygon.h
/////////////////////////////////////////////
#ifndef __POLYGON_H__
#define __POLYGON_H__
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;

class polygon {
public:
    polygon(int, int);
    virtual ~polygon();
    void display();
#ifdef USE_VIRTUAL
    virtual float area();
#else
    float area();
#endif

    int width;
    int height;
};

#endif

/////////////////////////////////////////////
//polygon.cpp
/////////////////////////////////////////////
#ifndef __POLYGON_CPP__
#define __POLYGON_CPP__
#include "polygon.h"

polygon::polygon(int width, int height) {
    this->width = width;
    this->height = height;
}

polygon::~polygon() {
}

float polygon::area() {
    return 0;
}

void polygon::display() {
    cout << "width: " << width << " height: " << height << endl;
}

#endif

/////////////////////////////////////////////
//rectangle.h
/////////////////////////////////////////////
#ifndef __RECTANGLE_H__
#define __RECTANGLE_H__
#include <iostream>
#include "polygon.h"
#include "triangle.h"
using std::cout;
using std::endl;

class triangle;

class rectangle : public polygon {
public:
    rectangle(int, int);
    virtual ~rectangle();

#ifdef USE_VIRTUAL
    virtual float area();
#else
    float area();
#endif

    float operator+(rectangle&);
#ifdef RECT_PLUS_TRI
    float operator+(triangle&);
#endif

};

#endif

/////////////////////////////////////////////
//rectangle.cpp
/////////////////////////////////////////////
#ifndef __RECTANGLE_CPP__
#define __RECTANGLE_CPP__
#include "rectangle.h"

rectangle::rectangle(int width, int height) : polygon(width, height) {
}

rectangle::~rectangle() {
}

float rectangle::area() {
    return width * height;
}

float rectangle::operator+(rectangle& rect) {
    return this->area() + rect.area();
}

#ifdef RECT_PLUS_TRI
float rectangle::operator+(triangle& tri) {
    return this->area() + tri.area();
}
#endif

#endif

/////////////////////////////////////////////
//triangle.h
/////////////////////////////////////////////
#ifndef __TRIANGLE_H__
#define __TRIANGLE_H__
#include <iostream>
#include "polygon.h"
#include "rectangle.h"
using std::cout;
using std::endl;

class rectangle;

class triangle : public polygon {
public:
    triangle(int, int);
    virtual ~triangle();

#ifdef USE_VIRTUAL
    virtual float area();
#else
    float area();
#endif

    float operator+(triangle&);
#ifdef TRI_PLUS_RECT
    float operator+(rectangle&);
#endif

};

#endif

/////////////////////////////////////////////
//triangle.cpp
/////////////////////////////////////////////
#ifndef __TRIANGLE_CPP__
#define __TRIANGLE_CPP__
#include "triangle.h"

triangle::triangle(int width, int height) : polygon(width, height) {
}

triangle::~triangle() {
}

float triangle::area() {
    return float(width) * float(height) / 2;
}

float triangle::operator+(triangle& tri) {
    return this->area() + tri.area();
}

#ifdef TRI_PLUS_RECT
float triangle::operator+(rectangle& rect) {
    return this->area() + rect.area();
}
#endif

#endif

/////////////////////////////////////////////
//main.cpp
/////////////////////////////////////////////
#include "polygon.h"
#include "rectangle.h"
#include "triangle.h"

int main(int argc, char* argv[]) {
    rectangle *rect1 = new rectangle(2, 2);
    cout << "Area of rect1angle by calling the function area of rect1angle: " << rect1->area() << endl;

    rectangle *rect2 = new rectangle(3, 3);
    cout << "Area of rectangle by calling the function area of rectangle: " << rect2->area() << endl;

    cout << "Area of rect1 plus rect2 is: " << *rect1 + *rect2 << endl;

    triangle *tri1 = new triangle(2, 2);
    cout << "Area of triangle by calling the function area of triangle: " << tri1->area() << endl;

    triangle *tri2 = new triangle(3, 3);
    cout << "Area of triangle by calling the function area of triangle: " << tri2->area() << endl;

    cout << "Area of tri1 plus tri2 is: " << *tri1 + *tri2 << endl;

#ifdef RECT_PLUS_TRI
    cout << "Area of rect1 plus tri1 is: " << *rect1 + *tri1 << endl;
#endif

#ifdef TRI_PLUS_RECT
    cout << "Area of tri1 plus rect1 is: " << *tri1 + *rect1 << endl;
#endif

    delete rect1;
    delete rect2;
    delete tri1;
    delete tri2;
    return 0;
}

#############################################
#Makefile
#############################################
.PHONY: clean all

all: clean a.out

CPPFLAGS = -DUSE_VIRTUAL

ifeq (${RECT_PLUS_TRI}, 1)
CPPFLAGS += -DRECT_PLUS_TRI
endif

ifeq (${TRI_PLUS_RECT}, 1)
CPPFLAGS += -DTRI_PLUS_RECT
endif

clean:
rm -rf *.o *.out *.exe

a.out: main.o  polygon.o rectangle.o triangle.o
g++ -o $@ $^

8) smart pointer
/////////////////////////////////////////////
//hasPtr.h
/////////////////////////////////////////////
#ifndef __HASPTR_H__
#define __HASPTR_H__

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

class HasPtr {
friend void swap(HasPtr&, HasPtr&);

public:
    HasPtr(const string &s = string()):
        ps(new string(s)), i(0), use(new std::size_t(1)) {}
    HasPtr(const HasPtr &);
    HasPtr& operator=(const HasPtr&);
    ~HasPtr();
    void display() {
        cout << "ps: " << *ps << " use: " << *use << endl;
    }
private:
    std::string *ps;
    int i;
    std::size_t *use;
};

#endif

/////////////////////////////////////////////
//hasPtr.cpp
/////////////////////////////////////////////
#ifndef __HASPTR_CPP__
#define __HASPTR_CPP__

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

class HasPtr {
friend void swap(HasPtr&, HasPtr&);

public:
    HasPtr(const string &s = string()):
        ps(new string(s)), i(0), use(new std::size_t(1)) {}
    HasPtr(const HasPtr &);
    HasPtr& operator=(const HasPtr&);
    ~HasPtr();
    void display() {
        cout << "ps: " << *ps << " use: " << *use << endl;
    }
private:
    std::string *ps;
    int i;
    std::size_t *use;
};

HasPtr::HasPtr(const HasPtr &p): ps(p.ps), i(p.i), use(p.use) {
    ++*use;
}

HasPtr::~HasPtr() {
    if (--*use == 0) {
        delete ps;
        delete use;
    }
}

HasPtr& HasPtr::operator=(const HasPtr &rhs) {
    ++*rhs.use;
    if(--*use == 0) {
        delete ps;
        delete use;
    }
    ps = rhs.ps;
    i = rhs.i;
    use = rhs.use;
    return *this;
}

#endif

/////////////////////////////////////////////
//main.cpp
/////////////////////////////////////////////
#include "hasPtr.h"

int main(int argc, char* argv[]) {
    HasPtr *hasPtr1 = new HasPtr("string 1");
    HasPtr *hasPtr2 = new HasPtr("string 2");
    hasPtr1->display();
    hasPtr2->display();

    *hasPtr2 = *hasPtr1;
    hasPtr1->display();
    hasPtr2->display();

    delete hasPtr1;
    hasPtr2->display();

    delete hasPtr2;

    return 0;
}

#############################################
#Makefile
#############################################
.PHONY: clean

all: clean a.out

clean:
rm -rf *.o *.exe *.out

a.out: main.o hasPtr.o swap.o
g++ -o $@ $^

9) friend function
/////////////////////////////////////////////
//hasPtr.h
/////////////////////////////////////////////
#ifndef __HASPTR_H__
#define __HASPTR_H__

#include <string>
#include <iostream>
#include "swap.h"
using std::string;
using std::cout;
using std::endl;

class HasPtr {
friend void swap(HasPtr&, HasPtr&);

public:
    HasPtr(const string &s = string()):
        ps(new string(s)), i(0), use(new std::size_t(1)) {}
    HasPtr(const HasPtr &);
    HasPtr& operator=(const HasPtr&);
    ~HasPtr();
    void display() {
        cout << "ps: " << *ps << " use: " << *use << endl;
    }
private:
    std::string *ps;
    int i;
    std::size_t *use;
};

#endif

/////////////////////////////////////////////
//hasPtr.cpp
/////////////////////////////////////////////
#ifndef __HASPTR_CPP__
#define __HASPTR_CPP__

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

class HasPtr {
friend void swap(HasPtr&, HasPtr&);

public:
    HasPtr(const string &s = string()):
        ps(new string(s)), i(0), use(new std::size_t(1)) {}
    HasPtr(const HasPtr &);
    HasPtr& operator=(const HasPtr&);
    ~HasPtr();
    void display() {
        cout << "ps: " << *ps << " use: " << *use << endl;
    }
private:
    std::string *ps;
    int i;
    std::size_t *use;
};

HasPtr::HasPtr(const HasPtr &p): ps(p.ps), i(p.i), use(p.use) {
    ++*use;
}

HasPtr::~HasPtr() {
    if (--*use == 0) {
        delete ps;
        delete use;
    }
}

HasPtr& HasPtr::operator=(const HasPtr &rhs) {
    ++*rhs.use;
    if(--*use == 0) {
        delete ps;
        delete use;
    }
    ps = rhs.ps;
    i = rhs.i;
    use = rhs.use;
    return *this;
}

#endif

/////////////////////////////////////////////
//swap.h
/////////////////////////////////////////////
#ifndef __SWAP_H__
#define __SWAP_H__

#include "hasPtr.h"

class HasPtr;

void swap(HasPtr&, HasPtr&);

#endif

/////////////////////////////////////////////
//swap.cpp
/////////////////////////////////////////////
#ifndef __SWAP_CPP__
#define __SWAP_CPP__

#include "swap.h"

void swap(HasPtr &lhs, HasPtr &rhs) {
    using std::swap;
    swap(lhs.ps, rhs.ps);
    swap(lhs.i, rhs.i);
}

#endif

/////////////////////////////////////////////
//main.cpp
/////////////////////////////////////////////
#include <iostream>
#include "hasPtr.h"
#include "swap.h"
using std::cout;
using std::endl;

int main(int argc, char* argv[]) {
    HasPtr *hasPtr1 = new HasPtr("string 1");
    HasPtr *hasPtr2 = new HasPtr("string 2");
    cout << "hasPtr1:" << endl;
    hasPtr1->display();
    cout << "hasPtr2:" << endl;
    hasPtr2->display();

    swap(*hasPtr1, *hasPtr2);

    cout << "hasPtr1:" << endl;
    hasPtr1->display();
    cout << "hasPtr2:" << endl;
    hasPtr2->display();

    delete hasPtr1;
    delete hasPtr2;

    return 0;
}

#############################################
#Makefile
#############################################
.PHONY: clean

all: clean a.out

clean:
rm -rf *.o *.exe *.out

a.out: main.o hasPtr.o swap.o
g++ -o $@ $^

10) friend class
/////////////////////////////////////////////
//hasPtr.h
/////////////////////////////////////////////
#ifndef __HASPTR_H__
#define __HASPTR_H__

#include <string>
#include <iostream>
#include "ptrOp.h"
using std::string;
using std::cout;
using std::endl;

class HasPtr {
friend class ptrOp;

public:
    HasPtr(const string &s = string()):
        ps(new string(s)), i(0), use(new std::size_t(1)) {}
    HasPtr(const HasPtr &);
    HasPtr& operator=(const HasPtr&);
    ~HasPtr();
    void display() {
        cout << "ps: " << *ps << " use: " << *use << endl;
    }
private:
    std::string *ps;
    int i;
    std::size_t *use;
};

#endif

/////////////////////////////////////////////
//hasPtr.cpp
/////////////////////////////////////////////
#ifndef __HASPTR_CPP__
#define __HASPTR_CPP__

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

class HasPtr {
friend class ptrOp;

public:
    HasPtr(const string &s = string()):
        ps(new string(s)), i(0), use(new std::size_t(1)) {}
    HasPtr(const HasPtr &);
    HasPtr& operator=(const HasPtr&);
    ~HasPtr();
    void display() {
        cout << "ps: " << *ps << " use: " << *use << endl;
    }
private:
    std::string *ps;
    int i;
    std::size_t *use;
};

HasPtr::HasPtr(const HasPtr &p): ps(p.ps), i(p.i), use(p.use) {
    ++*use;
}

HasPtr::~HasPtr() {
    if (--*use == 0) {
        delete ps;
        delete use;
    }
}

HasPtr& HasPtr::operator=(const HasPtr &rhs) {
    ++*rhs.use;
    if(--*use == 0) {
        delete ps;
        delete use;
    }
    ps = rhs.ps;
    i = rhs.i;
    use = rhs.use;
    return *this;
}

#endif

/////////////////////////////////////////////
//ptrOp.h
/////////////////////////////////////////////
#ifndef __SWAP_H__
#define __SWAP_H__

#include "hasPtr.h"

class HasPtr;

class ptrOp {
public:
    ptrOp(HasPtr&, HasPtr&);
    void swap(HasPtr&, HasPtr&);
private:
    HasPtr *hasPtr1;
    HasPtr *hasPtr2;
};

#endif

/////////////////////////////////////////////
//ptrOp.cpp
/////////////////////////////////////////////
#ifndef __PTROP_CPP__
#define __PTROP_CPP__

#include "ptrOp.h"

ptrOp::ptrOp(HasPtr& hasPtr1, HasPtr& hasPtr2) {
    this->hasPtr1 = &hasPtr1;
    this->hasPtr2 = &hasPtr2;
}

void ptrOp::swap(HasPtr &lhs, HasPtr &rhs) {
    using std::swap;
    swap(lhs.ps, rhs.ps);
    swap(lhs.i, rhs.i);
    swap(hasPtr1->ps, hasPtr2->ps);
    swap(hasPtr1->i, hasPtr2->i);
}

#endif

/////////////////////////////////////////////
//main.cpp
/////////////////////////////////////////////
#include <iostream>
#include "hasPtr.h"
#include "ptrOp.h"
using std::cout;
using std::endl;

int main(int argc, char* argv[]) {
    HasPtr *hasPtr1 = new HasPtr("string 1");
    HasPtr *hasPtr2 = new HasPtr("string 2");
    HasPtr *hasPtr3 = new HasPtr("string 3");
    HasPtr *hasPtr4 = new HasPtr("string 4");
    cout << "hasPtr1:" << endl;
    hasPtr1->display();
    cout << "hasPtr2:" << endl;
    hasPtr2->display();
    cout << "hasPtr3:" << endl;
    hasPtr3->display();
    cout << "hasPtr4:" << endl;
    hasPtr4->display();

    ptrOp *ptrOp1 = new ptrOp(*hasPtr1, *hasPtr2);
    ptrOp1->swap(*hasPtr3, *hasPtr4);

    cout << "hasPtr1:" << endl;
    hasPtr1->display();
    cout << "hasPtr2:" << endl;
    hasPtr2->display();
    cout << "hasPtr3:" << endl;
    hasPtr3->display();
    cout << "hasPtr4:" << endl;
    hasPtr4->display();

    delete hasPtr1;
    delete hasPtr2;
    delete hasPtr3;
    delete hasPtr4;

    return 0;
}

#############################################
#Makefile
#############################################
.PHONY: clean

all: clean a.out

clean:
rm -rf *.o *.exe *.out

a.out: main.o hasPtr.o ptrOp.o
g++ -o $@ $^

11) inline
/////////////////////////////////////////////
//rectangle.h
/////////////////////////////////////////////
#ifndef __RECTANGLE_H__
#define __RECTANGLE_H__
#include <iostream>
using std::cout;
using std::endl;

class rectangle {
public:
    rectangle(int, int);
    virtual ~rectangle();

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

private:
    int width;
    int height;
};

#endif

/////////////////////////////////////////////
//rectangle.cpp
/////////////////////////////////////////////
#ifndef __RECTANGLE_CPP__
#define __RECTANGLE_CPP__
#include "rectangle.h"

rectangle::rectangle(int width, int height) {
    this->width = width;
    this->height = height;
}

rectangle::~rectangle() {
}

#endif

/////////////////////////////////////////////
//main.cpp
/////////////////////////////////////////////
#include <iostream>
#include "rectangle.h"
using std::cout;
using std::endl;

int main(int argc, char* argv[]) {

    rectangle *rect = new rectangle(2, 3);
    cout << rect->area() << endl;

    delete rect;
    return 0;
}

#############################################
#Makefile
#############################################
.PHONY: clean

all: clean a.out

clean:
rm -rf *.o *.exe *.out

a.out: main.o rectangle.o
g++ -o $@ $^

21) vector
/////////////////////////////////////////////
//main.cpp
/////////////////////////////////////////////
#include <vector>
#include <iostream>
#include <string>
using std::vector;
using std::cout;
using std::endl;
using std::string;

int main(int argc, char* argv[]) {
    vector<int> a;
    if(a.empty()) {
        for (int i = 0; i != 3; ++i) {
            a.push_back(i+10);
        }
    }

    cout << "a size: " << a.size() << " max size: " << a.max_size() << endl;

    vector<int> b(a);
    cout << "b size: " << b.size() << " max size: " << b.max_size() << endl;
    cout << "b capacity: " << b.capacity() << endl;
    b.resize(10);
    b.reserve(100);
    cout << "b size: " << b.size() << " max size: " << b.max_size() << endl;
    cout << "b capacity: " << b.capacity() << endl;

    vector<int> c = a;
    vector<int> d(3);
    cout << d[0] << endl;
    while(d != a) {
        d = a;
        cout << d[0] << endl;
    }
    vector<int> e(3, 2);
    cout << e[0] << endl;

    //supported in C++11
    vector<int> f{1, 2, 3};
    vector<int> g = {4, 5, 6};

    vector<int> h{1,2,3};
    for (auto &i : h) {
        i *= i;
    }
    for (auto i : h) {
        cout << i << " ";
    }
    cout << endl;

    //cout << decltype(h.size()) << endl;
    for (auto it = h.cbegin(); it != h.cend(); ++it) {
        if(*it == 1) {
            //illegal
            //*it = 1;
        }
        cout << *it << " ";
    }
    cout << endl;
    for (auto it = h.crbegin(); it != h.crend(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    for (vector<int>::iterator it = a.begin(); it != a.end(); ++it) {
        if(*it == 10) {
            *it = 0;
        }
    }
    for (vector<int>::iterator it = a.begin(); it != a.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    for (vector<int>::reverse_iterator it = a.rbegin(); it != a.rend(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    for (vector<int>::const_iterator it = a.begin(); it != a.end(); ++it) {
        if(*it == 11) {
            //is not legal
            //*it = 1;
        }
    }
    for (vector<int>::const_iterator it = a.begin(); it != a.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    int i = 0;
    for (vector<int>::iterator it = b.begin(); it != b.end(); ++it) {
        *it = i;
        i++;
    }
    for (auto it = b.cbegin(); it != b.cend(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    b.pop_back();
    for (auto it = b.cbegin(); it != b.cend(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    b.insert(b.begin() + 1, 10);
    for (auto it = b.cbegin(); it != b.cend(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    b.erase(b.begin() + 3);
    for (auto it = b.cbegin(); it != b.cend(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    b.swap(c);
    for (auto it = b.cbegin(); it != b.cend(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    b.clear();
    cout << "b size: " << b.size() << " max size: " << b.max_size() << endl;

    return 0;
}

#############################################
#Makefile
#############################################
.PHONY: clean

CPPFLAGS = -std=c++0x

all: clean a.out

clean:
rm -rf *.o *.exe *.out

a.out: main.o
g++ -o $@ $^

No comments:

Post a Comment