0% found this document useful (0 votes)
7 views

basics of oop

The document explains key concepts in object-oriented programming, including classes, functions, objects, constructors, and memory allocation. It also covers advanced topics such as finalizers, encapsulation, the 'this' operator, copy constructors, immutable classes, composition, and aggregation. Each concept is defined with examples to illustrate their usage and differences.

Uploaded by

sanawarali920
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

basics of oop

The document explains key concepts in object-oriented programming, including classes, functions, objects, constructors, and memory allocation. It also covers advanced topics such as finalizers, encapsulation, the 'this' operator, copy constructors, immutable classes, composition, and aggregation. Each concept is defined with examples to illustrate their usage and differences.

Uploaded by

sanawarali920
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Class

A class is a blueprint or template that defines the


structure and behaviour (properties and methods) of objects.
It acts as a model to create objects in object-oriented
programming.
Function
A function is a block of reusable code designed to perform a
specific task. It can take inputs (parameters), process them,
and return an output.
Object
An object is an instance of a class. It represents a real-world
entity with specific properties (attributes) and behaviors
(methods) defined by the class.
Constructor
A constructor is a special method in a class that is
automatically called when an object is created. It initializes
the object's attributes and can also set up any required setup.
It has the same name as the class and no return type.
Overloaded constructor
An overloaded constructor is when a class has multiple
constructors with the same name but different parameter
lists. It allows objects to be initialized in different ways.
Memory allocation of object
The memory allocation of an object depends on whether it is
created statically or dynamically in object-oriented
programming:
1. Static Memory Allocation:
 The object is created on the stack.
 Memory is automatically managed and deallocated
when the object goes out of scope.
 Example:
 ClassName obj; // Memory allocated on stack
2. Dynamic Memory Allocation:
 The object is created on the heap using the new
keyword.
 You need to manually deallocate the memory using the
delete keyword.
 Example:
 ClassName* obj = new ClassName(); // Memory
allocated on heap
 delete obj; // Deallocates the memory
Key Difference:
 Static allocation is faster but limited in scope and
lifetime.
 Dynamic allocation provides flexibility but requires
manual management to prevent memory leaks.
Finilizer
A finalizer (also known as a destructor in languages like C++)
is a special method in object-oriented programming that is
called automatically when an object is no longer needed or is
being destroyed. Its purpose is to free up resources (like
memory, file handles, or database connections) held by the
object.
Encapsulation
Binding data and method within a class
Object as parameter in function
#include <iostream>
#include <string>
using namespace std;

// 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;
}

// Method to set details


void setDetails(string n, int a) {
name = n;
age = a;
}

// Method to update details (object passed as argument)


void updateDetails(const Person& p) {
name = p.name;
age = p.age;
}

// Method to create and return a new Person object


static Person createPerson(string n, int a) {
return Person(n, a); // Returning an object
}
};
int main() {
// Creating an object
Person p1("John", 25);
cout << "Original details of p1:\n";
p1.display();

// Passing object to method


Person p2;
p2.updateDetails(p1); // Copying details from p1 to p2
cout << "\nDetails of p2 after copying from p1:\n";
p2.display();

// Returning an object from method


Person p3 = Person::createPerson("Alice", 30);
cout << "\nDetails of p3 created using the static method:\
n";
p3.display();

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.

You might also like