Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] web/email now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as C++ by registered user raphael ( 1 year ago )
// Auteur : Raphael De Oliveira
#include <iostream>
#include <list>
#include "test.h"
using namespace std;

int main() {
    test t1;
    test t2(50);
    test t3(60);
    test t4(70);
    test t5(80);

    list<test> liste = {t1, t3, t2, t4, t5};
    liste.push_back(test(90));
    liste.sort();
    cout << "------------Affichage du vector liste avec une boucle for each--------------------------" << endl;
    for(test t : liste) {
        t.toString();
    }

    cout << "------------Affichage du vector liste avec un reverse_iterator--------------------------" << endl;
    for(list<test>::reverse_iterator iter = liste.rbegin(); iter != liste.rend(); ++iter) {
        iter->toString();
    }
    return 0;
}
//--------------------------------------------------------
#ifndef TEST_H
#define TEST_H


class test {
public:
    int val;
    int* pVal;
    int k=2*i;
    test(int x=40);          //Constructeur par transtypage et par defaut
    test(const test &other); //Constructeur par copie
    int increment_static();
    void toString();
    void operator=(const test &source);
    bool operator<(const test &source);
private:
    int i=0;
};

#endif // TEST_H
//---------------------------------------------------------------------------
#include "test.h"
#include <iostream>
using namespace std;

test::test(int x) : val(x), pVal(&val), k(x) {}

void test::toString() {
    cout << "val: " << val << ", *pVal: " << *pVal << ", pVal: " << pVal <<", i: "<<i<< endl;
}

test::test(const test &source){
    this->val = source.val;
    pVal = &val;
}

int test::increment_static(){
    auto s = [this](){this->i++;};
    s();
    return this->i;
}

void test::operator=(const test &source) {
    this->val = source.val;
    this->k = source.k;
    this->i = source.i;
    this->pVal = &this->val;
}

bool test::operator<(const test &source){
    if(source.val<this->val){
        return false;
    }
    return true;
}

 

Revise this Paste

Your Name: Code Language: