practice/inherit/main.c

57 lines
1.3 KiB
C

#include <stdio.h>
#include <string.h>
int main ()
{
/* Create generic vehicle type */
typedef struct
{
int life;
int magic;
}Vehicle;
/* Create car, which is a type of vehicle
* access vehicle vars by doing car.vehicle.var
*/
typedef struct
{
Vehicle vehicle;
char brand[10];
}Car;
/* We create our first car */
Car myCar;
myCar.vehicle.life = 5;
myCar.vehicle.magic = 9;
strcpy(myCar.brand, "Dodge");
printf("My car's life is: %d\n" "My car's magic is: %d\n"
"My car's brand is: %s\n\n", myCar.vehicle.life, myCar.vehicle.magic,
myCar.brand);
/* We create another to show using thing same struct with different values */
Car herCar;
herCar.vehicle.life = 8;
herCar.vehicle.magic = 5;
strcpy(herCar.brand, "Saturn");
printf("Her car's life is: %d\n" "Her car's magic is: %d\n"
"Her car's brand is: %s\n\n", herCar.vehicle.life, herCar.vehicle.magic,
herCar.brand);
/* Here we duplicate herCar (let's say we have a lot of saturns with same stats
* But may at some later point lose or gain stats
* */
Car herCar2 = herCar;
printf("Copy of HerCar\n" "Her car's life is: %d\n" "Her car's magic is: %d\n"
"Her car's brand is: %s\n", herCar2.vehicle.life, herCar2.vehicle.magic,
herCar2.brand);
}