basics of oop
basics of oop
// Class definition
class Person {
private:
string name;
int age;
public:
// Constructor
Person(string n = "", int a = 0) : name(n), age(a) {}
// Method to display person's details
void display() const {
cout << "Name: " << name << ", Age: " << age << endl;
}
return 0;
}
Use of this operator
The this operator in C++ is a special pointer that points to the
current object of a class. It is implicitly passed to all non-static
member functions, allowing access to the calling object.
Copy constructor
A copy constructor in C++ is a special constructor used to
create a new object as a copy of an existing object. It is called
when:
A new object is initialized from an existing object of the
same class.
An object is passed by value to a function or returned by
value from a function.
Syntax:
cpp
Copy code
ClassName(const ClassName &other);
Immutable class
An immutable class is a class whose objects cannot be
modified after they are created. Once an instance of the class
is initialized, its state (data) remains constant, and no setter
methods are provided to modify its attributes
Composition
Composition is an object-oriented programming (OOP)
concept where one class is made up of one or more objects
of other classes, forming a "has-a" relationship
Aggregation
Aggregation is a special form of association in object-oriented
programming (OOP) where one object contains references to
other objects, but unlike composition, the lifetime of the
contained objects is not controlled by the container object.
This means that the contained objects can exist
independently of the container object. Aggregation
represents a "has-a" relationship, similar to composition, but
with weaker ownership.