Szerkesztő:LinguisticMystic/cpp/ClassesObjectsMethods
🔹 1. Introduction to Data Types
[szerkesztés]C++ provides a wide range of fundamental (built-in) data types such as:
int→ for integersdouble→ for floating-point numberschar→ for charactersbool→ for Boolean valuesvoid→ for functions that return nothing
These types are sufficient for basic tasks. However, as problems become more complex, we need a way to group related data together and define behaviors — this is where user-defined types like struct and class come into play.
🔹 2. Structures (struct) – Grouping Data
[szerkesztés]struct DateStruct {
int day;
int month;
int year;
};
This struct acts as a simple data container. It lets us group related variables. You can initialize and access members like this:
DateStruct today {5, 5, 2023};
today.day = 18;
today.month = 2;
today.year = 2024;
To operate on this data, you typically use external functions:
void print(const DateStruct &date) {
std::cout << date.year << "-" << date.month << "-" << date.day;
}
However, functions in struct are not bound to the data. You can pass any DateStruct, and the function doesn’t inherently know which one it’s tied to.
🔹 3. Classes – The Power of OOP
[szerkesztés]C++ introduces the class keyword to allow not just storing data but also defining behavior directly associated with that data.
Example:
[szerkesztés]class DateClass {
public:
int day;
int month;
int year;
void printEU() {
std::cout << day << "-" << month << "-" << year;
}
void printUS() {
std::cout << year << "-" << month << "-" << day;
}
};
✅ Unlike
struct, aclasscombines data and functions (called methods) in a single unit. This is encapsulation, a core OOP concept.
🔹 4. Creating and Using Objects
[szerkesztés]Once a class is defined, you can create an object (an instance of the class):
DateClass today {11, 11, 2011};
today.day = 15;
today.printEU(); // Output: 15-11-2011
today.printUS(); // Output: 2011-11-15
DateClassis the blueprinttodayis the object (instance)printEU()andprintUS()are methods
Important:
[szerkesztés]- The object holds the data (state)
- The methods act on that specific object’s data
🔹 5. Defining Methods Outside the Class
[szerkesztés]For readability and maintainability, you often define method prototypes inside the class and define their bodies outside.
class DateClass {
public:
int day, month, year;
void printEU();
void printUS();
};
// Definition outside the class
void DateClass::printEU() {
std::cout << day << "-" << month << "-" << year;
}
void DateClass::printUS() {
std::cout << year << "-" << month << "-" << day;
}
DateClass::printEU()means “theprintEUmethod of theDateClassclass”.
🔹 6. Summary Cheat Sheet
[szerkesztés]| Concept | Description |
|---|---|
class |
A user-defined data type that can hold both data and functions |
object |
An instance of a class |
method |
A function that belongs to a class |
. operator |
Used to access members (variables and methods) of an object |
public: |
Access modifier that makes members accessible outside the class |
Best Practices:
[szerkesztés]- Use
structfor plain data containers. - Use
classwhen you need encapsulation, i.e., grouping data + behavior. - Use capitalized names for class identifiers (
DateClass,Employee, etc.). - Use methods inside the class to manipulate or view the class’s data.
🔹 7. Real-World Analogy
[szerkesztés]Imagine a Blueprint of a Car:
class Car {
public:
std::string color;
int speed;
void accelerate() {
speed += 10;
}
void brake() {
speed -= 10;
}
};
Now create objects:
Car myCar;
myCar.color = "Red";
myCar.speed = 0;
myCar.accelerate();
Caris the blueprintmyCaris an actual car with a specific color and speedaccelerate()changes that car’s speed
🔹 8. Conclusion
[szerkesztés]C++ classes allow you to model real-world entities by bundling data and operations together. This is essential for building scalable, reusable, and maintainable code in modern applications.
You’ve learned:
- How classes and structs differ
- How to declare objects and call methods
- How to define methods inside and outside a class
- Why encapsulation and object-oriented thinking are powerful