In this tutorial, we'll delve into the exciting world of Classes and Objects in C programming. Understanding these concepts will help you create more organized, maintainable, and efficient code. This topic is essential as it allows you to encapsulate data and functions into reusable entities, which are crucial for developing large-scale applications.
Real-world applications of classes and objects range from game development to system programming, where the creation of custom data types and behaviors can simplify complex tasks and improve code maintainability.
A Class is a blueprint or template that defines the structure and behavior of an object. It contains:
For example, consider a simple Person
class with name and age as data members:
#include <stdio.h>
#include <string.h>
// Define Person class structure
struct Person {
char name[50];
int age;
};
// Function to print a person's information
void printPerson(const struct Person* p) {
printf("Name: %s\nAge: %d\n", p->name, p->age);
}
In this example, we define the Person
class structure (a C-style struct) with two members: name and age. We also provide a function printPerson()
to print the information of a person object.
Let's create a more complex example using objects representing books. A book has title, author, pages, and price as data members, and methods to set and get these values:
#include <stdio.h>
#include <string.h>
// Define Book class structure with its members and methods
struct Book {
char title[50];
char author[50];
int pages;
float price;
// Setter functions
void setTitle(char* t) {
strcpy(title, t);
}
void setAuthor(char* a) {
strcpy(author, a);
}
void setPages(int p) {
pages = p;
}
void setPrice(float pr) {
price = pr;
}
// Getter functions
char* getTitle() {
return title;
}
char* getAuthor() {
return author;
}
int getPages() {
return pages;
}
float getPrice() {
return price;
}
};
int main() {
// Create a book object
struct Book book = { "The C Programming Language", "Kernighan & Ritchie", 300, 29.99 };
printf("Title: %s\nAuthor: %s\nPages: %d\nPrice: %.2f\n", book.getTitle(), book.getAuthor(), book.getPages(), book.getPrice());
// Change the book's title, author, pages, and price
book.setTitle("A More Advanced C Programming Book");
book.setAuthor("Advanced C Authors");
book.setPages(500);
book.setPrice(49.99);
printf("\nUpdated Title: %s\nUpdated Author: %s\nUpdated Pages: %d\nUpdated Price: %.2f\n", book.getTitle(), book.getAuthor(), book.getPages(), book.getPrice());
return 0;
}
In this example, we define a Book
class with five data members and six methods (three setters and three getters). In the main()
function, we create a Book
object, display its initial values, modify them using the setter functions, and then display the updated values.
What causes it:
// Bad C code example that triggers the error
struct Book {
char title[50];
char author[50];
int pages;
float price;
void setTitle(char* t) {
strcpy(title, t);
}
void setAuthor(char* a) {
strcpy(author, a);
}
void setPages(int p) {
pages = p;
}
void setPrice(float pr) {
price = pr;
}
// **Missing semicolon at the end of the struct definition**
};
Error message:
error: expected ';' before 'void'
Solution:
// Corrected C code
struct Book {
char title[50];
char author[50];
int pages;
float price;
};
Why it happens:
A semicolon is missing at the end of the struct definition, causing the compiler to expect a statement after it.
How to prevent it:
Always include a semicolon at the end of every C statement and ensure that all struct definitions end with a semicolon.
malloc()
, calloc()
, and free()
for dynamic memory allocation, and avoiding dangling pointers.