A Class is a user-defined data-type which has data members and member functions. Data members are the data variables and member functions used to manipulate these variables. Class is basically a template for objects.
An Object is a instance of a class. To create an object of class, specify the class name, followed by the object name.
Let's create a class called "Person" to represent a person's information:
class Person
{
// Access Specifier
public:
// Data members
string name;
int age;
// Member function to introduce the person
void introduce()
{
cout << "Hi, my name is " << name << " and I am " << age << " years old." << endl;
}
};
int main()
{
// Creating objects of the Person class
Person person1;
Person person2;
// Setting the data members of person1
person1.name = "Alice";
person1.age = 25;
// Setting the data members of person2
person2.name = "Bob";
person2.age = 30;
// Using member functions to introduce the persons
person1.introduce();
person2.introduce();
return 0;
}
Access Specifiers:
1. `public`:
2. `private`:
3. `protected`:
- Protected members are similar to private members but have additional accessibility in derived classes.
- Protected data members and member functions can be accessed by derived classes but not by objects of the class or any other part of the program.
class MyClass
{
public:
int publicVar; // Public data member
void publicFunction()
{
// Public member function
// Can access publicVar, privateVar, and protectedVar
}
private:
int privateVar; // Private data member
void privateFunction()
{
// Private member function
// Can only access privateVar
}
protected:
int protectedVar;
void protectedFunction()
{
// Protected member function
// Can access protectedVar and privateVar
}
};
int main()
{
MyClass obj;
obj.publicVar = 10; // Accessing public member
obj.publicFunction(); // Calling public member function
// obj.privateVar = 20; // Error: Cannot access private member
// obj.privateFunction();// Error: Cannot access private member
// obj.protectedVar = 30; // Error: Cannot access protected member
// obj.protectedFunction(); // Error: Cannot access protected member
return 0;
}
NOTE: In a class, the default access specifier for its members is "private". This means that if you don't specify an access specifier for a member, it will be considered private by default. In a structure, the default access specifier for its members is "public". If you don't explicitly mention an access specifier, it will be considered public. (alert-success)
Class Methods:
Class methods, also known as member functions, are functions defined within a class that operate on the class's data members. There are two ways to define functions that belongs to a class:
1. Defining Member Functions Inside the Class:
Member functions can be defined directly inside the class declaration.
Example:
class Circle
{
private:
double radius;
public:
void setRadius(double newRadius)
{
radius = newRadius;
}
double getArea() const
{
return 3.14159 * radius * radius;
}
};
In the above example, both `setRadius()` and `getArea()` functions are defined inside the class. They provide functionality to set the radius of the circle and calculate its area.
2. Defining Member Functions Outside the Class:
Member functions can also be defined outside the class declaration using the scope resolution operator (::) to specify the class they belong to.
Example:
class Rectangle
{
private:
double length;
double width;
public:
void setLength(double newLength);
void setWidth(double newWidth);
double getArea() const;
};
// Definitions of member functions outside the class
void Rectangle::setLength(double newLength)
{
length = newLength;
}
void Rectangle::setWidth(double newWidth)
{
width = newWidth;
}
double Rectangle::getArea() const
{
return length * width;
}
In the above example, the member functions `setLength()`, `setWidth()`, and `getArea()` are declared inside the class, but their definitions are placed outside the class using the scope resolution operator (::).
Classification of Member Functions:
There are several types of member functions, including accessor functions (or getter functions), mutator functions (or setter functions), and utility functions. Let's discuss each of them:
1. Accessor or Getter Functions:
- Accessor functions are used to retrieve the values of private data members of a class.
- They are defined as const member functions because they should not modify the object's state.
- Accessor functions provide read-only access to the private data members, allowing other parts of the program to obtain the values.
- Typically, accessor functions have a return type that matches the data member they retrieve.
Example:
class Rectangle
{
private:
double length;
double width;
public:
double getLength() const
{
return length;
}
double getWidth() const
{
return width;
}
};
In the above example, `getLength()` and `getWidth()` are accessor functions that allow retrieving the values of the private data members `length` and `width`, respectively.
2. Mutator or Setter Functions:
- Mutator functions are used to modify the values of private data members of a class.
- They provide a way to update the internal state of an object by setting new values for the private data members.
- Mutator functions do not return a value (or return `void`), but they typically take parameters to update the data members.
Example:
class Rectangle
{
private:
double length;
double width;
public:
void setLength(double newLength)
{
length = newLength;
}
void setWidth(double newWidth)
{
width = newWidth;
}
};
In the above example, `setLength()` and `setWidth()` are mutator functions that allow setting new values for the private data members `length` and `width`, respectively.
3. Utility Functions:
- Utility functions perform specific operations related to the class but may not directly manipulate the class's data members.
- They can be used for calculations, conversions, or any other functionality associated with the class.
Example:
class MathOperations
{
public:
static int square(int number)
{
return number * number;
}
};
In the above example, `square()` is a utility function defined as a static member function. It calculates the square of a given number, but it does not require any access to the object's data members.