Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

OOP CLASS C for student

+11 votes
asked Jul 9, 2022 by nizarbnm (220 points)
Good day !

I have a while looking for some books that helps me to learn how to make CLASS using only C to make a softwar .exe using Inheritance, Encapsulation and Object. I have Base algortihme, Pascal, and C ( Visual studio code using to code sous windows 10) i learn it in the university but i didnt take a speciality so i decide to take OOP C us my base to programmer level.

Thank you for Advanced wishing that i'll hear good news early.

2 Answers

+2 votes
answered Jul 9, 2022 by Peter Minarik (101,340 points)

The C language is not object-oriented. There are no classes there.

What you mean is the C++ language.

You can just check out tutorials online, you don't need a book (unless you like the physical form, the touch of a book, and the smell of the paper).

Here are a few C++ tutorial sites:

Feel free to search for your own.

Have fun learning!

commented Aug 7, 2022 by Shubham Kumar (110 points)
sharing you the Data structure and algorithms sheet
 - https://www.linkedin.com/feed/update/urn:li:activity:6960963623921876992/
0 votes
answered Jun 27, 2025 by Jerry Jeremiah (2,040 points)
edited Jun 27, 2025 by Jerry Jeremiah

If you are interested in doing OOP with C then you need to use function pointers to make member functions:

You do need to reference the base class directly since it isn't done automatically.

https://onlinegdb.com/uG7h4FgCF

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Define a base class Shape (abstract class)
typedef struct {
    double (*area)(void*);
    double (*perimeter)(void*);
} Shape;

// Circle struct and methods
typedef struct {
    Shape base;
    double radius;
} Circle;

double circle_area(void* c) {
    Circle* circle = (Circle*)c;
    return M_PI * circle->radius * circle->radius;
}

double circle_perimeter(void* c) {
    Circle* circle = (Circle*)c;
    return 2 * M_PI * circle->radius;
}

Circle* create_circle(double radius) {
    Circle* circle = (Circle*)malloc(sizeof(Circle));
    if (circle == NULL) {
        perror("Failed to allocate memory for circle");
        exit(EXIT_FAILURE);
    }
    circle->base.area = circle_area;
    circle->base.perimeter = circle_perimeter;
    circle->radius = radius;
    return circle;
}

// Square struct and methods
typedef struct {
    Shape base;
    double side;
} Square;

double square_area(void* s) {
    Square* square = (Square*)s;
    return square->side * square->side;
}

double square_perimeter(void* s) {
    Square* square = (Square*)s;
    return 4 * square->side;
}

Square* create_square(double side) {
    Square* square = (Square*)malloc(sizeof(Square));
    if (square == NULL) {
         perror("Failed to allocate memory for square");
         exit(EXIT_FAILURE);
    }
    square->base.area = square_area;
    square->base.perimeter = square_perimeter;
    square->side = side;
    return square;
}

// Triangle struct and methods
typedef struct {
    Shape base;
    double side1;
    double side2;
    double side3;
} Triangle;

double triangle_area(void* t) {
    Triangle* triangle = (Triangle*)t;
    double s = (triangle->side1 + triangle->side2 + triangle->side3) / 2.0;
    return sqrt(s * (s - triangle->side1) * (s - triangle->side2) * (s - triangle->side3));
}

double triangle_perimeter(void* t) {
    Triangle* triangle = (Triangle*)t;
    return triangle->side1 + triangle->side2 + triangle->side3;
}

Triangle* create_triangle(double side1, double side2, double side3) {
    Triangle* triangle = (Triangle*)malloc(sizeof(Triangle));
    if (triangle == NULL) {
        perror("Failed to allocate memory for triangle");
        exit(EXIT_FAILURE);
    }
    triangle->base.area = triangle_area;
    triangle->base.perimeter = triangle_perimeter;
    triangle->side1 = side1;
    triangle->side2 = side2;
    triangle->side3 = side3;
    return triangle;
}

// Main function
int main() {
    // Create shape objects
    Circle* circle = create_circle(5.0);
    Square* square = create_square(4.0);
    Triangle* triangle = create_triangle(3.0, 4.0, 5.0);

    // Calculate and print areas and perimeters
    printf("Circle Area: %.2f, Perimeter: %.2f\n", circle->base.area(circle), circle->base.perimeter(circle));
    printf("Square Area: %.2f, Perimeter: %.2f\n", square->base.area(square), square->base.perimeter(square));
    printf("Triangle Area: %.2f, Perimeter: %.2f\n", triangle->base.area(triangle), triangle->base.perimeter(triangle));

    // Free allocated memory
    free(circle);
    free(square);
    free(triangle);

    return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...