#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
lab2ex2.cpp: In function ‘int main()’: lab2ex2.cpp:7:14: erreur: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int*’La partie de droite après le = doit être un entier ou une référence sur un entier et non un pointeur d'entier.
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
0
#include <math.h> ... double Point::distance(const Point& point) const { double dx = x - point.x, dy = y - point.y; return sqrt(dx*dx + dy*dy); }
g++ -o lab2ex4a lab2ex4a.cpp point.cpp ./lab2ex4a
5
#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
(0,0) (0,0) (3,4) (0,5) (0,5)
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
Point::Point(double, double) Point::Point(double, double) Point::Point(double, double) Point::Point(double, double) Point::Point(const Point&) (0,0) Point::~Point() (0,0) (3,4) (0,5) (0,5) Point::~Point() Point::~Point() Point::~Point() Point::~Point()
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
int main() { Rectangle* r1 = new Rectangle(); Rectangle* r2 = new Rectangle(Point(0, 0), Point(10, 10)); Rectangle* r3 = new Rectangle(); *r3 = *r2; delete r1; // Il faut faire delete sur r1 avant d'écraser sa valeur r1 = r3; // Copie du pointeur. delete r2; delete r3; }
g++ -o lab2ex5 lab2ex5.cpp point.cpp ./lab2ex5
Notez que cet exercice est inspiré d'une question d'examen de mi-session.
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; return 0; }
Avant de compiler et d'exécuter le programme ci-haut, essayez de répondre aux questions suivantes.
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;
return 0;
}
A0 A0 A0 A20 A5 B20 C21 ------- A0 A0 A0 A0 A9 C9 C20 C2000 C0 C0
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; delete b; delete[] t; return 0; }
Après correction, le programme affiche:
A0 A0 A0 A20 A5 B20 C21 ------- A0 A0 A0 A0 A9 C9 C4 C0 C9 C0 C0 C20 C2000 C0 C0