C++ Objects and Class

h‮//:sptt‬www.theitroad.com

What are Classes and Objects?

In C++, classes and objects are used to implement object-oriented programming (OOP) concepts. A class is a user-defined type that encapsulates data and functionality into a single unit. An object is an instance of a class, created from the class blueprint, which has its own set of data and functions.

Defining a Class:

A class is defined using the class keyword, followed by the class name and a set of curly braces. Inside the curly braces, you can define data members and member functions.

Here's an example of a simple class definition:

class Person {
public:
    std::string name;
    int age;

    void introduce() {
        std::cout << "Hi, my name is " << name << " and I am " << age << " years old." << std::endl;
    }
};

In this example, the Person class has two data members, name and age, which are of type std::string and int, respectively. It also has a member function, introduce(), which prints a message to the console.

Creating Objects:

Once you've defined a class, you can create objects from it using the class name and the object name, like this:

Person john;

This creates an object of the Person class named john. You can then access the data members and member functions of the object using the dot operator, like this:

john.name = "John Doe";
john.age = 30;
john.introduce();

This code sets the name and age data members of the john object and calls the introduce() member function to print a message to the console.

You can also create objects dynamically using the new keyword, like this:

Person* jane = new Person();

This creates a dynamic object of the Person class named jane. You can access the data members and member functions of the object using the arrow operator, like this:

jane->name = "Jane Doe";
jane->age = 25;
jane->introduce();

This code sets the name and age data members of the jane object and calls the introduce() member function to print a message to the console.

Conclusion:

In summary, a class is a user-defined type that encapsulates data and functionality into a single unit, while an object is an instance of a class. You can define a class using the class keyword, and create objects using the class name and the object name. Once you have created an object, you can access its data members and member functions using the dot operator or the arrow operator.

Declaring a Class and Creating Objects

To declare a class in C++, you use the class keyword followed by the name of the class. Here's an example:

class MyClass {
  public:
    int myInt;
    void myMethod();
};

In this example, we've declared a class called MyClass with two members: an integer called myInt and a method called myMethod.

To create an object of the MyClass class, you use the new keyword to allocate memory for the object on the heap. Here's an example:

MyClass* obj = new MyClass();

In this example, we've created a pointer to a MyClass object called obj and used the new keyword to allocate memory for the object on the heap. We can access the members of the object using the . operator. For example:

obj->myInt = 5;
obj->myMethod();

In this example, we've set the myInt member of the obj object to 5 and called the myMethod() method of the obj object. Note that we use the -> operator to access the members of the object through the pointer.

When you're finished with the object, you should use the delete keyword to free the memory allocated on the heap. Here's an example:

delete obj;

In this example, we've used the delete keyword to free the memory allocated for the obj object on the heap.