In C++, a CLASS is a data type that groups data and functions under a single name and represents a real-world concept or entity.
Classes provide a template for creating instances of the class. Each instance has its own state *(stored in fields) and behavior (defined in methods).
Classes allow us to encapsulate related data and functionality, which facilitates code reuse and maintenance of our applications. They are the foundation of object-oriented programming.
If you want to learn more, check out the Object-Oriented Programming Course
Classes in C++
The basic syntax for defining a class in C++ is as follows:
class ClassName
{
public:
// Field definitions
DataType fieldName;
// Property definitions (access methods)
DataType getPropertyName() const;
void setPropertyName(DataType value);
// Method definitions
ReturnType methodName(Parameters);
};
- ClassName: The name of the class, which must be unique.
- DataType: The data type of the fields and properties.
- fieldName: The name of the field.
- methodName: The name of the method.
- ReturnType: The data type that the method returns.
- Parameters: The variables used to pass information to the method.
Classes are generally defined in files with the .cpp or .h (header) extension.
Composition of a Class in C++
A class in C++ is composed of three main elements: fields (attributes), properties (accessor methods), and methods (member functions).
- stringfechaNacimiento
- stringdni
- stringSaludar()
- stringMandarEmail()
Basic Example
To create a class in C++, we use the class keyword followed by the class name and a list of members (fields and methods).
#include <iostream>
#include <string>
class Person
{
public:
// Fields
std::string Name;
int Age;
// Constructor
Person(const std::string& name, int age)
: Name(name), Age(age) {}
// Method
void Greet()
{
std::cout << "Hello, I'm " << Name << " and I'm " << Age << " years old!" << std::endl;
}
};
int main()
{
// Create an object of the Person class
Person person("Luis", 25);
// Call the Greet method
person.Greet();
return 0;
}
In this example, we have:
- Defined a
Personclass with two fields (Name,Age). - Implemented a constructor that initializes these fields.
- Defined a
Greetmethod that prints a greeting with the person’s name and age. - Created an instance of
Personand called theGreetmethod.
Using Classes
Creating Objects
To create an object of a class, we simply declare a variable of the class type and, optionally, call the constructor.
Person person("Ana", 30);
Accessing Fields and Properties
The fields and properties of a class are accessed using the dot notation (.).
std::cout << person.Name << std::endl;
Calling Methods
Methods of a class are also called using the dot notation (.).
person.Greet();
Inheritance and Polymorphism
C++ supports inheritance, which allows creating a new class based on an existing class, and polymorphism, which allows using methods from base and derived classes in a flexible way.
We will see these concepts in the rest of the course
Practical Examples
Representing a Person
Here we define a Person class that contains properties for name and age, a constructor to initialize these values, and a method to greet.
#include <iostream>
#include <string>
class Person
{
private:
std::string name;
int age;
public:
// Constructor
Person(const std::string& name, int age)
: name(name), age(age) {}
// Method
void Greet() const
{
std::cout << "Hello, I'm " << name << " and I'm " << age << " years old!" << std::endl;
}
};
Representing a Product
The Product class represents a product with properties for name and price. It includes a constructor to initialize these fields and a method to display the product information.
#include <iostream>
#include <string>
class Product
{
private:
std::string name;
double price;
public:
// Constructor
Product(const std::string& name, double price)
: name(name), price(price) {}
// Method
void ShowInformation() const
{
std::cout << "Product: " << name << ", Price: " << price << std::endl;
}
};
