Classe Cotxe, amb C a pèl i amb C++
Contingut |
Introducció
Es vol fer una comparativa entre utilitzar C a pèl o C++ per definir la classe Cotxe. Evidentment, en el cas de C a pèl no podem definir la classe Cotxe, però sí que podem definir una estructura on podem inquibir-hi les propietats. El mètodes, com ara inicialitzar_cotxe(), no formen part de l'estructura i són externs.
La conclusió és que utilitzar C++ per a fer POO dóna com a resultat una solució molt més elegant i encapsulada, com és d'esperar. Tot i això, també es pot afrontar la mateixa solució amb C a pèl.
C a pèl
tutorial_c_v1.c
// gcc -o tutorial_c tutorial_c_v1.c //v1. Struct Cotxe (funciona com si fos un objecte, però no ho és, perquè no és POO. És llenguatge C a pèl). #include <stdio.h> #include <malloc.h> #include <string.h> struct Cotxe { char *marca; int cilindrada; }; struct Cotxe cotxe1; //desenes de milió, milió, centenars de milers, desenes de milers, milers, centenars, desenes i unitats void inicialitzar_cotxe (struct Cotxe *); //passem per referència int main(int argc, char* argv[]) { inicialitzar_cotxe(&cotxe1); printf("marca: %s - cilindrada: %d\n", cotxe1.marca, cotxe1.cilindrada); return 0; } void inicialitzar_cotxe (struct Cotxe *cotxe) { cotxe->marca = (char *)malloc(30*sizeof(char)); cotxe->cilindrada = 1430; strcpy(cotxe->marca,"Seat"); }
tutorial_c_v2.c
// gcc -o tutorial_c tutorial_c_v2.c //v1. Struct Cotxe (funciona com si fos un objecte, però no ho és, perquè no és POO. És llenguatge C a pèl). //v2. matriu d'objectes Cotxe. Inicialitzem els cotxes 1 a 1 #include <stdio.h> #include <malloc.h> #include <string.h> struct Cotxe { char *marca; int cilindrada; }; struct Cotxe cotxe[5]; void inicialitzar_cotxe (struct Cotxe *); //passem per referència int main(int argc, char* argv[]) { int i; for(i = 0; i < sizeof(cotxe) / sizeof(struct Cotxe); i++) { inicialitzar_cotxe(&cotxe[i]); printf("marca: %s - cilindrada: %d\n", cotxe[0].marca, cotxe[0].cilindrada); } return 0; } void inicialitzar_cotxe (struct Cotxe *cotxe) { cotxe->marca = (char *)malloc(30*sizeof(char)); cotxe->cilindrada = 1430; strcpy(cotxe->marca,"Seat"); }
tutorial_c_v3.c
Compte! El codi no està bé del tot. Veure versió 4
// gcc -o tutorial_c tutorial_c_v3.c //v1. Struct Cotxe (funciona com si fos un objecte, però no ho és, perquè no és POO. És llenguatge C a pèl). //v2. matriu d'objectes Cotxe. Inicialitzem els cotxers 1 a 1 //v3. matriu d'objectes Cotxe. Inicialitzem l'array de cotxes dins la funció inicialitzar_matriu_cotxes // http://stackoverflow.com/questions/1106957/passing-an-array-by-reference-in-c // passo l'argument size i facilita molt les coses // però en realitat hi ha una errada, tot i que compila i s'executa bé. El correcte és la v4 #include <stdio.h> #include <malloc.h> #include <string.h> struct Cotxe { char *marca; int cilindrada; }; struct Cotxe cotxe[5]; void inicialitzar_matriu_cotxes( struct Cotxe *, int size); int main(int argc, char* argv[]) { int size = sizeof(cotxe) / sizeof(struct Cotxe); inicialitzar_matriu_cotxes(cotxe, size); int i; for(i = 0; i < size ; i++) { printf("marca: %s - cilindrada: %d\n", cotxe[i].marca, cotxe[i].cilindrada); } return 0; } void inicialitzar_matriu_cotxes( struct Cotxe *cotxe, int size ) { int i; for(i = 0; i < size ; i++) { cotxe[i].marca = (char *)malloc(30*sizeof(char)); cotxe[i].cilindrada = 1430+i; strcpy(cotxe[i].marca,"Seat"); //printf("marca: %s - cilindrada: %d\n", cotxe[i].marca, cotxe[i].cilindrada); } }
tutorial_c_v4.c
// gcc -o tutorial_c tutorial_c_v4.c //v1. Struct Cotxe (funciona com si fos un objecte, però no ho és, perquè no és POO. És llenguatge C a pèl). //v2. matriu d'objectes Cotxe. Inicialitzem els cotxers 1 a 1 //v3. matriu d'objectes Cotxe. Inicialitzem l'array de cotxes dins la funció inicialitzar_matriu_cotxes // http://stackoverflow.com/questions/1106957/passing-an-array-by-reference-in-c // passo l'argument size i facilita molt les coses // però en realitat hi ha una errada, tot i que compila i s'executa bé. El correcte és la v4 //v4. ara també compila i s'executa bé. Aquesta és la versió correcta. #include <stdio.h> #include <malloc.h> #include <string.h> struct Cotxe { char *marca; int cilindrada; }; struct Cotxe cotxe[5]; void inicialitzar_matriu_cotxes( struct Cotxe [], int size); int main(int argc, char* argv[]) { int size = sizeof(cotxe) / sizeof(struct Cotxe); inicialitzar_matriu_cotxes(&cotxe[0], size); int i; for(i = 0; i < size ; i++) { printf("marca: %s - cilindrada: %d\n", cotxe[i].marca, cotxe[i].cilindrada); } return 0; } void inicialitzar_matriu_cotxes( struct Cotxe cotxe[], int size ) { int i; for(i = 0; i < size ; i++) { cotxe[i].marca = (char *)malloc(30*sizeof(char)); cotxe[i].cilindrada = 1430+i; strcpy(cotxe[i].marca,"Seat"); //printf("marca: %s - cilindrada: %d\n", cotxe[i].marca, cotxe[i].cilindrada); } }
tutorial_c_v5.c, cotxe.c, cotxe.h
Recordar que puc compilar el fitxer cotxe.c (amb l'opció -c) abans de linkar:
$ gcc -c -o cotxe.o cotxe.c $ gcc -o tutorial_c tutorial_c_v5.c cotxe.o
// gcc -o tutorial_c tutorial_c_v5.c cotxe.c //v1. Struct Cotxe (funciona com si fos un objecte, però no ho és, perquè no és POO. És llenguatge C a pèl). //v2. matriu d'objectes Cotxe. Inicialitzem els cotxers 1 a 1 //v3. matriu d'objectes Cotxe. Inicialitzem l'array de cotxes dins la funció inicialitzar_matriu_cotxes // http://stackoverflow.com/questions/1106957/passing-an-array-by-reference-in-c // passo l'argument size i facilita molt les coses // però en realitat hi ha una errada, tot i que compila i s'executa bé. El correcte és la v4 //v4. ara també compila i s'executa bé. Aquesta és la versió correcta. //v5. Ara ja podem separar el fitxer principal i la 'classe' cotxe.c i cotxe.h //tot això ho he fet amb C a pèl, sense c++ ni classes. La següent versió hauria de ser amb C++ i definir la classe Cotxe, amb propietats i mètodes. //recordar que puc compilar el fitxer cotxe.c (amb l'opció -c) abans de linkar: // $ gcc -c -o cotxe.o cotxe.c // $ gcc -o tutorial_c tutorial_c_v5.c cotxe.o #include <stdio.h> #include <malloc.h> #include <string.h> #include "cotxe.h" struct Cotxe cotxe[5]; int main(int argc, char* argv[]) { int size = sizeof(cotxe) / sizeof(struct Cotxe); inicialitzar_matriu_cotxes(&cotxe[0], size); int i; for(i = 0; i < size ; i++) { printf("marca: %s - cilindrada: %d\n", cotxe[i].marca, cotxe[i].cilindrada); } return 0; }
cotxe.h
struct Cotxe { char *marca; int cilindrada; }; void inicialitzar_matriu_cotxes( struct Cotxe [], int size);
cotxe.c
#include <stdio.h> #include <malloc.h> #include <string.h> #include "cotxe.h" void inicialitzar_matriu_cotxes( struct Cotxe cotxe[], int size ) { int i; for(i = 0; i < size ; i++) { cotxe[i].marca = (char *)malloc(30*sizeof(char)); cotxe[i].cilindrada = 1430+i; strcpy(cotxe[i].marca,"Seat"); //printf("marca: %s - cilindrada: %d\n", cotxe[i].marca, cotxe[i].cilindrada); } }
C++
tutorial_cpp_v1.cpp
// g++ -o tutorial_cpp tutorial_cpp_v1.cpp //v1. classe Cotxe #include <iostream> #include <string> //amb la classe string, el tractament de les cadenes és molt més fàcil amb C++. Per ex, ara no cal malloc using namespace std; class Cotxe { int cilindrada; string marca; public: Cotxe (); Cotxe (int, string); void set_values (int, string); void print_valors (); }; Cotxe::Cotxe () { cilindrada = 1430; marca = "Seat"; } Cotxe::Cotxe (int cil, string mar) { cilindrada = cil; marca = mar; } void Cotxe::set_values (int cil, string mar) { cilindrada = cil; marca = mar; } void Cotxe::print_valors () { cout << "cilindrada: " << cilindrada << endl; cout << "marca: " << marca << endl; } int main () { Cotxe cotxe1 (1600,"Volvo"); Cotxe cotxe2; cotxe1.print_valors (); cotxe2.print_valors (); cotxe1.set_values (1800,"Renault"); cotxe1.print_valors (); return 0; }
tutorial_cpp_v2.cpp
// g++ -o tutorial_cpp tutorial_cpp_v2.cpp //v1. classe Cotxe //v2. matriu d'objectes Cotxe. Inicialitzem els cotxes 1 a 1 #include <iostream> #include <string> //amb la classe string, el tractament de les cadenes és molt més fàcil amb C++. Per ex, ara no cal malloc using namespace std; class Cotxe { int cilindrada; string marca; public: Cotxe (); Cotxe (int, string); void set_values (int, string); void print_valors (); }; Cotxe::Cotxe () { cilindrada = 1430; marca = "Seat"; } Cotxe::Cotxe (int cil, string mar) { cilindrada = cil; marca = mar; } void Cotxe::set_values (int cil, string mar) { cilindrada = cil; marca = mar; } void Cotxe::print_valors () { cout << "cilindrada: " << cilindrada << endl; cout << "marca: " << marca << endl; } int main () { int size; int i; Cotxe cotxe[5]; cotxe[0].print_valors (); cotxe[1].print_valors (); cotxe[2].set_values (1800,"Renault"); cotxe[2].print_valors (); size = sizeof(cotxe) / sizeof(Cotxe); cout << "num items: " << size << endl << endl; for(i = 0; i < size; i++) { cotxe[i].set_values (1430+i,"Seat"); cotxe[i].print_valors (); } return 0; }
tutorial_cpp_v4.cpp
// g++ -o tutorial_cpp tutorial_cpp_v4.cpp //v1. classe Cotxe //v2. matriu d'objectes Cotxe. Inicialitzem els cotxes 1 a 1 //v4. matriu d'objectes Cotxe. Inicialitzem l'array de cotxes dins la funció inicialitzar_matriu_cotxes. És un exemple de com puc passar amb u punter tot l'array de cotxes. #include <iostream> #include <string> //amb la classe string, el tractament de les cadenes és molt més fàcil amb C++. Per ex, ara no cal malloc using namespace std; class Cotxe { int cilindrada; string marca; public: Cotxe (); Cotxe (int, string); void set_values (int, string); void print_valors (); }; Cotxe::Cotxe () { cilindrada = 1430; marca = "Seat"; } Cotxe::Cotxe (int cil, string mar) { cilindrada = cil; marca = mar; } void Cotxe::set_values (int cil, string mar) { cilindrada = cil; marca = mar; } void Cotxe::print_valors () { cout << "cilindrada: " << cilindrada << endl; cout << "marca: " << marca << endl; } void inicialitzar_matriu_cotxes( Cotxe *, int size); int main () { int size; int i; Cotxe cotxe[5]; cotxe[0].print_valors (); cotxe[1].print_valors (); cotxe[2].set_values (1800,"Renault"); cotxe[2].print_valors (); size = sizeof(cotxe) / sizeof(Cotxe); cout << "num items: " << size << endl << endl; inicialitzar_matriu_cotxes(cotxe, size); return 0; } void inicialitzar_matriu_cotxes( Cotxe *cotxe, int size ) { int i; for(i = 0; i < size ; i++) { cotxe[i].set_values (1430+i,"Seat"); cotxe[i].print_valors (); } }
tutorial_cpp_v5.cpp, Cotxe.cpp, Cotxe.hpp
Recordar que puc compilar el fitxer cotxe.c (amb l'opció -c) abans de linkar:
$ g++ -c -o Cotxe.o Cotxe.cpp $ g++ -o tutorial_cpp tutorial_cpp_v5.cpp Cotxe.o
tutorial_cpp_v5.cpp
// g++ -o tutorial_cpp tutorial_cpp_v5.cpp Cotxe.cpp //v1. classe Cotxe //v2. matriu d'objectes Cotxe. Inicialitzem els cotxes 1 a 1 //v4. matriu d'objectes Cotxe. Inicialitzem l'array de cotxes dins la funció inicialitzar_matriu_cotxes. És un exemple de com puc passar amb u punter tot l'array de cotxes. //v5. Ara ja podem separar el fitxer principal i la 'classe' Cotxe.cpp i Cotxe.hpp, on queda definit tota la lògica de la classe Cotxe #include <iostream> #include <string> //amb la classe string, el tractament de les cadenes és molt més fàcil amb C++. Per ex, ara no cal malloc #include "Cotxe.hpp" using namespace std; void inicialitzar_matriu_cotxes( Cotxe *, int size); int main () { int size; int i; Cotxe cotxe[5]; cotxe[0].print_valors (); cotxe[1].print_valors (); cotxe[2].set_values (1800,"Renault"); cotxe[2].print_valors (); size = sizeof(cotxe) / sizeof(Cotxe); cout << "num items: " << size << endl << endl; inicialitzar_matriu_cotxes(cotxe, size); return 0; } void inicialitzar_matriu_cotxes( Cotxe *cotxe, int size ) { int i; for(i = 0; i < size ; i++) { cotxe[i].set_values (1430+i,"Seat"); cotxe[i].print_valors (); } }
Cotxe.cpp
#include <iostream> #include <string> //amb la classe string, el tractament de les cadenes és molt més fàcil amb C++. Per ex, ara no cal malloc #include "Cotxe.hpp" Cotxe::Cotxe () { cilindrada = 1430; marca = "Seat"; } Cotxe::Cotxe (int cil, string mar) { cilindrada = cil; marca = mar; } void Cotxe::set_values (int cil, string mar) { cilindrada = cil; marca = mar; } void Cotxe::print_valors () { cout << "cilindrada: " << cilindrada << endl; cout << "marca: " << marca << endl; }
Cotxe.hpp
using namespace std; class Cotxe { int cilindrada; string marca; public: Cotxe (); Cotxe (int, string); void set_values (int, string); void print_valors (); };
creat per Joan Quintana Compte, març 2014