C++

From My wiki
Jump to: navigation, search

About

Currently in version C++17

Basics

Fixed width integer types

Examples

Hello World

//============================================================================
// Name        : Codes.cpp
// Author      : Iago
// Version     :
// Copyright   : 
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int main() {
	cout << "Hello World!!!" << endl; // prints Hello World!!!
	return 0;
}

Class

Header and Body

Friendship and Inheritance

Abstract Class

At least one pure virtual method.

#ifndef SHAPE_H_
#define SHAPE_H_
class Shape
{
  public:
    Shape();
    virtual ~Shape();
    virtual void moveX(int x) = 0;
    virtual void moveY(int y) = 0;
    virtual void update();
};
#endif
#include "shape.h"
Shape::Shape(){

}

Shape::~Shape(){

}

void Shape::update(){

}

Interface

Just Header and pure virtual methods.

#ifndef SHAPE_H_
#define SHAPE_H_
class Shape
{
  public:
    Shape();
    virtual ~Shape();
    virtual void moveX(int x) = 0;
    virtual void moveY(int y) = 0;
    virtual void update() = 0;
};
#endif
Using

Sometimes you overload a method/function of the abstract class...

class classA {
    public:
        void func(char[]){};    
};

class classB : public classA { 
    public:
        void func(int){};
};

int main(){
    int a;
    char b[20];
    classB objB;
    objB.func(a);    //this one is fine
    objB.func(b);    //here's the problem!
    return 0;
}

... and you get an error. That's because the child class found an implementation. Let's "call" the other...

class classB : public classA {
    public:
        using classA::func;
        void func(int){};
};

Code

Links

Official site

Wikipedia