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

Member Variables And Member Functions Of C++ Classes

In C++, classes are user-defined data types that represent an entity or concept. Classes have two types of members: member variables (also known as data members) and member functions (also known as methods). This tutorial will explain the basics of member variables and member functions in C++ classes.

Member Variables

Member variables (data members) are the properties of a class. They store the state or data associated with an object of the class.

Example:

class Rectangle {
    double width;
    double height;
};

In this example, the Rectangle class has two data members, width and height, that represent the dimensions of a rectangle.

Member Functions

Member functions (methods) are functions that belong to a class and can access its member variables. They define the behavior or operations that can be performed on an object of the class.

Example:

class Rectangle {
    double width;
    double height;

public:
    void setDimensions(double w, double h) {
        width = w;
        height = h;
    }

    double getArea() {
        return width * height;
    }
};

In this example, the Rectangle class has two member functions:

  1. setDimensions(double w, double h) sets the dimensions of a rectangle.
  2. getArea() calculates and returns the area of a rectangle.

Accessing Member Variables and Member Functions

To access member variables and member functions of a class, you need to create an object (instance) of the class and use the dot operator (.).

Example:

#include <iostream>

class Rectangle {
    double width;
    double height;

public:
    void setDimensions(double w, double h) {
        width = w;
        height = h;
    }

    double getArea() {
        return width * height;
    }
};

int main() {
    Rectangle rect;
    rect.setDimensions(5, 3);

    double area = rect.getArea();
    std::cout << "Area of the rectangle: " << area << std::endl;

    return 0;
}

In this example, we create an object rect of the Rectangle class, set its dimensions using the setDimensions() member function, and calculate and display its area using the getArea() member function.

In conclusion, member variables and member functions are essential components of C++ classes. Member variables represent the properties or state of a class, while member functions define the behavior or operations that can be performed on an object of the class. You can create objects of a class and use the dot operator to access and manipulate their member variables and member functions.

  1. Defining member variables in C++ classes:

    • Description: Member variables are attributes or properties of a class. They represent the state of the objects created from the class.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          // Member variable
          int data;
      };
      
      int main() {
          // Creating an object of MyClass with a member variable
          MyClass obj;
          obj.data = 42;
      
          std::cout << "Data: " << obj.data << std::endl;
      
          return 0;
      }
      
  2. C++ class member functions examples:

    • Description: Member functions are operations or behaviors associated with a class. They define how objects of the class interact.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          // Member variable
          int data;
      
          // Member function
          void display() {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
      int main() {
          // Creating an object of MyClass and calling a member function
          MyClass obj;
          obj.data = 42;
          obj.display();
      
          return 0;
      }
      
  3. Private and public member variables in C++:

    • Description: Introduces the concept of access specifiers (private and public) for member variables, controlling their visibility.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      private:
          // Private member variable
          int privateData;
      
      public:
          // Public member variable
          int publicData;
      
          // Member function with access to privateData
          void setPrivateData(int value) {
              privateData = value;
          }
      };
      
  4. C++ class member function overloading:

    • Description: Demonstrates how a class can have multiple member functions with the same name but different parameters.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          void display(int value) {
              std::cout << "Displaying int: " << value << std::endl;
          }
      
          void display(double value) {
              std::cout << "Displaying double: " << value << std::endl;
          }
      };
      
  5. How to initialize member variables in C++ classes:

    • Description: Describes various ways to initialize member variables during object creation using constructors.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          // Constructor for initializing member variable
          MyClass(int value) : data(value) {}
      
          // Member variable
          int data;
      
          // Member function
          void display() {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
  6. C++ static member variables and functions:

    • Description: Introduces static member variables and functions, which are shared among all objects of the class.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          // Static member variable
          static int staticData;
      
          // Static member function
          static void staticFunction() {
              std::cout << "Static function called." << std::endl;
          }
      };
      
      // Initializing static member variable
      int MyClass::staticData = 0;
      
  7. Inheritance and member functions in C++:

    • Description: Illustrates how inheritance allows a class to inherit member functions from another class.
    • Example Code:
      #include <iostream>
      
      // Base class
      class BaseClass {
      public:
          void baseFunction() {
              std::cout << "Base function called." << std::endl;
          }
      };
      
      // Derived class inheriting from BaseClass
      class DerivedClass : public BaseClass {
      public:
          void derivedFunction() {
              std::cout << "Derived function called." << std::endl;
          }
      };
      
  8. C++ const member functions and variables:

    • Description: Introduces const member functions and variables, which cannot modify member variables.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          // Constant member variable
          const int constantData = 42;
      
          // Constant member function
          void display() const {
              std::cout << "Constant Data: " << constantData << std::endl;
          }
      };