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

FeatureC++Java
Memory managementManual (new/delete) or smart pointersAutomatic (GC)
PointersFull pointer supportNo raw pointers (references only)
Multiple inheritanceYesNo (multiple interface implementation)
Operator overloadingYesNo
CompilationCompiled to machine codeCompiled to bytecode (JVM)
PlatformPlatform dependentPlatform independent (JVM)
Call by value/refBoth supportedObjects by reference, primitives by value
Header filesRequiredNo
Namespacesnamespacespackages
Stringschar array + string classBuilt-in String class
DestructorExplicit (called when out of scope)finalize() — unpredictable
PerformanceFaster (no GC overhead)Slower (JVM + GC overhead)
ExceptionOptionalMandatory (checked exceptions)

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.