C++ and Object-Oriented Programming
Static Members; Copy Constructor; Operator new and delete; C++ vs Java
C-CAT
Static Members
Static Data Members
Shared across all objects of the class — only one copy.
class Student {
private:
string name;
static int count; // shared by all Student objects
public:
Student(string n) : name(n) { count++; }
~Student() { count--; }
static int getCount() { return count; } // static method — no `this`
};
int Student::count = 0; // MUST initialize outside class
int main() {
cout << Student::getCount() << endl; // 0
Student s1("Alice");
Student s2("Bob");
cout << Student::getCount() << endl; // 2
{
Student s3("Charlie");
cout << Student::getCount() << endl; // 3
} // s3 destroyed
cout << Student::getCount() << endl; // 2
return 0;
}
Copy Constructor
What is a Copy Constructor?
Special constructor that creates a new object as a copy of an existing object.
class Array {
private:
int *data;
int size;
public:
// Regular constructor
Array(int n) : size(n) {
data = new int[size];
}
// DEEP copy constructor (from another Array object)
Array(const Array &other) : size(other.size) {
data = new int[size]; // allocate NEW memory
for (int i = 0; i < size; i++) {
data[i] = other.data[i]; // copy values
}
}
~Array() { delete[] data; }
};
int main() {
Array a1(5);
Array a2 = a1; // Copy constructor called
Array a3(a1); // Copy constructor called (explicit)
return 0;
}
Shallow copy (default): Copies pointer → both objects point to same memory (DANGEROUS with dynamic allocation!) Deep copy: Allocates new memory, copies values → safe.
Operator new and delete
// Allocate single object
int *p = new int; // default initialized (garbage)
int *q = new int(42); // value initialized to 42
string *s = new string("Hello");
// Allocate array
int *arr = new int[10]; // array of 10 ints
int *arr2 = new int[10]();
// zero-initialized
// Delete
delete p; // single object
delete[] arr; // array (must use
delete[])
// Custom allocation
MyClass *obj = new MyClass(arg1, arg2); // calls constructor
delete
obj; // calls destructor + frees memory
// nothrow new
int *safe = new(nothrow) int[1000000];
if (safe == nullptr) cout << "Allocation failed\n";
C++ vs Java
| Feature | C++ | Java |
|---|---|---|
| Memory management | Manual (new/delete) or smart pointers | Automatic (GC) |
| Pointers | Full pointer support | No raw pointers (references only) |
| Multiple inheritance | Yes | No (multiple interface implementation) |
| Operator overloading | Yes | No |
| Compilation | Compiled to machine code | Compiled to bytecode (JVM) |
| Platform | Platform dependent | Platform independent (JVM) |
| Call by value/ref | Both supported | Objects by reference, primitives by value |
| Header files | Required | No |
| Namespaces | namespaces | packages |
| Strings | char array + string class | Built-in String class |
| Destructor | Explicit (called when out of scope) | finalize() — unpredictable |
| Performance | Faster (no GC overhead) | Slower (JVM + GC overhead) |
| Exception | Optional | Mandatory (checked exceptions) |
Continue learning
Related notes
Definition of AI; Need of AI
Artificial Intelligence
Introduction to Data Engineering; Big Data — The 5 V's; Types of Data
Big Data and Data Engineering
Introduction to C Programming; C Program Structure; Data Types and Variables
C Programming
What Is a Computer?; Machine Cycle: Fetch–Decode–Execute; CPU Organization
Computer Architecture
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.