Professional Documents
Culture Documents
class MyClass {
private:
int value;
public:
MyClass(int num) : value(num) {}
friend std::ostream& operator<<(std::ostream& os, const MyClass& obj) {
os << "Value: " << obj.value; // Customize the output format
return os;
}
};
OPERATOR >>
friend std::istream& operator>>(std::istream& is, MyClass& obj) {
is >> obj.value; // Read the input value into obj.value
return is;
OPERATOR +=
MyClass& operator+=(const MyClass& other) {
value += other.value; // Perform addition and assignment
return *this;
}
OPERATOR -=
MyNumber& operator-=(const MyNumber& other) {
value -= other.value;
return *this;
}
OPERATOR == / < / > / <= / >=
bool operator==(const MyNumber& other) const {
return value == other.value;
}
OPERATOR =
MyNumber& operator=(const MyNumber& other) {
if (this != &other) {
value = other.value;
}
return *this;
}
OPERATOR ++ / --
MyClass& operator++() {
++value;
return *this;
}
OPERATOR += FOR ADDING ELEMENT IN A DYNAMICALLY ALLOCATED ARRAY
#include <iostream>
class FirstClass {
private:
int value;
public:
FirstClass(int val) : value(val) {}
class SecondClass {
private:
FirstClass** array; // Array of dynamically allocated FirstClass objects
int size; // Size of the array
public:
SecondClass(int n) : size(n) {
array = new FirstClass*[size];
for (int i = 0; i < size; i++) {
array[i] = nullptr;
}
}
#include <iostream>
class FirstClass {
private:
int value;
public:
FirstClass(int val) : value(val) {}
class SecondClass {
private:
FirstClass** array; // Array of dynamically allocated FirstClass objects
int size; // Size of the array
public:
SecondClass(int n) : size(n) {
array = new FirstClass*[size];
for (int i = 0; i < size; i++) {
array[i] = nullptr;
}
}