Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ Classes & Objects: A Practical Guide with Code Examples

What is a Class?

A C++ class is a user‑defined data type that bundles data and the functions that operate on that data into a single entity. Think of it as a blueprint: it defines the shape and behaviour of objects that can be created from it.

Just as a house blueprint specifies the placement of doors, windows, and rooms, a class describes the layout of its data members and the operations that can manipulate them. Multiple houses (objects) can be built from the same blueprint.

C++ Classes & Objects: A Practical Guide with Code Examples

In the illustration above, the single prototype produces two distinct houses with different features.

In this tutorial you’ll learn:

Class Declaration

Declaring a class is straightforward: use the class keyword followed by the class name and enclose the body within curly braces.

Syntax

class className
{
    // data members
    // member functions
};

Access Modifiers: private vs. public

Access modifiers control visibility of class members.

Creating Objects (Instantiation)

Objects are instances of a class. Declaring an object follows the same pattern as declaring a variable: className objectName;

Syntax

className objectName;

Instantiation allocates memory for the object and may trigger its constructor.

Accessing Data Members

Public data members are accessed using the dot (.) operator.

Example

#include <iostream>
using namespace std;

class Phone {
public:
    double cost;
    int slots;
};

int main() {
    Phone Y6;
    Phone Y7;

    Y6.cost = 100.0;
    Y6.slots = 2;

    Y7.cost = 200.0;
    Y7.slots = 2;

    cout << "Cost of Huawei Y6 : " << Y6.cost << endl;
    cout << "Cost of Huawei Y7 : " << Y7.cost << endl;

    cout << "Number of card slots for Huawei Y6 : " << Y6.slots << endl;
    cout << "Number of card slots for Huawei Y7 : " << Y7.slots << endl;

    return 0;
}

Output:

C++ Classes & Objects: A Practical Guide with Code Examples

Why Use Getters/Setters?

Accessing or modifying data directly can break encapsulation. Using getter and setter member functions provides controlled access.

Example with Encapsulation

#include <iostream>
using namespace std;

class ClassA {
public:
    void setA(int val);
    int getA() const;

private:
    int a;
};

int ClassA::getA() const {
    return a;
}

void ClassA::setA(int val) {
    a = val;
}

int main() {
    ClassA obj;
    obj.setA(20);
    cout << "Value of a is: " << obj.getA();
    return 0;
}

Output:

C++ Classes & Objects: A Practical Guide with Code Examples

Protected Members and Inheritance

Protected members are accessible within the class and by derived classes, enabling controlled inheritance.

Example

#include <iostream>
using namespace std;

class ParentClass {
protected:
    int value;
};

class ChildClass : public ParentClass {
public:
    void setId(int x) {
        value = x;
    }
    void displayValue() {
        cout << "Value is: " << value << endl;
    }
};

int main() {
    ChildClass c;
    c.setId(21);
    c.displayValue();
    return 0;
}

Output:

C++ Classes & Objects: A Practical Guide with Code Examples

Member Functions Inside vs. Outside the Class

Member functions can be defined inline within the class or declared inside and defined later using the scope resolution operator ::.

Example

#include <iostream>
#include <string>
using namespace std;

class Guru99 {
public:
    string tutorial_name;
    int id;
    void printName();
    void printId() {
        cout << "Tutorial id is: " << id;
    }
};

void Guru99::printName() {
    cout << "Tutorial name is: " << tutorial_name;
}

int main() {
    Guru99 guru;
    guru.tutorial_name = 'C++';
    guru.id = 1001;
    guru.printName();
    cout << endl;
    guru.printId();
    return 0;
}

Output:

C++ Classes & Objects: A Practical Guide with Code Examples

Constructors and Destructors

Constructors initialise objects when they are created, while destructors clean up just before an object is destroyed.

Example

#include <iostream>
using namespace std;

class ClassA {
public:
    ClassA() {
        cout << "Class constructor called" << endl;
    }
    ~ClassA() {
        cout << "Class destructor called" << endl;
    }
};

int main() {
    ClassA a;   // constructor called
    int p = 1;
    if (p) {
        ClassA b;   // constructor called, destructor called at end of block
    }
    // destructor for 'a' called here
    return 0;
}

Output:

C++ Classes & Objects: A Practical Guide with Code Examples

Key Takeaways

C Language

  1. C# Classes & Objects: Foundations for Robust OOP
  2. C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
  3. C++ Operators Explained: Types, Examples, and Sample Programs
  4. C++ Structs Explained with a Practical Example
  5. Mastering std::list in C++: Syntax, Functions & Practical Examples
  6. C++ Struct vs Class: Key Differences, Syntax, and Best Use Cases
  7. Understanding C# Interfaces: Definition, Example, and Practical Use
  8. C# Serialization & Deserialization: A Practical Example
  9. Understanding Java Classes and Objects: Clear Concepts, Practical Examples
  10. Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained