C++ Class Definition Syntax

5 min read Jul 01, 2024
C++ Class Definition Syntax

C++ Class Definition Syntax

A class in C++ is a blueprint or a prototype that defines the variables and methods (functions) that can be used to create objects of that class. It encapsulates data and behavior within a single unit.

Here's a breakdown of the syntax for defining a class in C++:

class ClassName { 
    // Data members (variables)
    private:
        // Private data members can only be accessed within the class
        int data1; 
        string data2;

    // Member functions (methods)
    public:
        // Public member functions can be accessed from outside the class
        void method1();
        int method2(int param);
};

Explanation:

1. Class Keyword:

  • The class keyword is used to declare a class.

2. Class Name:

  • ClassName is the name you choose for your class. It should be a descriptive and meaningful identifier.

3. Class Body:

  • The body of the class is enclosed within curly braces ({}).
  • Inside the body, you define:
    • Data Members (Variables): These are the variables that represent the data associated with objects of this class.
    • Member Functions (Methods): These are functions that define the behavior of objects of this class.

4. Access Specifiers:

  • C++ uses access specifiers to control the visibility of class members. Common access specifiers are:
    • public: Members declared public can be accessed from anywhere.
    • private: Members declared private can only be accessed from within the class itself.
    • protected: Members declared protected can be accessed from within the class, derived classes, and friend classes.

5. Data Members:

  • Data members are the variables that hold the data associated with an object of the class.
  • In this example, data1 is an integer variable, and data2 is a string variable.

6. Member Functions:

  • Member functions define the behavior of objects of the class.
  • They can access and manipulate the data members of the class.
  • In this example, method1() is a void function that does not return a value, and method2() is an integer function that takes an integer parameter and returns an integer value.

Example:

#include 
#include 

using namespace std;

class Employee {
    private:
        int employeeID;
        string name;
        double salary;

    public:
        // Constructor
        Employee(int id, string n, double s) {
            employeeID = id;
            name = n;
            salary = s;
        }
        
        // Method to display employee details
        void displayDetails() {
            cout << "Employee ID: " << employeeID << endl;
            cout << "Name: " << name << endl;
            cout << "Salary: " << salary << endl;
        }
};

int main() {
    Employee emp1(101, "John Doe", 50000.00); 
    emp1.displayDetails();
    
    return 0;
}

Output:

Employee ID: 101
Name: John Doe
Salary: 50000

This example demonstrates how to define a class, create an object of the class, and call its member functions. The Employee class encapsulates data (employee ID, name, salary) and behavior (displaying employee details).

Remember that this is a basic introduction to class definition syntax. C++ offers numerous advanced features and concepts for working with classes, such as inheritance, polymorphism, and more.

Featured Posts