C++ Classes and Objects: Definitions, Creation, and Access Modifiers
In object-oriented programming, classes and objects are two very important concepts. C++, as a programming language that supports object-oriented programming, organizes and manages code through classes and objects. In this article, we will explore classes and objects in C++, including how to define, create, and access member variables.
1. What Are Classes and Objects?
-
Class is a blueprint or template that describes a set of entities with the same attributes and behaviors. It contains data members (attributes) and member functions (methods). It can be seen as a custom data type.
-
Object is a concrete instance created based on this template, having actual states (data values) and behaviors (functions).
Example
Below is a simple class representing a person:
class Person {public: string name; // Name int age; // Age void introduce() { // Method for introduction cout << "Hello, my name is " << name << ", I am " << age << " years old." << endl; }};
In this example, <span>Person</span>
is a class with two data members: <span>name</span>
and <span>age</span>
; and one member function: <span>introduce()</span>
.
2. Defining a C++ Class
Now that we understand what a class is, let’s look at how to define a simple class in C++. Below are some explanations about constructors, destructors, and access modifiers (permissions).
2.1 Basic Structure of a Class
class ClassName {public: // Public section, accessible from outside data_type member_variable; // Data member void memberFunction(); // Declaration of member function private: // Private section, for internal use only data_type private_member_variable; protected: // Protected section, accessible by this class and its subclasses data_type protected_member_variable;};
3. Creating Objects
Once we have defined one or more templates (i.e., classes), we can create specific objects based on these templates. Below is how to create a <span>Person</span>
object and assign values in C++:
#include <iostream>
using namespace std;
class Person {public: string name; int age; void introduce() { cout << "Hello, my name is " << name << ", I am " << age << " years old." << endl; }};
int main() { Person person1; // Create a Person type object person1 person1.name = "Xiao Ming"; // Assign value to data member person1.age = 20; // Assign value to data member person1.introduce(); // Call method return 0;}
When the above code runs, it will output:
Hello, my name is Xiao Ming, I am 20 years old.
4. Detailed Explanation of Access Modifiers
In C++, access to member variables or methods in a class is controlled through keywords, which is crucial for encapsulation.
Public
Public members allow external code to access them directly. This is usually used for methods or properties that need to be publicly accessible.
Private
Private members can only be accessed by the class itself, and external code cannot reference them directly. This is often used to protect certain information from being overly exposed, which could lead to erroneous operations.
Protected
Protected members are similar to private, but they can be accessed by subclasses or derived classes. This is generally encountered when needing to extend functionality for future use.
Example:
class Base {private: int a;public: Base(int value) : a(value) {} protected: void showValue() { cout<<a<<endl; }};
class Derived : public Base { public: Derived(int value) : Base(value) {}
void displayParentValue(){this->showValue();}};
int main(){Derived obj(100);obj.displayParentValue();return 0;}
The above example demonstrates the basic process of accessing and displaying values between a base class and a derived class. This illustrates the importance of understanding the mechanisms of different programming paradigms.
In summary, utilizing polymorphism in C++ simplifies what would otherwise be a complex task of expressing algorithms and extending functionality, thus saving time and enhancing efficiency.