C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

Define Class and Create Object in C++

In C++, a class is a user-defined data type that represents a concept or an entity with properties (data members) and associated operations (member functions). An object is an instance of a class.

Defining a Class

To define a class in C++, use the class keyword followed by the class name and the class body enclosed in curly braces {}. The class body contains data members (variables) and member functions (methods).

Here's an example of a simple class representing a Person:

class Person {
    // Data members
    std::string name;
    int age;

    // Member functions
    void setName(std::string newName) {
        name = newName;
    }

    void setAge(int newAge) {
        age = newAge;
    }

    std::string getName() {
        return name;
    }

    int getAge() {
        return age;
    }
};

Creating Objects

To create an object (instance) of a class, use the class name followed by the object name. You can also initialize the object's data members using a constructor if one is defined in the class.

Example:

Person person1; // Creating an object of the Person class

Accessing Class Members

Use the dot operator (.) to access an object's data members and member functions.

Example:

person1.setName("John");
person1.setAge(30);

std::string personName = person1.getName();
int personAge = person1.getAge();

Putting it all together

Here's a complete example that demonstrates defining a class, creating an object, and accessing class members:

#include <iostream>
#include <string>

class Person {
    // Data members
    std::string name;
    int age;

public:
    // Member functions
    void setName(std::string newName) {
        name = newName;
    }

    void setAge(int newAge) {
        age = newAge;
    }

    std::string getName() {
        return name;
    }

    int getAge() {
        return age;
    }
};

int main() {
    // Creating an object of the Person class
    Person person1;

    // Setting and getting data members using member functions
    person1.setName("John");
    person1.setAge(30);

    std::string personName = person1.getName();
    int personAge = person1.getAge();

    std::cout << "Name: " << personName << ", Age: " << personAge << std::endl;

    return 0;
}

In this example, we define a Person class with data members name and age, as well as member functions for setting and getting the values of these data members. In the main() function, we create an object person1 of the Person class, set its data members using the member functions, and retrieve and display the data member values.

In conclusion, a class in C++ is a user-defined data type representing an entity or concept with properties and operations. Objects are instances of classes that encapsulate data and behavior. You can define classes, create objects, and access class members using the dot operator to create powerful, modular, and maintainable code.

  1. How to define a class in C++:

    • Description: In C++, a class is a blueprint for creating objects. It encapsulates data and functions that operate on that data.
    • Example Code:
      #include <iostream>
      
      // Class definition
      class MyClass {
      public:
          int data;
          void display() {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
      int main() {
          // Creating an object of MyClass
          MyClass obj;
          obj.data = 42;
          obj.display();
      
          return 0;
      }
      
  2. C++ class constructor and destructor:

    • Description: Introduces the constructor and destructor methods, which are called during object creation and destruction, respectively.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          // Constructor
          MyClass() {
              std::cout << "Constructor called!" << std::endl;
          }
      
          // Destructor
          ~MyClass() {
              std::cout << "Destructor called!" << std::endl;
          }
      };
      
      int main() {
          // Creating an object invokes the constructor
          MyClass obj;
      
          // Destructor is automatically called when the object goes out of scope
          return 0;  // Destructor called!
      }
      
  3. C++ class and object initialization:

    • Description: Describes different ways to initialize class members during object creation.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          int data;
      
          // Parameterized constructor for initialization
          MyClass(int value) : data(value) {}
      
          void display() {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
      int main() {
          // Initializing object using a parameterized constructor
          MyClass obj(42);
          obj.display();
      
          return 0;
      }
      
  4. C++ class declaration and definition:

    • Description: Differentiates between class declaration and definition, where declaration provides a forward declaration of the class.
    • Example Code:
      // Class Declaration (usually in a header file)
      class MyClass;
      
      int main() {
          // Class Definition
          class MyClass {
          public:
              int data;
              void display();
          };
      
          return 0;
      }