C++ class vs C structs
C language
Here the struct contains all the elements inside the declaration. There is also an anonymous struct and a union inside.
#include <stdio.h>
#include <string.h>
struct Humano{
char nombre[30];
int edad;
struct{ // Anonymous structure
char calle[30]
int numero;
char ciudad[30];
}
union{ // Anonymous union
char dni[10];
char pasaporte[25];
}
float altura;
};
int main(){
struct Humano juan;
strcpy(juan.nombre, "juan");
juan.edad = 30;
strcpy(juan.calle, "Av Jazmines");
juan.numero = 752;
strcpy(juan.ciudad, "Lima");
juan.altura = 1.75;
snprintf(juan.dni, sizeof(juan.dni), "1234");
printf("Nombre: %s\n", juan.nombre);
printf("Direccion: %s %d %s\n", juan.calle, juan.numero, juan.ciudad);
printf("DNI: %s\n", juan.dni);
return 0;
}
C++ language variant 1
Here the function prototypes are inside the class declaration and they are implemented outside the brackets of Human
.
#include <iostream>
#include <string.h>
struct Humano{
private:
char nombre[30];
int edad;
public:
Humano(const char* nombre, int edad);
void hablar();
}
Humano::Humano(const char* nombre, int edad){
strcpy(this->nombre, nombre);
this->edad = edad;
}
void Humano::hablar(){
printf("Hola soy %s\n", nombre);
}
int main(){
Humano juan = Humano("Juan", 27);
juan.hablar();
return 0;
}
C++ language variant 2
Here the methods are implemented inside the class declaration.
#include <iostream>
#include <string.h>
struct Humano{
private:
char nombre[30];
int edad;
public:
Humano(const char* nombre, int edad){
strcpy(this->nombre, nombre);
this->edad = edad;
}
void hablar(){
printf("Hola soy %s\n", nombre);
}
}
int main(){
Humano juan = Humano("Juan", 27);
juan.hablar();
return 0;
}
Implement the code with libraries
The following code in C++ can be chopped down to make it more modulare and easier to use in other codes.
#include <iostream>
#include <string.h>
/******************* .h ******************************/
struct Humano{
private:
char nombre[30];
int edad;
public:
Humano(const char* nombre, int edad);
void hablar();
}
/******************* .cpp ****************************/
Humano::Humano(const char* nombre, int edad){
strcpy(this->nombre, nombre);
this->edad = edad;
}
void Humano::hablar(){
printf("Hola soy %s\n", nombre);
}
/******************* main.cpp **************************/
int main(){
Humano juan = Humano("Juan", 27);
juan.hablar();
return 0;
}
Resulting in:
main.cpp
#include <iostream>
#include <string.h>
#include "lib.h"
int main(){
Humano juan = Humano("Juan", 27);
juan.hablar();
return 0;
}
lib.h
#ifdef LIB_H
#define LIB_H
struct Humano{
private:
char nombre[30];
int edad;
public:
Humano(const char* nombre, int edad);
void hablar();
}
#endif
lib.cpp
#include "lib.h"
Humano::Humano(const char* nombre, int edad){
strcpy(this->nombre, nombre);
this->edad = edad;
}
void Humano::hablar(){
printf("Hola soy %s\n", nombre);
}