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
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:
setDimensions(double w, double h)
sets the dimensions of a rectangle.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.
Defining member variables in C++ classes:
#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; }
C++ class member functions examples:
#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; }
Private and public member variables in C++:
#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; } };
C++ class member function overloading:
#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; } };
How to initialize member variables in C++ classes:
#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; } };
C++ static member variables and functions:
#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;
Inheritance and member functions in C++:
#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; } };
C++ const member functions and variables:
#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; } };