INF3105 — Structure de données et algorithmes
Automne 2016
#include <iostream>
using namespace std;
int main(){
int a=1, b=2, c=3, d=4;
int* pa=&a;
int* pb=&b;
int* pc=&c, *pd=&d;
cout << "a=" << a << "\tb=" << b << "\tc=" << c << "\td=" << d << endl;
cout << "pa=" << pa << "\tpb=" << pb << "\tpc=" << pc << "\tpd=" << pd << endl;
*pa = 4;
cout << "a=" << a << "\tb=" << b << "\tc=" << c << "\td=" << d << endl;
cout << "pa=" << pa << "\tpb=" << pb << "\tpc=" << pc << "\tpd=" << pd << endl;
pa = pb;
*pa = 8;
cout << "a=" << a << "\tb=" << b << "\tc=" << c << "\td=" << d << endl;
cout << "pa=" << pa << "\tpb=" << pb << "\tpc=" << pc << "\tpd=" << pd << endl;
c = 10;
d += *pd;
cout << "a=" << a << "\tb=" << b << "\tc=" << c << "\td=" << d << endl;
cout << "pa=" << pa << "\tpb=" << pb << "\tpc=" << pc << "\tpd=" << pd << endl;
return 0;
}
g++ lab2ex1.cpp -o lab2ex1 ./lab2ex1
#include <iostream>
using namespace std;
int main(){
int a=1, b=2, c=3, d=4;
int& ra=a;
int& rb=&b;
int* pc=&c, *pd=&d;
cout << "a=" << a << "\tb=" << b << "\tc=" << c << "\td=" << d << endl;
cout << "ra=" << ra << "\trb=" << rb << "\tpc=" << pc << "\tpd=" << pd << endl;
ra = 4;
cout << "a=" << a << "\tb=" << b << "\tc=" << c << "\td=" << d << endl;
cout << "ra=" << ra << "\trb=" << rb << "\tpc=" << pc << "\tpd=" << pd << endl;
ra = rb;
cout << "a=" << a << "\tb=" << b << "\tc=" << c << "\td=" << d << endl;
cout << "ra=" << ra << "\trb=" << rb << "\tpc=" << pc << "\tpd=" << pd << endl;
c = 10;
d += *pd;
cout << "a=" << a << "\tb=" << b << "\tc=" << c << "\td=" << d << endl;
cout << "ra=" << ra << "\trb=" << rb << "\tpc=" << pc << "\tpd=" << pd << endl;
return 0;
}
g++ lab2ex2.cpp -o lab2ex2
g++ lab2ex2.cpp -o lab2ex2 ./lab2ex2
Expérimentez l'exemple retrouvé à la section 2.10.1 des notes de cours.
// lab2ex3.cpp
#include <iostream>
using namespace std;
void test(int a, int* b, int* c, int& d, int*& e){
a=11; // effet local
b++; // change l’adresse locale de b
*c=13; // change la valeur pointee par c
d=14; // change la valeur referee par d
e=c; // change la valeur du pointeur (adresse) pour celle de c.
}
int main(){
int v1=1, v2=2, v3=3, v4=4, *p5=&v1;
test(v1, &v2, &v3, v4, p5);
cout<<v1<<"\t"<<v2<<"\t"<<v3<<"\t"<<v4<<"\t"<<*p5<<"\t"<<endl;
return 0;
}
#include <iostream>
class Point {
public:
Point(double x=0, double y=0);
Point(const Point&);
~Point();
double distance(const Point&) const;
private:
double x;
double y;
friend std::ostream& operator << (std::ostream&, const Point&);
friend std::istream& operator >> (std::istream&, Point&);
};
#include <assert.h>
#include "point.h"
Point::Point(double _x, double _y)
: x(_x), y(_y){
}
Point::Point(const Point& point)
: x(point.x), y(point.y){
}
Point::~Point(){
}
double Point::distance(const Point& point) const {
// TODO
return 0;
}
std::ostream& operator << (std::ostream& os, const Point& point) {
os << "(" << point.x << "," << point.y << ")";
return os;
}
std::istream& operator >> (std::istream& is, Point& point) {
char po, vir, pf;
is >> po;
if(is){
is >> point.x >> vir >> point.y >> pf;
assert(po=='('); assert(vir==','); assert(pf==')');
}
return is;
}
#include "point.h"
using namespace std;
int main(){
Point a;
Point b(3,4);
cout << a.distance(b) << endl;
return 0;
}
g++ -o lab2ex4a lab2ex4a.cpp point.cpp ./lab2ex4a
g++ -o lab2ex4a lab2ex4a.cpp point.cpp ./lab2ex4a
#include "point.h"
using namespace std;
void test(Point p1, Point& p2, const Point& p3, Point* p4){
cout << p1 << endl;
p1 = p2;
*p4 = p3;
p4 = &p1;
}
int main(){
Point a, b(3,4), c(0,5);
Point*d = new Point(5,0);
test(a,b,c,d);
cout << a << '\t' << b << '\t' << c << '\t' << *d << endl;
return 0;
}
g++ -o lab2ex4b lab2ex4b.cpp point.cpp ./lab2ex4b
g++ -o lab2ex4b lab2ex4b.cpp point.cpp ./lab2ex4b
Point::Point(double _x, double _y)
: x(_x), y(_y){
std::cerr << "Point::Point(double, double)" << std::endl;
}
Point::Point(const Point& point)
: x(point.x), y(point.y){
std::cerr << "Point::Point(const Point&)" << std::endl;
}
Point::~Point(){
std::cerr << "Point::~Point()" << std::endl;
}
g++ -o lab2ex4b lab2ex4b.cpp point.cpp ./lab2ex4b
Créez le programme lab2ex5.cpp à partir du code ci-dessous.
#include "point.h"
class Rectangle{
public:
Rectangle();
Rectangle(const Point& p1, const Point& p2);
private:
Point point1, point2;
};
Rectangle::Rectangle()
{
}
Rectangle::Rectangle(const Point& p1, const Point& p2)
: point1(p1)
{
point2 = p2;
}
int main(){
Rectangle* r1 = new Rectangle();
Rectangle* r2 = new Rectangle(Point(0,0), Point(10,10));
Rectangle* r3 = new Rectangle();
*r3 = *r2;
r1 = r3;
delete r1;
delete r2;
delete r3;
}
g++ -o lab2ex5 lab2ex5.cpp point.cpp ./lab2ex5
g++ -o lab2ex5 lab2ex5.cpp point.cpp ./lab2ex5
Cet exercice est inspiré de la question 2 de l'examen de mi-session de la session d'hiver 2013.
Soit le programme lab2ex6.cpp suivant.
#include <iostream>
class Allo{
public:
Allo(int x_=0)
: x(x_)
{
std::cout << "A" << x << " ";
}
Allo(const Allo& autre)
: x(autre.x)
{
std::cout << "B" << x << " ";
}
~Allo() {
std::cout << "C" << x << " ";
}
int x;
};
void f1(Allo a1, Allo& a2, Allo* a3){
a1.x++;
a2.x--;
a3+=1;
(a3->x)+=2000;
}
int main(){
Allo tab[3];
Allo a(20);
Allo* b = new Allo(5);
Allo* c = tab + 1;
f1(a, b, c);
std::cout << std::endl << "-------" << std::endl;
Allo* t = new Allo[4];
t[2] = Allo(9);
std::cout << std::endl;
}
Avant de compiler et d'exécuter le programme ci-haut, essayez de répondre aux questions suivantes.