A Class is a user-defined blueprint from which individual Objects are created. An object is an actual instance occupying physical RAM with concrete data values.
#include <iostream>
#include <string>
class Employee {
private:
int id;
std::string name;
double salary;
public:
// Member function to set data (using 'this' to disambiguate names)
void setData(int id, const std::string& name, double salary) {
this->id = id;
this->name = name;
this->salary = salary;
}
void displayProfile() const {
std::cout << "Employee ID: " << id << " | Name: " << name << " | Salary: $" << salary << "\n";
}
};
int main() {
// 1. Stack Allocation (Automatic lifetime)
Employee emp1;
emp1.setData(101, "Alice", 85000);
emp1.displayProfile(); // Uses dot operator (.)
// 2. Heap Allocation (Pointer access)
Employee* empPtr = new Employee();
empPtr->setData(102, "Bob", 92000);
empPtr->displayProfile(); // Uses arrow operator (->)
delete empPtr;
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Object initialization and cleanup mechanics.
public, private, and protected member visibility.
Review the 4 core pillars of object orientation.