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

Classes And Objects in C++

In C++, classes are user-defined data types that represent an entity or concept. They contain properties (data members) and associated operations (member functions). Objects are instances of classes, encapsulating data and behavior.

Defining a Class

To define a class, 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).

Example of a simple Car class:

class Car {
    // Data members
    std::string make;
    std::string model;
    int year;

public:
    // Member functions
    void setMake(std::string newMake) {
        make = newMake;
    }

    void setModel(std::string newModel) {
        model = newModel;
    }

    void setYear(int newYear) {
        year = newYear;
    }

    std::string getMake() {
        return make;
    }

    std::string getModel() {
        return model;
    }

    int getYear() {
        return year;
    }
};

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:

Car car1; // Creating an object of the Car class

Accessing Class Members

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

Example:

car1.setMake("Toyota");
car1.setModel("Camry");
car1.setYear(2020);

std::string carMake = car1.getMake();
std::string carModel = car1.getModel();
int carYear = car1.getYear();

Complete Example

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

#include <iostream>
#include <string>

class Car {
    // Data members
    std::string make;
    std::string model;
    int year;

public:
    // Member functions
    void setMake(std::string newMake) {
        make = newMake;
    }

    void setModel(std::string newModel) {
        model = newModel;
    }

    void setYear(int newYear) {
        year = newYear;
    }

    std::string getMake() {
        return make;
    }

    std::string getModel() {
        return model;
    }

    int getYear() {
        return year;
    }
};

int main() {
    // Creating an object of the Car class
    Car car1;

    // Setting and getting data members using member functions
    car1.setMake("Toyota");
    car1.setModel("Camry");
    car1.setYear(2020);

    std::string carMake = car1.getMake();
    std::string carModel = car1.getModel();
    int carYear = car1.getYear();

    std::cout << "Make: " << carMake << ", Model: " << carModel << ", Year: " << carYear << std::endl;

    return 0;
}

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

  1. Creating and using objects in C++:

    • Description: Objects are instances of classes in C++. They encapsulate data and behavior, allowing you to model real-world entities.
    • Example Code:
      #include <iostream>
      
      // Class definition
      class MyClass {
      public:
          int data;
      
          void display() {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
      int main() {
          // Creating objects of MyClass
          MyClass obj1;
          MyClass obj2;
      
          // Using objects
          obj1.data = 42;
          obj2.data = 64;
      
          obj1.display();
          obj2.display();
      
          return 0;
      }
      
  2. C++ constructor and destructor in classes:

    • Description: Constructors are special member functions called during object creation, while destructors are called during object destruction.
    • 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. Inheritance in C++ classes:

    • Description: Introduces inheritance, allowing a class to inherit properties and behaviors from another class.
    • Example Code:
      #include <iostream>
      
      // Base class
      class Animal {
      public:
          void eat() {
              std::cout << "Animal is eating." << std::endl;
          }
      };
      
      // Derived class inheriting from Animal
      class Dog : public Animal {
      public:
          void bark() {
              std::cout << "Dog is barking." << std::endl;
          }
      };
      
  4. Friend functions and classes in C++:

    • Description: Discusses friend functions and classes that have access to private and protected members of another class.
    • Example Code:
      #include <iostream>
      
      // Forward declaration
      class MyClass;
      
      // Friend function
      void friendFunction(MyClass obj);
      
      // Class definition
      class MyClass {
      private:
          int privateData;
      
      public:
          MyClass(int value) : privateData(value) {}
      
          // Friend function declaration
          friend void friendFunction(MyClass obj);
      };
      
      // Friend function definition
      void friendFunction(MyClass obj) {
          std::cout << "Friend Function accessing privateData: " << obj.privateData << std::endl;
      }