Commit 0f97246e authored by CHAMONT David's avatar CHAMONT David
Browse files

Preparation de la seance 2 et des premiers travaux individuels.

parent cafd7d1a
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-fa19ba3560e6cca6",
"locked": true,
"schema_version": 3,
"solution": false
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Introduction to modern C++\n",
"\n",
"1. [Type inference](en.1-inference.ipynb)\n",
"1. [Uniform initialization](en.2-initialization.ipynb)\n",
"1. [Rvalue references and move semantics](en.3-references.ipynb)\n",
"1. [Standard Library smart pointers](en.4-pointers.ipynb)\n",
"1. [Improvements of object mechanisms](en.5-objects.ipynb)\n",
"1. [Improvements of templates](en.6-templates.ipynb)\n",
"1. [New constant expressions](en.7-constexpr.ipynb)\n",
"1. [Anonymous functions](en.8-lambdas.ipynb)"
]
}
],
"metadata": {
"celltoolbar": "Diaporama",
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
#include <iostream>
constexpr int fibonacci( int n )
{
if (n>1) return fibonacci(n-1) + fibonacci(n-2) ;
else return n ;
}
int main()
{
constexpr int fibo10 {fibonacci(10)} ;
std::cout<<fibo10<<std::endl ;
return 0 ;
}
\ No newline at end of file
#include <vector>
#include <string>
#include <iostream>
class Sentence
{
public :
void add( char const * word )
{ m_words.push_back(word) ; }
auto begin() const { return m_words.begin() ; }
auto end() const { return m_words.end() ; }
private :
std::vector<std::string> m_words ;
} ;
std::ostream & operator<<( std::ostream & os, Sentence const & s )
{
for ( auto const & word : s )
{ os<<word<<" " ; }
return os ;
}
int main()
{
Sentence s ;
s.add("Hello") ;
s.add("world") ;
s.add("!") ;
std::cout<<s<<std::endl ;
}
#include <vector>
#include <string>
#include <initializer_list>
#include <iostream>
class Sentence
{
public :
Sentence( std::initializer_list<std::string> const & words )
{
for ( std::string const & word : words )
{ m_words.push_back(word) ; }
}
auto begin() const { return m_words.begin() ; }
auto end() const { return m_words.end() ; }
private :
std::vector<std::string> m_words ;
} ;
std::ostream & operator<<( std::ostream & os, Sentence const & s )
{
for ( auto const & mot : s )
{ os<<mot<<" " ; }
return os ;
}
int main()
{
Sentence s { "Hello", "world", "!" } ;
std::cout<<s<<std::endl ;
}
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <cmath>
int main()
{
int const DIM {10} ;
int const DEGREE {5} ;
// generate random input
std::vector<double> input(DIM) ;
std::for_each(input.begin(),input.end(),[]( double & value )
{ value = ((2.*std::rand())/RAND_MAX-1.) ; }) ;
// compute output
std::vector<double> output(DIM) ;
std::transform(input.begin(),input.end(),output.begin(),[DEGREE]( double x )
{ return std::pow(x,DEGREE) ; }) ;
// print sum
std::cout<<std::accumulate(output.begin(),output.end(),0.)<<std::endl ;
}
\ No newline at end of file
#include <cstdlib> // pour std::rand()
#include <iostream>
#include <string>
class Particle
{
public :
Particle( double mass ) : m_mass {mass} {}
Particle( Particle const & ) = delete ;
virtual ~Particle() = default ;
double mass() { return m_mass ; }
virtual std::string name() { return "Particle" ; }
private :
double m_mass ;
} ;
class ChargedParticle : public Particle
{
public :
ChargedParticle( double mass, double charge )
: Particle(mass), m_charge {charge} {}
double charge() { return m_charge ; }
std::string name() override { return "ChargedParticle" ; }
private :
double m_charge = 0.0 ;
} ;
void display( Particle & p )
{
std::cout << p.name() << std::endl ;
std::cout << " mass = " << p.mass() << std::endl ;
}
int main()
{
for ( int i = 0 ; i < 5 ; ++i )
{
if ( std::rand() < (.5*RAND_MAX) )
{
Particle p {2} ;
display(p) ;
}
else
{
ChargedParticle p {1,1} ;
display(p) ;
std::cout << " charge = " << p.charge() << std::endl ;
}
}
}
\ No newline at end of file
#include <memory>
#include <iostream>
class MyData
{
public :
MyData( int data ) : m_data {data}
{ std::cout<<"MyData::MyData("<<m_data<<")"<<std::endl ; }
int data() const { return m_data ; }
~MyData() { std::cout<<"MyData::~MyData("<<m_data<<")"<<std::endl ; }
private :
int m_data ;
} ;
void display( std::unique_ptr<MyData> const & data_ptr )
{ std::cout<<data_ptr->data()<<std::endl ; }
int main()
{
auto data_ptr = std::make_unique<MyData>(42) ;
display(data_ptr) ;
return 0 ;
}
#include <cstring>
#include <iostream>
class Text
{
public :
Text( char const * str )
: m_size {std::strlen(str)+1},
m_data {new char [m_size]}
{ std::copy(str,str+m_size,m_data) ; }
Text( Text const & t )
: m_size {t.m_size},
m_data {new char [m_size]}
{
std::cout<<"copy constructor"<<std::endl ;
std::copy(t.m_data,t.m_data+m_size,m_data) ;
}
Text( Text && t )
: m_size {t.m_size},
m_data {t.m_data}
{
std::cout<<"move constructor"<<std::endl ;
t.m_size = 0 ; t.m_data = 0 ;
}
~Text()
{ delete [] m_data ; }
unsigned int size()
{ return m_size ; }
char & operator[]( unsigned int i )
{ return m_data[i] ; }
friend std::ostream & operator<<( std::ostream & os, Text const & t )
{ if (t.m_size) return os<<t.m_data ; else return os ; }
private :
std::size_t m_size ;
char * m_data ;
} ;
Text hello()
{ return "hello" ; }
Text uppercase( Text t )
{
for ( unsigned int i=0 ; i<t.size() ; ++i )
{ t[i] = std::toupper(t[i]) ; }
return t ;
}
int main()
{
std::cout<<uppercase(hello())<<std::endl ;
return 0 ;
}
\ No newline at end of file
#include <memory>
#include <iostream>
class MyData
{
public :
MyData( int i, double d ) : m_i(i), m_d(d)
{ std::cout<<"MyData::MyData()"<<std::endl ; }
int i() { return m_i ; }
double d() { return m_d ; }
~MyData() { std::cout<<"MyData::~MyData()"<<std::endl ; }
private :
int m_i ;
double m_d ;
} ;
void display( std::shared_ptr<MyData> data_ptr )
{ std::cout<<data_ptr->i()<<" "<<data_ptr->d()<<std::endl ; }
template <typename T, typename... Args>
std::shared_ptr<T> make_ptr( Args... args )
{ return std::make_shared<T>(args...) ; }
int main()
{
auto data_ptr {make_ptr<MyData>(42,3.14)} ;
display(data_ptr) ;
return 0 ;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment