C++ Class Example

5 min read Jul 01, 2024
C++ Class Example

A Simple C++ Class Example: The Dog Class

This article will walk you through a basic example of a C++ class, using a familiar concept: a dog.

Defining the Dog Class

#include 
#include 

using namespace std;

class Dog {
public:
    // Constructor
    Dog(string name, int age) : name(name), age(age) {}

    // Methods (functions within the class)
    void bark() const {
        cout << name << " barks!" << endl;
    }

    void displayInfo() const {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }

private:
    // Data members (attributes of the class)
    string name;
    int age;
};

Explanation:

  • class Dog: This line declares a class named Dog. Classes are blueprints for creating objects.
  • public:: This keyword defines the public section of the class. Members declared here are accessible from outside the class.
  • Dog(string name, int age) : name(name), age(age) {}: This is the constructor. It's called whenever a new Dog object is created. It initializes the name and age data members.
  • void bark() const { ... } and void displayInfo() const { ... }: These are methods (functions within the class). The const keyword indicates that these methods do not modify the object's data members.
  • private:: This keyword defines the private section of the class. Members declared here are only accessible within the class itself.
  • string name; and int age;: These are the data members (attributes) of the Dog class. They store information about each individual dog object.

Creating and Using Dog Objects

int main() {
    // Creating two Dog objects
    Dog rover("Rover", 3);
    Dog bella("Bella", 5);

    // Accessing and using the methods
    rover.bark(); // Output: Rover barks!
    bella.displayInfo(); // Output: 
    // Name: Bella
    // Age: 5

    return 0;
}

Explanation:

  • Dog rover("Rover", 3); and Dog bella("Bella", 5);: These lines create two Dog objects named rover and bella. The constructor is called automatically, initializing the objects with the given names and ages.
  • rover.bark(); and bella.displayInfo();: These lines call the methods defined in the Dog class. We use the dot operator (.) to access the methods of an object.

Key Concepts

  • Classes: Blueprints for creating objects. They define the data members and methods an object will have.
  • Objects: Instances of a class. They represent real-world entities.
  • Constructors: Special methods called when an object is created, used to initialize data members.
  • Methods: Functions defined within a class, used to perform actions on an object.
  • Data Members: Variables that store information specific to each object.
  • Public/Private Access Specifiers: Control how members of a class can be accessed from outside the class.

This simple example provides a foundation for understanding the fundamental concepts of C++ classes. You can build upon this knowledge by creating more complex classes with numerous data members and methods, reflecting diverse real-world objects and systems.

Featured Posts