Scope Resolution in C++
what is use of scope resolution in oops in c++
The scope resolution operator (`::`) in C++ is used to access members (variables, functions, or classes)
that belong to a specific scope. It helps resolve ambiguity when multiple scopes have elements with the
same name.
Uses of Scope Resolution Operator (`::`) in OOPs (C++)
1. Access Global Variables When Shadowed
cpp
#include <iostream>
using namespace std;
int x = 10; // Global variable
class Test {
public:
int x = 20; // Member variable
void show() {
cout << "Local x: " << x << endl;
cout << "Global x: " << ::x << endl; // Accessing global x
}
};
int main() {
Test obj;
obj.show();
return 0;
}
Output:
sql
Local x: 20
Global x: 10
2. Define Class Member Functions Outside the Class
cpp
class Demo {
public:
void show(); // Function prototype
};
void Demo::show() { // Function definition outside class
cout << "Scope Resolution Operator Example" << endl;
}
int main() {
Demo obj;
obj.show();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/5
return 0;
}
3. Access Static Class Members
cpp
class Sample {
public:
static int count;
};
int Sample::count = 100; // Initialize static variable outside the class
int main() {
cout << "Static count: " << Sample::count << endl;
return 0;
}
Output:
scss
Static count: 100
4. Access Base Class Members in Multiple Inheritance
cpp
class A {
public:
void show() { cout << "Class A" << endl; }
};
class B {
public:
void show() { cout << "Class B" << endl; }
};
class C : public A, public B {
public:
void display() {
A::show(); // Resolving ambiguity
B::show();
}
};
int main() {
C obj;
obj.display();
return 0;
}
Output:
css
Class A
Class B
5. Access Namespace Members
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/5
cpp
#include <iostream>
namespace ns {
int x = 50;
}
int main() {
cout << "Namespace x: " << ns::x << endl;
return 0;
}
Output:
vbnet
Namespace x: 50
Thus, the scope resolution operator (`::`) helps access specific elements across different scopes,
prevents ambiguity, and ensures better code organization.
difference between shallow copy and deep copy in c++
Difference Between Shallow Copy and Deep Copy in C++
Feature Shallow Copy Deep Copy
Copies object members as they are, Creates a new copy of dynamically allocated
Definition including pointers (only the memory memory (allocates new memory and copies
address is copied). actual values).
Memory The new object shares the same memory The new object gets separate memory for
Allocation for dynamically allocated members. dynamically allocated members.
Changes in copied object do not affect the
Impact on Changes in copied object affect the
original object (since a new copy of data is
Original Object original object (due to shared memory).
created).
Slower, as it allocates new memory and copies
Performance Faster, as only pointers are copied.
the data.
Used when an object does not contain Used when an object has dynamically allocated
Use Case
dynamically allocated memory. memory to avoid accidental modifications.
Causes issues like dangling pointers and
Avoids such issues by maintaining
Risk double deletion during object
independent copies.
destruction.
Example of Shallow Copy
cpp
#include <iostream>
using namespace std;
class Shallow {
public:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/5
int* ptr;
// Constructor
Shallow(int val) {
ptr = new int(val);
}
// Default Copy Constructor (Shallow Copy)
Shallow(const Shallow& obj) {
ptr = obj.ptr; // Only copies the pointer address, not the actual value.
}
// Destructor
~Shallow() {
delete ptr; // May cause issues when multiple objects share the same pointer.
}
};
int main() {
Shallow obj1(10);
Shallow obj2 = obj1; // Shallow Copy
cout << "obj1: " << *obj1.ptr << endl;
cout << "obj2: " << *obj2.ptr << endl;
*obj2.ptr = 20; // Modifies obj1.ptr as well
cout << "After modification:" << endl;
cout << "obj1: " << *obj1.ptr << endl;
cout << "obj2: " << *obj2.ptr << endl;
return 0;
}
Output:
yaml
obj1: 10
obj2: 10
After modification:
obj1: 20
obj2: 20
🔴 Problem: Both `obj1` and `obj2` share the same memory. When one is modified, the other also gets
affected.
Example of Deep Copy
cpp
#include <iostream>
using namespace std;
class Deep {
public:
int* ptr;
// Constructor
Deep(int val) {
ptr = new int(val);
}
// Deep Copy Constructor
Deep(const Deep& obj) {
ptr = new int(*obj.ptr); // Creates a new copy of the data.
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/5
// Destructor
~Deep() {
delete ptr;
}
};
int main() {
Deep obj1(10);
Deep obj2 = obj1; // Deep Copy
cout << "obj1: " << *obj1.ptr << endl;
cout << "obj2: " << *obj2.ptr << endl;
*obj2.ptr = 20; // Changes only obj2
cout << "After modification:" << endl;
cout << "obj1: " << *obj1.ptr << endl;
cout << "obj2: " << *obj2.ptr << endl;
return 0;
}
Output:
yaml
obj1: 10
obj2: 10
After modification:
obj1: 10
obj2: 20
✅ Solution: Each object gets its own memory, preventing unintended modifications.
Conclusion
Use shallow copy when objects do not contain dynamically allocated memory.
Use deep copy when objects contain dynamically allocated memory to ensure each object has its
own copy.
For dynamically allocated objects, always implement deep copy to avoid dangling pointers and
memory leaks.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/5