C++ Classes and Objects – A Comprehensive Guide with Example

The programming language C++ is object-oriented.

The classes and objects in C++, along with their attributes and methods, are the foundation of everything. For instance, a car is an object in the real world. The car has characteristics like weight and color, as well as functions like drive and brake.

In essence, attributes and methods are variables and operations that are unique to a certain class. The term “class members” is frequently used to refer to them.

A class serves as an object constructor or “blueprint” for making objects and is a user-defined data type that we can use in our program.


Create a Class

Use the class keyword to create a class:

Syntax:

class Geektocode {   // Class defind
public:               // Access specifier
int geektocode;      //Attribute (int variable)
};
Explanation:
  • A class named Geektocode is created using the class keyword.
  • The public keyword signifies that a class’s members (attributes and methods) are accessible to users outside of the class. Access specifiers will be covered in greater detail later.
  • There is a single integer variable inside the class. Attributes are used to describe variables that are defined inside a class.
  • Finally, add a semicolon (;) at the end of the class definition.

Create an Object

An object in C++ is created from a class. We have already created the class named GeektoCode, so now we can use this to create objects.

To create an object of GeektoCode, specify the class name, followed by the object name.

To access the class attributes (geek), use the dot syntax (.) on the object:

Example:

#include <iostream>
//geektocode.com
using namespace std;

class GeektoCode {       // The class
  public:             // Access specifier
    int geek;        // Attribute (int variable)
};

int main() {
  GeektoCode geekobj;  // Create an object of MyClass

  // Access attributes and set values
  geekobj.geek = 2023;

  // Print values
  cout << geekobj.geek << "\n"; 
  return 0;
}
OUTPUT:
2023

Class with Multiple Objects – Examples

#include <iostream>
using namespace std;

class Circle {
private:
    double radius;

public:
    Circle(double r) {
        radius = r;
    }

    double calculateArea() {
        return 3.14 * radius * radius;
    }
};

int main() {
    Circle circle1(5.0);
    Circle circle2(2.7);

    double area1 = circle1.calculateArea();
    double area2 = circle2.calculateArea();

    cout << "Area of circle 1: " << area1 << endl;
    cout << "Area of circle 2: " << area2 << endl;

    return 0;
}
OUTPUT:
Area of circle 1: 78.5
Area of circle 2: 22.8906

In conclusion, the fundamental building blocks of C++ programming are classes and objects. Classes offer a means of encapsulating data and associated functions, aiding in the organization and reuse of code. On the other hand, objects give us the ability to make numerous instances of a class, each with its own state and behavior. Developers can create modular, effective, and scalable applications by using classes and objects.

Leave a Reply

Your email address will not be published. Required fields are marked *