02. C++ Overview

Posted by Yulai Blog on November 2, 2015

C++ overview

Designed by [Bjarne Stroustrup], a Danish computer scientist, starting in 1979 at Bell Labs.

  1. is a better C, is a direct descendant of C89

  2. supports data abstraction (C with Classes)
    • adds keyword class, also keeps struct for compatible
    • A union is a class defined with the class-key union; it holds at most one data member at a time
    • supports access control (compare with struct)
    • Members of a class defined with the keyword class are private by default.
    • Members of a class defined with the keywords struct or union are public by default. * constructors and destructors * initialization * Rule of three
  3. supports Object-oriented programming without extensions
    • to be continue…
  4. supports function overloading
#include <iostream>

int sum(int a, int b)
{
    std::cout << "calc integer sum: " << a << " + " << b << " = ";
    return a + b;
}

double sum(double a, double b)
{
    std::cout << "calc float sum: " << a << " + " << b << " = ";
    return a + b;
}

void sum_test()
{
    std::cout << sum(1, 2) << std::endl;
    std::cout << sum(1.1, 2.2) << std::endl;
}

calc integer sum: 1 + 2 = 3 calc float sum: 1.1 + 2.2 = 3.3

  1. supports generic programming through the use of templates
#include <iostream>
#include <typeinfo>

template<typename T> T sum(T a, T b)
{
    std::cout << "Template calc " << typeid(T).name() << " sum: " << a << " + " << b << " = ";
    return a + b;
}

void template_sum_test()
{
    std::cout << sum<int>(1, 2) << std::endl;
    std::cout << sum<double>(1.1, 2.2) << std::endl;
    std::cout << sum<int>(1.2, 2.2) << std::endl;
}

Template calc int sum: 1 + 2 = 3 Template calc double sum: 1.1 + 2.2 = 3.3 Template calc int sum: 1 + 2 = 3

  1. supports overloading of operators
    • Overloaded “&&” and “   ” operators lose their short-circuit evaluation property.
    #include <iostream>
    
    void foo()
    {
        bar();
    }
    
  1. extends the C89 standard library with C++ standard library
  • For compatibility with the C standard library, provides the 26 C headers:

  • C++ style head file, e.g.: #include <cstdio> instead of #include <stdio.h>

    • #include <cstdio> imports the symbol names in std:: namespace and possibly in Global namespace.
    • #include <stdio.h> imports the symbol names in Global namespace and possibly in std:: namespace.

Sample for C++ style header:

#include <cstdio>

void include_cstdio()
{
    std::printf("Hello, world!\n");
    printf("Hello, again!\n");
}
  1. explicit cast operator (static_cast, dynamic_cast, const_cast and reinterpret_cast)
    • keyword bool, comparison operators returns bool, while C returns int.
    • sizeof
  2. introduce reference, also keeps the pointer
    • similar to a pointer
    • NO need to use a prefix * to access the value
    • CANNOT be made to refer to a different object after its initialization
    • CANNOT be NULL
#include <iostream>

void point_swap(int* a, int* b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void ref_swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

void swap_test()
{
    int x = 10;
    int y = 20;
    int* px = &x;
    int* py = &y;
    int& rx = x;
    int& ry = y;
    
    std::cout << "x = " << x << " y = " << y << std::endl;
    
    std::cout << "pointer swap:" << std::endl;
    point_swap(&x, &y);
    std::cout << "x = " << x << " y = " << y << std::endl;
    point_swap(px, py);
    std::cout << "x = " << x << " y = " << y << std::endl;
    
    std::cout << "reference swap:" << std::endl;
    ref_swap(x, y);
    std::cout << "x = " << x << " y = " << y << std::endl;
    ref_swap(rx, ry);
    std::cout << "x = " << x << " y = " << y << std::endl;
}

x = 10 y = 20 pointer swap: x = 20 y = 10 x = 10 y = 20 reference swap: x = 20 y = 10 x = 10 y = 20

  1. introduce new keyword namespace
int x; // global variable

namespace first {
    int first1;
    int x;
    int y;
    int z;
    
    namespace inside_first {
        int i_first;
        int x;
        int y;
        int z;
    }
}

namespace second {
    int second1;
    int x;
    int y;
}

namespace first {
    int first2;
}

// namespace alias    
namespace ff = first::inside_first;

int main() 
{
    x = 1; // global x
    first::x = 1;
    second::x = 1;
    first::inside_first::x = 1;
    ff::x = 1;
    
    {
        using namespace first;
//		x = 1; // error, ambiguous symbol with global variable 'x'
        y = 1; // first::y
        z = 1; // first::z
        first1 = 1; // first::first1
        first::x = 1;
        second::x = 1;
    
        using namespace second;
//		x = 1; // error, ambiguous symbol with global variable 'x'
//		y = 1; // error, ambiguous symbol with first::y
        z = 1; // first::z
        first::x = 1;
        second::x = 1;
        first1 = 1; // first::first1
        second1 = 1; // second::second1
    
        using namespace first::inside_first;
//		x = 1; // error, ambiguous symbol with global variable 'x'
//		y = 1; // error, ambiguous symbol with first::y
//		z = 1; // error, ambiguous symbol with first::z
        i_first = 1;  //first::inside_first::i_first
        first::x = 1;
        second::x = 1;
        first::inside_first::x = 1;
        ff::x = 1;
    }

    x = 2; // global x
    {
        using namespace first;
        y = 1; // first::y
    }
    {
        using namespace second;
        y = 1; // second::y
    }
    
    return 0;
}