C++ Classes and Objects
Introduction
In C++, a class is a user-defined data type that encapsulates data members (variables) and member functions (methods) into a single entity. An object is an instance of a class. It represents a specific instance with its own set of data and behavior.
Creating a Class
To create a class in C++, you need to define its structure, which includes data members and member functions. Here's how you can create a class:
// Header file: MyClass.h
class MyClass {
private:
// Data members (variables)
int myVariable;
public:
// Member functions (methods)
void myMethod();
};
In the above example, we create a class named MyClass. It has a private data member myVariable and a public member function myMethod. The private keyword signifies that myVariable can only be accessed within the class, while the public keyword allows myMethod to be accessed from outside the class.
Creating an Object
Once you have defined a class, you can create objects of that class. Objects are instances that hold the data members and can invoke member functions.
Here's how you can create an object of the MyClass class:
// Source file: main.cpp
#include "MyClass.h" // Include the class header file
int main() {
MyClass myObject; // Create an object of MyClass
return 0;
}
In the above example, we include the header file MyClass.h, which contains the class definition. Then, within the main() function, we create an object named myObject of type MyClass using the class name followed by the object name.
Accessing Members of an Object
Once you have created an object, you can access its data members and member functions using the dot operator (.).
Here's an example:
// Source file: main.cpp
#include "MyClass.h"
int main() {
MyClass myObject;
myObject.myVariable = 42; // Accessing a data member
myObject.myMethod(); // Invoking a member function
return 0;
}
In the above example, we access the data member myVariable of myObject using the dot operator (.) and assign it the value 42. Similarly, we invoke the member function myMethod() using the dot operator.
Multiple Objects
You can create multiple objects of a class, each with its own set of data and behavior.
Here's an example:
// Source file: main.cpp
#include "MyClass.h"
int main() {
MyClass obj1;
MyClass obj2;
obj1.myVariable = 10;
obj2.myVariable = 20;
obj1.myMethod();
obj2.myMethod();
return 0;
}
In the above example, we create two objects obj1 and obj2 of type MyClass. We assign different values to the myVariable data member of each object. Then, we invoke the myMethod() member function on both objects.
Example Code Summary
Here's a summary of the example code discussed above:
// Header file: MyClass.h
class MyClass {
private:
int myVariable;
public:
void myMethod();
};
// Source file: main.cpp
#include "MyClass.h"
int main() {
MyClass myObject;
myObject.myVariable = 42;
myObject.myMethod();
return 0;
}
// Source file: main.cpp
#include "MyClass.h"
int main() {
MyClass obj1;
MyClass obj2;
obj1.myVariable = 10;
obj2.myVariable = 20;
obj1.myMethod();
obj2.myMethod();
return 0;
}
In C++, classes and objects allow you to create user-defined types with their own data and behavior. You can define classes, create objects, access data members, and invoke member functions to perform operations. This object-oriented paradigm provides encapsulation and modularity, making it a powerful feature of C++.